List of usage examples for java.sql DatabaseMetaData getConnection
Connection getConnection() throws SQLException;
From source file:org.talend.core.model.metadata.DBConnectionFillerImplTest.java
/** * Test filling catalogs for odbc teredata . * {@link org.talend.metadata.managment.model.DBConnectionFillerImpl#fillCatalogs(Connection, DatabaseMetaData, IMetadataConnection, List) * @throws SQLException/*from w ww.j av a2s. co m*/ */ @Test public void testFillCatalogs_AS400() throws SQLException { // mock ReturnCode sql.Connection java.sql.Connection mockSqlConn = Mockito.mock(java.sql.Connection.class); Mockito.when(mockSqlConn.getCatalog()).thenReturn("tbi"); //$NON-NLS-1$ // ~mock // mock ResultSet ResultSet mockCatalogResults = Mockito.mock(ResultSet.class); Mockito.when(mockCatalogResults.next()).thenReturn(true, false); Mockito.when(mockCatalogResults.getString(MetaDataConstants.TABLE_CAT.name())).thenReturn("tbi"); //$NON-NLS-1$ ResultSet mockSchemaResults = Mockito.mock(ResultSet.class); Mockito.when(mockSchemaResults.next()).thenReturn(true, false); Mockito.when(mockSchemaResults.getString(MetaDataConstants.TABLE_SCHEM.name())).thenReturn("dbo"); //$NON-NLS-1$ // ~Result // mock JDBC Metadata DatabaseMetaData dbJDBCMetadata = mock(DatabaseMetaData.class); Mockito.when(dbJDBCMetadata.getDatabaseProductName()).thenReturn(EDatabaseTypeName.AS400.getProduct()); Mockito.when(dbJDBCMetadata.getDriverName()).thenReturn("com.ibm.as400.access.AS400JDBCDriver"); //$NON-NLS-1$ Mockito.when(dbJDBCMetadata.getCatalogs()).thenReturn(mockCatalogResults); Mockito.when(dbJDBCMetadata.getConnection()).thenReturn(mockSqlConn); Mockito.when(dbJDBCMetadata.getSchemas()).thenReturn(mockSchemaResults); // ~JDBC Metadata // stub(method(ConnectionUtils.class, "isOdbcTeradata", DatabaseMetaData.class)).toReturn(true); //$NON-NLS-1$ // mock DatabaseConnection List<String> catalogFilter = new ArrayList<String>(); DatabaseConnection dbConnection = mock(DatabaseConnection.class); Mockito.when(dbConnection.getSID()).thenReturn(""); //$NON-NLS-1$ Mockito.when(dbConnection.getDatabaseType()).thenReturn(EDatabaseTypeName.AS400.getDisplayName()); Mockito.when(dbConnection.getUiSchema()).thenReturn(""); //$NON-NLS-1$ // ~DatabaseConnection // mock MetadataConnection IMetadataConnection metadaConnection = Mockito.mock(MetadataConnection.class); Mockito.when(metadaConnection.getDatabase()).thenReturn(""); //$NON-NLS-1$ // ~MetadataConnection // mock ConvertionHelper PowerMockito.mockStatic(ConvertionHelper.class); Mockito.when(ConvertionHelper.convert(dbConnection)).thenReturn(metadaConnection); // Mockito.when(ExtractMetaDataUtils.getDatabaseMetaData(mockSqlConn, EDatabaseTypeName.IBMDB2ZOS.getXmlName(), // false, "")) // .thenCallRealMethod(); // ~ConvertionHelper // mock ConnectionHelper PowerMockito.mockStatic(ConnectionHelper.class); Mockito.when(ConnectionHelper.getTables(dbConnection)).thenReturn(new HashSet<MetadataTable>()); // Mockito.when(ExtractMetaDataUtils.getDatabaseMetaData(mockSqlConn, EDatabaseTypeName.IBMDB2ZOS.getXmlName(), // false, "")) // .thenCallRealMethod(); // ~ConnectionHelper when(dbConnection.isContextMode()).thenReturn(false); List<Catalog> fillCatalogs = this.dBConnectionFillerImpl.fillCatalogs(dbConnection, dbJDBCMetadata, null, catalogFilter); assertTrue(fillCatalogs.size() == 1); assertTrue("tbi".equals(fillCatalogs.get(0).getName())); List<Schema> schemas = CatalogHelper.getSchemas(fillCatalogs.get(0)); assertTrue(schemas.size() == 1); assertTrue("dbo".equals(schemas.get(0).getName())); }
From source file:org.talend.core.model.metadata.DBConnectionFillerImplTest.java
private void initializeForFillTables(orgomg.cwm.objectmodel.core.Package pack, DatabaseMetaData dbmd, String[] tableType, boolean isOracle) throws SQLException { when(pack.getName()).thenReturn("tdqPackage");//$NON-NLS-1$ PowerMockito.mockStatic(PackageHelper.class); when(PackageHelper.getParentPackage(pack)).thenReturn(pack); when(PackageHelper.getCatalogOrSchema(pack)).thenReturn(pack); Connection con = mock(Connection.class); PowerMockito.mockStatic(MetadataConnectionUtils.class); when(MetadataConnectionUtils.isAS400(pack)).thenReturn(false); when(MetadataConnectionUtils.isOracle(con)).thenReturn(isOracle); when(MetadataConnectionUtils.isSybase(dbmd)).thenReturn(false); when(MetadataConnectionUtils.isOracle8i(con)).thenReturn(false); when(MetadataConnectionUtils.isOracleJDBC(con)).thenReturn(isOracle); PowerMockito.mockStatic(ConnectionHelper.class); when(ConnectionHelper.getConnection(pack)).thenReturn(con); List<String> filterNames = new ArrayList<String>(); filterNames.add("Table1");//$NON-NLS-1$ if (isOracle) { java.sql.Connection sqlConn = mock(java.sql.Connection.class); when(dbmd.getConnection()).thenReturn(sqlConn); Statement stme = mock(Statement.class); ResultSet rsTables = mock(ResultSet.class); when(sqlConn.createStatement()).thenReturn(stme); when(stme.executeQuery(TableInfoParameters.ORACLE_10G_RECBIN_SQL)).thenReturn(rsTables); stub(method(ExtractMetaDataFromDataBase.class, "getTableNamesFromQuery")).toReturn(filterNames);//$NON-NLS-1$ stub(method(ExtensionImplementationProvider.class, "getInstanceV2", IExtensionPointLimiter.class)) //$NON-NLS-1$ .toReturn(new ArrayList<IConfigurationElement>()); }// w ww. jav a 2 s . com ResultSet rs = mock(ResultSet.class); when(rs.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(rs.getString(GetTable.TABLE_NAME.name())).thenReturn("Table1").thenReturn("Table2");//$NON-NLS-1$ //$NON-NLS-2$ when(rs.getString(GetTable.TABLE_TYPE.name())).thenReturn("Table");//$NON-NLS-1$ when(rs.getString(GetTable.REMARKS.name())).thenReturn("");//$NON-NLS-1$ when(dbmd.getTables("tdqPackage", "tdqPackage", "", tableType)).thenReturn(rs);//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ when(dbmd.getTables("tdqPackage", null, "", tableType)).thenReturn(rs);//$NON-NLS-1$//$NON-NLS-2$ stub(method(StringUtils.class, "isBlank")).toReturn(false);//$NON-NLS-1$ ProxyRepositoryFactory proxFactory = mock(ProxyRepositoryFactory.class); when(proxFactory.getNextId()).thenReturn("abcd1").thenReturn("abcd2");//$NON-NLS-1$//$NON-NLS-2$ stub(method(ProxyRepositoryFactory.class, "getInstance")).toReturn(proxFactory);//$NON-NLS-1$ }
From source file:org.talend.metadata.managment.model.DBConnectionFillerImpl.java
protected String getSchemaName(ResultSet schemaRs, DatabaseMetaData dbJDBCMetadata, Catalog catalog) { String schemaName = null;/* w ww.j ava 2 s .c o m*/ String catalogName = null; try { schemaName = schemaRs.getString(MetaDataConstants.TABLE_SCHEM.name()); // MOD klliu bug 19004 2011-03-31 if (!(MetadataConnectionUtils.isPostgresql(dbJDBCMetadata) || MetadataConnectionUtils.isSybase(dbJDBCMetadata))) { catalogName = schemaRs.getString(MetaDataConstants.TABLE_CATALOG.name()); } // the case for mssql if (MetadataConnectionUtils.isMssql(dbJDBCMetadata) && dbJDBCMetadata.getDatabaseMajorVersion() > 8 && dbJDBCMetadata.getDriverMajorVersion() > 1) { if (catalogName != null && catalogName != schemaName) { schemaName = catalogName; } } if (!MetadataConnectionUtils.isMssql(dbJDBCMetadata.getConnection()) && catalogName != null && !catalogName.equals(catalog.getName())) { return null; } } catch (Exception e) { // not some things need to do } return schemaName; }
From source file:org.talend.metadata.managment.model.DBConnectionFillerImpl.java
@Override public List<MetadataTable> fillAll(Package pack, DatabaseMetaData dbJDBCMetadata, IMetadataConnection metaConnection, List<String> tableFilter, String tablePattern, String[] tableType) { List<MetadataTable> list = new ArrayList<MetadataTable>(); if (dbJDBCMetadata == null) { return null; }//from w w w . j a va2 s . com ExtractMetaDataUtils extractMeta = ExtractMetaDataUtils.getInstance(); Package catalogOrSchema = PackageHelper.getCatalogOrSchema(pack); String catalogName = null; String schemaPattern = null; if (catalogOrSchema != null) { // catalog if (catalogOrSchema instanceof Catalog) { catalogName = catalogOrSchema.getName(); } else {// schema Package parentCatalog = PackageHelper.getParentPackage(catalogOrSchema); // in the fillSchema, we set one default schema with " ", but this one doesn't exist, so we should // replace to get the tables from all schemas instead schemaPattern = " ".equals(catalogOrSchema.getName()) ? null : catalogOrSchema.getName(); //$NON-NLS-1$ catalogName = parentCatalog == null ? null : parentCatalog.getName(); } } try { // common boolean isOracle8i = true; boolean isOracle = false; boolean isOracleJdbc = false; String tableComment = null; List<String> tablesToFilter = new ArrayList<String>(); if (pack != null) { Connection c = ConnectionHelper.getConnection(pack); isOracle8i = MetadataConnectionUtils.isOracle8i(c); isOracle = MetadataConnectionUtils.isOracle(c); isOracleJdbc = MetadataConnectionUtils.isOracleJDBC(c); if ((isOracleJdbc || isOracle) && !isOracle8i) {// oracle and not oracle8 Statement stmt; try { // MOD qiongli TDQ-4732 use the common method to create statement both DI and DQ,avoid Exception // for top. stmt = dbJDBCMetadata.getConnection().createStatement(); ResultSet rsTables = stmt.executeQuery(TableInfoParameters.ORACLE_10G_RECBIN_SQL); tablesToFilter = ExtractMetaDataFromDataBase.getTableNamesFromQuery(rsTables, dbJDBCMetadata.getConnection()); rsTables.close(); stmt.close(); } catch (SQLException e) { ExceptionHandler.process(e); } } } boolean isHive2 = HiveConnectionManager.getInstance().isHive2(metaConnection); if (isHive2) { // for CDH4 HIVE2 , the table type are MANAGED_TABLE and EXTERNAL_TABLE ...... // tableType = null; } ResultSet tables = dbJDBCMetadata.getTables(catalogName, schemaPattern, tablePattern, tableType); while (tables.next()) { String coloumnName = GetTable.TABLE_SCHEM.name(); if (schemaPattern != null) { try { tables.getString(coloumnName); } catch (Exception e) { coloumnName = GetTable.TABLE_SCHEMA.name(); } } String tableName = getStringFromResultSet(tables, GetTable.TABLE_NAME.name()); String temptableType = getStringFromResultSet(tables, GetTable.TABLE_TYPE.name()); // for special db. teradata_sql_model/db2_zos/as400 temptableType = convertSpecialTableType(temptableType); // for if (!isCreateElement(tableFilter, tableName)) { continue; } if (tableName == null || tablesToFilter.contains(tableName)) { continue; } // if (!isOracle && !isOracle8i && !isOracleJdbc && tableName.startsWith("/")) { //$NON-NLS-1$ // continue; // } if (!isOracle8i) { tableComment = getTableComment(dbJDBCMetadata, tables, tableName, catalogName, schemaPattern); } MetadataTable metadatatable = null; if (TableType.VIEW.toString().equals(temptableType) || ETableTypes.VIRTUAL_VIEW.getName().equals(temptableType)) { metadatatable = RelationalFactory.eINSTANCE.createTdView(); } else { metadatatable = RelationalFactory.eINSTANCE.createTdTable(); } metadatatable.setName(tableName); // Added by Marvin Wang on Feb. 6, 2012 for bug TDI-24413, it is just for hive external table. if (ETableTypes.TABLETYPE_EXTERNAL_TABLE.getName().equals(temptableType) || ETableTypes.EXTERNAL_TABLE.getName().equals(temptableType) || ETableTypes.MANAGED_TABLE.getName().equals(temptableType) || ETableTypes.INDEX_TABLE.getName().equals(temptableType)) { metadatatable.setTableType(ETableTypes.TABLETYPE_TABLE.getName()); } else if (ETableTypes.VIRTUAL_VIEW.getName().equals(temptableType)) { metadatatable.setTableType(ETableTypes.TABLETYPE_VIEW.getName()); } else { metadatatable.setTableType(temptableType); } metadatatable.setLabel(metadatatable.getName()); if (tableComment != null) { metadatatable.setComment(tableComment); ColumnSetHelper.setComment(tableComment, metadatatable); } try { if (tables.getString("SYSTEM_TABLE_NAME") != null //$NON-NLS-1$ && tables.getString("SYSTEM_TABLE_SCHEMA") != null //$NON-NLS-1$ && tables.getString("TABLE_SCHEMA") != null) { //$NON-NLS-1$ TaggedValueHelper.setTaggedValue(metadatatable, TaggedValueHelper.SYSTEMTABLENAME, tables.getString("SYSTEM_TABLE_NAME").trim()); //$NON-NLS-1$ TaggedValueHelper.setTaggedValue(metadatatable, TaggedValueHelper.SYSTEMTABLESCHEMA, tables.getString("SYSTEM_TABLE_SCHEMA").trim()); //$NON-NLS-1$ TaggedValueHelper.setTaggedValue(metadatatable, TaggedValueHelper.TABLESCHEMA, tables.getString("TABLE_SCHEMA").trim()); //$NON-NLS-1$ } } catch (SQLException e) { // don't catch anything if the system table name or schema doesn't exist // this part is needed only for as400 } list.add(metadatatable); } if (dbJDBCMetadata.getDatabaseProductName() != null && dbJDBCMetadata.getDatabaseProductName().equals("Microsoft SQL Server")) { //$NON-NLS-1$ for (String element : tableType) { if (element.equals("SYNONYM")) { //$NON-NLS-1$ Statement stmt = extractMeta.getConn().createStatement(); extractMeta.setQueryStatementTimeout(stmt); String schemaname = schemaPattern + ".sysobjects"; //$NON-NLS-1$ String sql = "select name from " + schemaname + " where xtype='SN'"; //$NON-NLS-1$//$NON-NLS-2$ if ("dbo".equalsIgnoreCase(schemaPattern)) { //$NON-NLS-1$ // SELECT name AS object_name ,SCHEMA_NAME(schema_id) AS schema_name FROM sys.objects where // type='SN' ResultSet rsTables = stmt.executeQuery(sql); while (rsTables.next()) { String nameKey = rsTables.getString("name").trim(); //$NON-NLS-1$ MetadataTable metadatatable = null; metadatatable = RelationalFactory.eINSTANCE.createTdTable(); metadatatable.setName(nameKey); metadatatable.setTableType(ETableTypes.TABLETYPE_SYNONYM.getName()); metadatatable.setLabel(metadatatable.getName()); list.add(metadatatable); } } } } } else if (dbJDBCMetadata.getDatabaseProductName() != null && dbJDBCMetadata.getDatabaseProductName().startsWith("DB2/")) { //$NON-NLS-1$ for (String element : tableType) { if (element.equals("SYNONYM")) { //$NON-NLS-1$ Statement stmt = extractMeta.getConn().createStatement(); extractMeta.setQueryStatementTimeout(stmt); String sql = "SELECT NAME FROM SYSIBM.SYSTABLES where TYPE='A' and BASE_SCHEMA = '" //$NON-NLS-1$ + schemaPattern + "'"; //$NON-NLS-1$ ResultSet rsTables = stmt.executeQuery(sql); while (rsTables.next()) { String nameKey = rsTables.getString("NAME").trim(); //$NON-NLS-1$ MetadataTable metadatatable = null; metadatatable = RelationalFactory.eINSTANCE.createTdTable(); metadatatable.setName(nameKey); metadatatable.setTableType(ETableTypes.TABLETYPE_SYNONYM.getName()); metadatatable.setLabel(metadatatable.getName()); list.add(metadatatable); } } } } if (isLinked()) { PackageHelper.addMetadataTable(ListUtils.castList(MetadataTable.class, list), pack); } } catch (SQLException e) { log.error(e, e); } return list; }
From source file:org.talend.metadata.managment.model.DBConnectionFillerImpl.java
/** * get Table Comment.//from w w w . j a va 2 s .c o m * * @param dbJDBCMetadata * @param tables * @param tableName * @param catalogName * @param schemaPattern * @return */ private String getTableComment(DatabaseMetaData dbJDBCMetadata, ResultSet tables, String tableName, String catalogName, String schemaPattern) { String tableComment = getStringFromResultSet(tables, GetTable.REMARKS.name()); try { String productName = dbJDBCMetadata.getDatabaseProductName(); if (StringUtils.isBlank(tableComment)) { String selectRemarkOnTable = MetadataConnectionUtils.getCommentQueryStr(productName, tableName, catalogName, schemaPattern); if (selectRemarkOnTable != null) { tableComment = executeGetCommentStatement(selectRemarkOnTable, dbJDBCMetadata.getConnection()); } } } catch (SQLException e) { log.error(e, e); } return tableComment; }
From source file:org.talend.metadata.managment.model.DBConnectionFillerImpl.java
/** * get the Column Comment especially for oracle type. * /*from www . j a va 2 s. co m*/ * @param dbJDBCMetadata * @param columns * @param tableName * @param columnName * @param schemaPattern * @return */ private String getColumnComment(DatabaseMetaData dbJDBCMetadata, ResultSet columns, String tableName, String columnName, String schemaPattern) { String columnComment = getStringFromResultSet(columns, GetColumn.REMARKS.name()); try { if (StringUtils.isBlank(columnComment) && MetadataConnectionUtils.isOracle(dbJDBCMetadata)) { String selectRemarkOnTable = "SELECT COMMENTS FROM ALL_COL_COMMENTS WHERE TABLE_NAME='" + tableName //$NON-NLS-1$ + "' AND OWNER='" + schemaPattern.toUpperCase() + "' AND COLUMN_NAME='" + columnName + "'"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ columnComment = executeGetCommentStatement(selectRemarkOnTable, dbJDBCMetadata.getConnection()); } } catch (SQLException e) { log.error(e, e); } if (columnComment == null) { columnComment = "";//$NON-NLS-1$ } return columnComment; }
From source file:org.talend.metadata.managment.model.DBConnectionFillerImpl.java
@Override public List<TdTable> fillTables(Package pack, DatabaseMetaData dbJDBCMetadata, List<String> tableFilter, String tablePattern, String[] tableType) { List<TdTable> tableList = new ArrayList<TdTable>(); if (dbJDBCMetadata == null) { return null; }/* w w w. java2 s . c o m*/ Package catalogOrSchema = PackageHelper.getCatalogOrSchema(pack); String catalogName = null; String schemaPattern = null; if (catalogOrSchema != null) { // catalog if (catalogOrSchema instanceof Catalog) { catalogName = catalogOrSchema.getName(); if (MetadataConnectionUtils.isAS400(catalogOrSchema)) { return tableList; } } else {// schema Package parentCatalog = PackageHelper.getParentPackage(catalogOrSchema); schemaPattern = catalogOrSchema.getName(); catalogName = parentCatalog == null ? null : parentCatalog.getName(); } } try { // common boolean flag = true; String tableComment = null; List<String> tablesToFilter = new ArrayList<String>(); if (pack != null) { Connection c = ConnectionHelper.getConnection(pack); flag = MetadataConnectionUtils.isOracle8i(c); boolean isOracle = MetadataConnectionUtils.isOracle(c); boolean isOracleJdbc = MetadataConnectionUtils.isOracleJDBC(c); // MetadataConnectionUtils.isOracle8i(connection) if ((isOracle || isOracleJdbc) && !flag) {// oracle and not oracle8 Statement stmt; try { // MOD qiongli TDQ-4732 use the common method to create statement both DI and DQ,avoid Exception // for top. stmt = dbJDBCMetadata.getConnection().createStatement(); ResultSet rsTables = stmt.executeQuery(TableInfoParameters.ORACLE_10G_RECBIN_SQL); tablesToFilter = ExtractMetaDataFromDataBase.getTableNamesFromQuery(rsTables, dbJDBCMetadata.getConnection()); rsTables.close(); stmt.close(); } catch (SQLException e) { ExceptionHandler.process(e); } } } ResultSet tables = dbJDBCMetadata.getTables(catalogName, schemaPattern, tablePattern, tableType); while (tables.next()) { String tableName = getStringFromResultSet(tables, GetTable.TABLE_NAME.name()); String temptableType = getStringFromResultSet(tables, GetTable.TABLE_TYPE.name()); // for special db. teradata_sql_model/db2_zos/as400 temptableType = convertSpecialTableType(temptableType); // if TableType is view type don't create it at here. if (TableType.VIEW.toString().equals(temptableType)) { continue; } // for if (!isCreateElement(tableFilter, tableName)) { continue; } if (tableName == null || tablesToFilter.contains(tableName) || tableName.startsWith("/")) { //$NON-NLS-1$ continue; } if (!flag) { tableComment = getTableComment(dbJDBCMetadata, tables, tableName, catalogName, schemaPattern); } // create table TdTable table = RelationalFactory.eINSTANCE.createTdTable(); table.setName(tableName); table.setTableType(temptableType); table.setLabel(table.getName()); // MOD qiongli 2011-11-30 TDQ-3930.set id for this retrive table. table.setId(EcoreUtil.generateUUID()); if (tableComment != null) { ColumnSetHelper.setComment(tableComment, table); } tableList.add(table); } if (isLinked()) { PackageHelper.addMetadataTable(ListUtils.castList(MetadataTable.class, tableList), pack); } } catch (SQLException e) { log.error(e, e); } return tableList; }
From source file:org.talend.metadata.managment.utils.MetadataConnectionUtils.java
/** * zshen Comment method "isODBCCatalog". * // w w w .jav a2 s .com * @param catalogName the name for need to be decided. * @param connection * @return if connection is a ODBC connection and catalogName isn't which be found then return ture, else return * true. * @throws SQLException */ public static boolean isODBCCatalog(String catalogName, DatabaseMetaData connectionMetadata) throws SQLException { if (isOdbcConnection(connectionMetadata)) { String userCatalogName = connectionMetadata.getConnection().getCatalog(); if (catalogName != null && (catalogName.equals(userCatalogName) || userCatalogName == null || "null".equals(userCatalogName))) { //$NON-NLS-1$ return true; } else { return false; } } return true; }
From source file:org.xenei.bloomgraph.bloom.sql.MySQLCommands.java
/** * Create any necessary procedures./*from w w w .ja v a 2 s. co m*/ * * @param metadata * the metadata for the database. * @throws SQLException * on error */ private void createProcedures(final DatabaseMetaData metadata) throws SQLException { ResultSet rs = null; Statement stmt = null; final Connection connection = metadata.getConnection(); try { rs = metadata.getProcedures(connection.getCatalog(), connection.getSchema(), "add_triple"); if (!rs.next()) { stmt = connection.createStatement(); stmt.executeUpdate(CREATE_ADD_TRIPLE_PROCEDURE); } } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); } }
From source file:org.xenei.bloomgraph.bloom.sql.MySQLCommands.java
/** * Create the page index table./* w w w . j av a 2s.c om*/ * * @param metadata * the metadata for the database. * @throws SQLException */ private void createPageIndexTable(final DatabaseMetaData metadata) throws SQLException { ResultSet rs = null; Statement stmt = null; final Connection connection = metadata.getConnection(); try { rs = metadata.getTables(connection.getCatalog(), connection.getSchema(), getPageIndexTableName(), new String[] { "TABLE" }); if (!rs.next()) { // table does not exist stmt = connection.createStatement(); stmt.executeUpdate(CREAT_PAGE_INDEX_TABLE_FMT); String stmtStr = String.format(CREATE_PAGE_INDX_TRIGGER, "INSERT"); stmt.executeUpdate(stmtStr); stmtStr = String.format(CREATE_PAGE_INDX_TRIGGER, "UPDATE"); stmt.executeUpdate(stmtStr); } } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); } }