JAVA-9556 added try-with-resources for resultset

This commit is contained in:
keerthigadde 2022-05-26 15:38:47 +10:00
parent 33258722ca
commit 863356b4bc

View File

@ -14,33 +14,35 @@ public class MetadataExtractor {
} }
public void extractTableInfo() throws SQLException { public void extractTableInfo() throws SQLException {
ResultSet resultSet = databaseMetaData.getTables(null, null, "CUST%", new String[] { "TABLE" }); try (ResultSet resultSet = databaseMetaData.getTables(null, null, "CUST%", new String[] { "TABLE" })) {
while (resultSet.next()) { while (resultSet.next()) {
// Print the names of existing tables // Print the names of existing tables
System.out.println(resultSet.getString("TABLE_NAME")); System.out.println(resultSet.getString("TABLE_NAME"));
System.out.println(resultSet.getString("REMARKS")); System.out.println(resultSet.getString("REMARKS"));
} }
} }
}
public void extractSystemTables() throws SQLException { public void extractSystemTables() throws SQLException {
ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[] { "SYSTEM TABLE" }); try (ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[] { "SYSTEM TABLE" })) {
while (resultSet.next()) { while (resultSet.next()) {
// Print the names of system tables // Print the names of system tables
System.out.println(resultSet.getString("TABLE_NAME")); System.out.println(resultSet.getString("TABLE_NAME"));
} }
} }
}
public void extractViews() throws SQLException { public void extractViews() throws SQLException {
ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[] { "VIEW" }); try(ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[] { "VIEW" })) {
while (resultSet.next()) { while (resultSet.next()) {
// Print the names of existing views // Print the names of existing views
System.out.println(resultSet.getString("TABLE_NAME")); System.out.println(resultSet.getString("TABLE_NAME"));
} }
} }
}
public void extractColumnInfo(String tableName) throws SQLException { public void extractColumnInfo(String tableName) throws SQLException {
ResultSet columns = databaseMetaData.getColumns(null, null, tableName, null); try(ResultSet columns = databaseMetaData.getColumns(null, null, tableName, null)) {
while (columns.next()) { while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME"); String columnName = columns.getString("COLUMN_NAME");
String columnSize = columns.getString("COLUMN_SIZE"); String columnSize = columns.getString("COLUMN_SIZE");
@ -50,22 +52,20 @@ public class MetadataExtractor {
System.out.println(String.format("ColumnName: %s, columnSize: %s, datatype: %s, isColumnNullable: %s, isAutoIncrementEnabled: %s", columnName, columnSize, datatype, isNullable, isAutoIncrement)); System.out.println(String.format("ColumnName: %s, columnSize: %s, datatype: %s, isColumnNullable: %s, isAutoIncrementEnabled: %s", columnName, columnSize, datatype, isNullable, isAutoIncrement));
} }
} }
}
public void extractPrimaryKeys(String tableName) throws SQLException { public void extractPrimaryKeys(String tableName) throws SQLException {
ResultSet primaryKeys = databaseMetaData.getPrimaryKeys(null, null, tableName); try(ResultSet primaryKeys = databaseMetaData.getPrimaryKeys(null, null, tableName)) {
while (primaryKeys.next()) { while (primaryKeys.next()) {
String primaryKeyColumnName = primaryKeys.getString("COLUMN_NAME"); String primaryKeyColumnName = primaryKeys.getString("COLUMN_NAME");
String primaryKeyName = primaryKeys.getString("PK_NAME"); String primaryKeyName = primaryKeys.getString("PK_NAME");
System.out.println(String.format("columnName:%s, pkName:%s", primaryKeyColumnName, primaryKeyName)); System.out.println(String.format("columnName:%s, pkName:%s", primaryKeyColumnName, primaryKeyName));
} }
} }
public void fun() throws SQLException {
} }
public void extractForeignKeys(String tableName) throws SQLException { public void extractForeignKeys(String tableName) throws SQLException {
ResultSet foreignKeys = databaseMetaData.getImportedKeys(null, null, tableName); try(ResultSet foreignKeys = databaseMetaData.getImportedKeys(null, null, tableName)) {
while (foreignKeys.next()) { while (foreignKeys.next()) {
String pkTableName = foreignKeys.getString("PKTABLE_NAME"); String pkTableName = foreignKeys.getString("PKTABLE_NAME");
String fkTableName = foreignKeys.getString("FKTABLE_NAME"); String fkTableName = foreignKeys.getString("FKTABLE_NAME");
@ -74,6 +74,7 @@ public class MetadataExtractor {
System.out.println(String.format("pkTableName:%s, fkTableName:%s, pkColumnName:%s, fkColumnName:%s", pkTableName, fkTableName, pkColumnName, fkColumnName)); System.out.println(String.format("pkTableName:%s, fkTableName:%s, pkColumnName:%s, fkColumnName:%s", pkTableName, fkTableName, pkColumnName, fkColumnName));
} }
} }
}
public void extractDatabaseInfo() throws SQLException { public void extractDatabaseInfo() throws SQLException {
String productName = databaseMetaData.getDatabaseProductName(); String productName = databaseMetaData.getDatabaseProductName();
@ -89,13 +90,14 @@ public class MetadataExtractor {
public void extractUserName() throws SQLException { public void extractUserName() throws SQLException {
String userName = databaseMetaData.getUserName(); String userName = databaseMetaData.getUserName();
System.out.println(userName); System.out.println(userName);
ResultSet schemas = databaseMetaData.getSchemas(); try(ResultSet schemas = databaseMetaData.getSchemas()) {
while (schemas.next()) { while (schemas.next()) {
String table_schem = schemas.getString("TABLE_SCHEM"); String table_schem = schemas.getString("TABLE_SCHEM");
String table_catalog = schemas.getString("TABLE_CATALOG"); String table_catalog = schemas.getString("TABLE_CATALOG");
System.out.println(String.format("Table_schema:%s, Table_catalog:%s", table_schem, table_catalog)); System.out.println(String.format("Table_schema:%s, Table_catalog:%s", table_schem, table_catalog));
} }
} }
}
public void extractSupportedFeatures() throws SQLException { public void extractSupportedFeatures() throws SQLException {
System.out.println("Supports scrollable & Updatable Result Set: " + databaseMetaData.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)); System.out.println("Supports scrollable & Updatable Result Set: " + databaseMetaData.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE));