List of usage examples for java.sql DatabaseMetaData getDriverName
String getDriverName() throws SQLException;
From source file:org.sakaiproject.warehouse.util.db.DbLoader.java
protected String getLocalDataTypeName(String genericDataTypeName) { String localDataTypeName = null; try {/*from w w w. ja v a2s .co m*/ DatabaseMetaData dbmd = con.getMetaData(); String dbName = dbmd.getDatabaseProductName(); String dbVersion = dbmd.getDatabaseProductVersion(); String driverName = dbmd.getDriverName(); String driverVersion = dbmd.getDriverVersion(); // Check for a mapping in DbLoader.xml localDataTypeName = propertiesHandler.properties.getMappedDataTypeName(dbName, dbVersion, driverName, driverVersion, genericDataTypeName); if (localDataTypeName != null) return localDataTypeName; // Find the type code for this generic type name int dataTypeCode = getJavaSqlType(genericDataTypeName); // Find the first local type name matching the type code ResultSet rs = dbmd.getTypeInfo(); try { while (rs.next()) { int localDataTypeCode = rs.getInt("DATA_TYPE"); if (dataTypeCode == localDataTypeCode) { try { localDataTypeName = rs.getString("TYPE_NAME"); } catch (SQLException sqle) { } break; } } } finally { rs.close(); } if (localDataTypeName != null) return localDataTypeName; // No matching type found, report an error logger.error("Error in DbLoader.getLocalDataTypeName()"); logger.error("Your database driver, '" + driverName + "', version '" + driverVersion + "', was unable to find a local type name that matches the generic type name, '" + genericDataTypeName + "'."); logger.error("Please add a mapped type for database '" + dbName + "', version '" + dbVersion + "' inside your properties file and run this program again."); logger.error("Exiting..."); } catch (Exception e) { logger.error("Error in DbLoader.getLocalDataTypeName()", e); } return null; }
From source file:org.seasar.dbflute.helper.jdbc.connection.DfDataSourceHandler.java
protected void processConnectionMetaInfo(Connection conn) throws SQLException { if (_alreadySetupMeta) { return;//from w w w. j a va2 s . c o m } try { final DfConnectionMetaInfo metaInfo = new DfConnectionMetaInfo(); final DatabaseMetaData metaData = conn.getMetaData(); metaInfo.setProductName(metaData.getDatabaseProductName()); metaInfo.setProductVersion(metaData.getDatabaseProductVersion()); metaInfo.setDriverName(metaData.getDriverName()); metaInfo.setDriverVersion(metaData.getDriverVersion()); final int majorVersion = metaData.getJDBCMajorVersion(); final int minorVersion = metaData.getJDBCMinorVersion(); metaInfo.setJdbcVersion(majorVersion + "." + minorVersion); _log.info(" product = " + metaInfo.getProductDisp()); _log.info(" driver = " + metaInfo.getDriverDisp()); _connectionMetaInfo = metaInfo; } catch (SQLException continued) { _log.info("*Failed to get connection meta: " + continued.getMessage()); _connectionMetaInfo = null; } _alreadySetupMeta = true; }
From source file:org.sonar.server.platform.monitoring.DatabaseMonitor.java
private void completeDbAttributes(Map<String, Object> attributes) { try (DbSession dbSession = dbClient.openSession(false); Connection connection = dbSession.getConnection()) { DatabaseMetaData metadata = connection.getMetaData(); attributes.put("Database", metadata.getDatabaseProductName()); attributes.put("Database Version", metadata.getDatabaseProductVersion()); attributes.put("Username", metadata.getUserName()); attributes.put("URL", metadata.getURL()); attributes.put("Driver", metadata.getDriverName()); attributes.put("Driver Version", metadata.getDriverVersion()); attributes.put("Version Status", getMigrationStatus()); } catch (SQLException e) { throw new IllegalStateException("Fail to get DB metadata", e); }/*from w ww.ja v a 2s .co m*/ }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException { if (logger.isDebugEnabled()) { logger.debug("Executing SQL batch update [" + sql + "]"); }/*from ww w .jav a 2s . c o m*/ return (int[]) execute(sql, new PreparedStatementCallback() { public Object doInPreparedStatement(PreparedStatement ps) throws SQLException { int batchSize = pss.getBatchSize(); DatabaseMetaData dbmd = ps.getConnection().getMetaData(); try { boolean supportsBatchUpdates = false; try { if (dbmd != null) { if (dbmd.supportsBatchUpdates()) { if (logger.isDebugEnabled()) { logger.debug("Batch Updates supported for [" + dbmd.getDriverName() + " " + dbmd.getDriverVersion() + "]"); } supportsBatchUpdates = true; } else { if (logger.isDebugEnabled()) { logger.debug("Batch Updates are not supported for [" + dbmd.getDriverName() + " " + dbmd.getDriverVersion() + "]"); } } } } catch (AbstractMethodError ame) { logger.warn("Driver does not support JDBC 2.0 method supportsBatchUpdatres [" + dbmd.getDriverName() + " " + dbmd.getDriverVersion() + "]"); } if (supportsBatchUpdates) { for (int i = 0; i < batchSize; i++) { pss.setValues(ps, i); ps.addBatch(); } return ps.executeBatch(); } else { int[] rowsAffected = new int[batchSize]; for (int i = 0; i < batchSize; i++) { pss.setValues(ps, i); rowsAffected[i] = ps.executeUpdate(); } return rowsAffected; } } finally { if (pss instanceof ParameterDisposer) { ((ParameterDisposer) pss).cleanupParameters(); } } } }); }
From source file:org.springframework.jdbc.core.StatementCreatorUtils.java
/** * Set the specified PreparedStatement parameter to null, * respecting database-specific peculiarities. *//* ww w .jav a 2 s. c o m*/ private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName) throws SQLException { if (sqlType == SqlTypeValue.TYPE_UNKNOWN || sqlType == Types.OTHER) { boolean useSetObject = false; Integer sqlTypeToUse = null; if (!shouldIgnoreGetParameterType) { try { sqlTypeToUse = ps.getParameterMetaData().getParameterType(paramIndex); } catch (SQLException ex) { if (logger.isDebugEnabled()) { logger.debug("JDBC getParameterType call failed - using fallback method instead: " + ex); } } } if (sqlTypeToUse == null) { // Proceed with database-specific checks sqlTypeToUse = Types.NULL; DatabaseMetaData dbmd = ps.getConnection().getMetaData(); String jdbcDriverName = dbmd.getDriverName(); String databaseProductName = dbmd.getDatabaseProductName(); if (databaseProductName.startsWith("Informix") || (jdbcDriverName.startsWith("Microsoft") && jdbcDriverName.contains("SQL Server"))) { // "Microsoft SQL Server JDBC Driver 3.0" versus "Microsoft JDBC Driver 4.0 for SQL Server" useSetObject = true; } else if (databaseProductName.startsWith("DB2") || jdbcDriverName.startsWith("jConnect") || jdbcDriverName.startsWith("SQLServer") || jdbcDriverName.startsWith("Apache Derby")) { sqlTypeToUse = Types.VARCHAR; } } if (useSetObject) { ps.setObject(paramIndex, null); } else { ps.setNull(paramIndex, sqlTypeToUse); } } else if (typeName != null) { ps.setNull(paramIndex, sqlType, typeName); } else { ps.setNull(paramIndex, sqlType); } }
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/* www .ja v a 2s . c o 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.cwm.db.connection.ConnectionUtils.java
/** * DOC xqliu Comment method "isOdbcMssql". bug 9822 * //from w w w. j a v a2 s .c o m * @param connection * @return * @throws SQLException */ public static boolean isOdbcMssql(java.sql.Connection connection) throws SQLException { @SuppressWarnings("deprecation") DatabaseMetaData connectionMetadata = org.talend.utils.sql.ConnectionUtils .getConnectionMetadata(connection); if (connectionMetadata.getDriverName() != null && connectionMetadata.getDriverName().toLowerCase().startsWith(DatabaseConstant.ODBC_DRIVER_NAME) && connectionMetadata.getDatabaseProductName() != null && connectionMetadata.getDatabaseProductName().equals(DatabaseConstant.ODBC_MSSQL_PRODUCT_NAME)) { return true; } return false; }
From source file:org.talend.cwm.db.connection.ConnectionUtils.java
public static boolean isOdbcProgress(java.sql.Connection connection) throws SQLException { @SuppressWarnings("deprecation") DatabaseMetaData connectionMetadata = org.talend.utils.sql.ConnectionUtils .getConnectionMetadata(connection); if (connectionMetadata.getDriverName() != null && connectionMetadata.getDriverName().toLowerCase().startsWith(DatabaseConstant.ODBC_DRIVER_NAME) && connectionMetadata.getDatabaseProductName() != null && connectionMetadata.getDatabaseProductName().toLowerCase() .indexOf(DatabaseConstant.ODBC_PROGRESS_PRODUCT_NAME) > -1) { return true; }/*ww w . j a v a 2 s. c om*/ return false; }
From source file:org.talend.cwm.db.connection.ConnectionUtils.java
/** * DOC zshen Comment method "isMssql"./*w ww. java 2s .c om*/ * * @param connection * @return decide to whether is mssql connection * @throws SQLException */ public static boolean isMssql(java.sql.Connection connection) throws SQLException { @SuppressWarnings("deprecation") DatabaseMetaData connectionMetadata = org.talend.utils.sql.ConnectionUtils .getConnectionMetadata(connection); if (connectionMetadata.getDriverName() != null && !connectionMetadata.getDriverName().toLowerCase().startsWith(DatabaseConstant.ODBC_DRIVER_NAME) && connectionMetadata.getDatabaseProductName() != null && connectionMetadata.getDatabaseProductName().equals(DatabaseConstant.ODBC_MSSQL_PRODUCT_NAME)) { return true; } return false; }
From source file:org.talend.cwm.db.connection.ConnectionUtils.java
/** * DOC hwang Comment method "isMysql"./*from w w w . ja va2s. c o m*/ * * @param connection * @return * @throws SQLException */ public static boolean isMysql(java.sql.Connection connection) throws SQLException { @SuppressWarnings("deprecation") DatabaseMetaData connectionMetadata = org.talend.utils.sql.ConnectionUtils .getConnectionMetadata(connection); if (connectionMetadata.getDriverName() != null && connectionMetadata.getDriverName().toLowerCase().startsWith(DatabaseConstant.MYSQL_PRODUCT_NAME) && connectionMetadata.getDatabaseProductName() != null && connectionMetadata .getDatabaseProductName().toLowerCase().indexOf(DatabaseConstant.MYSQL_PRODUCT_NAME) > -1) { return true; } return false; }