Merge pull request #12262 from keerthigadde/JAVA-9556

JAVA-9556 added try-with-resources for resultset
This commit is contained in:
kwoyke 2022-05-27 10:40:37 +02:00 committed by GitHub
commit d0b326355c
1 changed files with 49 additions and 47 deletions

View File

@ -14,33 +14,35 @@ public class MetadataExtractor {
}
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()) {
// Print the names of existing tables
System.out.println(resultSet.getString("TABLE_NAME"));
System.out.println(resultSet.getString("REMARKS"));
}
}
}
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()) {
// Print the names of system tables
System.out.println(resultSet.getString("TABLE_NAME"));
}
}
}
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()) {
// Print the names of existing views
System.out.println(resultSet.getString("TABLE_NAME"));
}
}
}
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()) {
String columnName = columns.getString("COLUMN_NAME");
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));
}
}
}
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()) {
String primaryKeyColumnName = primaryKeys.getString("COLUMN_NAME");
String primaryKeyName = primaryKeys.getString("PK_NAME");
System.out.println(String.format("columnName:%s, pkName:%s", primaryKeyColumnName, primaryKeyName));
}
}
public void fun() 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()) {
String pkTableName = foreignKeys.getString("PKTABLE_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));
}
}
}
public void extractDatabaseInfo() throws SQLException {
String productName = databaseMetaData.getDatabaseProductName();
@ -89,13 +90,14 @@ public class MetadataExtractor {
public void extractUserName() throws SQLException {
String userName = databaseMetaData.getUserName();
System.out.println(userName);
ResultSet schemas = databaseMetaData.getSchemas();
try(ResultSet schemas = databaseMetaData.getSchemas()) {
while (schemas.next()) {
String table_schem = schemas.getString("TABLE_SCHEM");
String table_catalog = schemas.getString("TABLE_CATALOG");
System.out.println(String.format("Table_schema:%s, Table_catalog:%s", table_schem, table_catalog));
}
}
}
public void extractSupportedFeatures() throws SQLException {
System.out.println("Supports scrollable & Updatable Result Set: " + databaseMetaData.supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE));