Java Code Examples for java.sql.DatabaseMetaData#getDriverName()

The following examples show how to use java.sql.DatabaseMetaData#getDriverName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KingbaseDictionary.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public void connectedConfiguration(Connection conn) throws SQLException {
	super.connectedConfiguration(conn);
	boolean requiresWarnings = true;
	DatabaseMetaData meta = conn.getMetaData();
	String driverName = meta.getDriverName();
	String url = meta.getURL();
	if (this.driverVendor == null) {
		if ((driverName != null) && (driverName.equalsIgnoreCase("com.kingbase.Driver"))) {
			this.driverVendor = "KingbaseJdbcDriver";
			if ((url != null) && (url.startsWith("jdbc:kingbase://"))) {
				requiresWarnings = false;
			}
		} else {
			this.driverVendor = "other";
		}
	}
	if (("KingbaseJdbcDriver".equalsIgnoreCase(this.driverVendor)) && (requiresWarnings)) {
		this.log.warn(_loc.get("kingbase Jdbc connection", url));
	}
}
 
Example 2
Source File: DMDictionary.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public void connectedConfiguration(Connection conn) throws SQLException {
	super.connectedConfiguration(conn);
	boolean requiresWarnings = true;
	DatabaseMetaData meta = conn.getMetaData();
	String driverName = meta.getDriverName();
	String url = meta.getURL();
	if (this.driverVendor == null) {
		if ((driverName != null) && (driverName.equalsIgnoreCase("dm.jdbc.driver.DmDriver"))) {
			this.driverVendor = "Dm7JdbcDriver";
			if ((url != null) && (url.startsWith("jdbc:dm://"))) {
				requiresWarnings = false;
			}
		} else {
			this.driverVendor = "other";
		}
	}
	if (("Dm7JdbcDriver".equalsIgnoreCase(this.driverVendor)) && (requiresWarnings)) {
		this.log.warn(_loc.get("Dm Jdbc connection", url));
	}
}
 
Example 3
Source File: Kingbase8DictionaryBack.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
public void connectedConfiguration(Connection conn) throws SQLException {
	super.connectedConfiguration(conn);
	boolean requiresWarnings = true;
	DatabaseMetaData meta = conn.getMetaData();
	String driverName = meta.getDriverName();
	String url = meta.getURL();
	if (this.driverVendor == null) {
		if ((driverName != null) && (driverName.equalsIgnoreCase("com.kingbase8.Driver"))) {
			this.driverVendor = "Kingbase8JdbcDriver";
			if ((url != null) && (url.startsWith("jdbc:kingbase8://"))) {
				requiresWarnings = false;
			}
		} else {
			this.driverVendor = "other";
		}
	}
	if (("Kingbase8JdbcDriver".equalsIgnoreCase(this.driverVendor)) && (requiresWarnings)) {
		this.log.warn(_loc.get("kingbase8 Jdbc connection", url));
	}
}
 
Example 4
Source File: JDBCConnMetadataModel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static JDBCMetadata createMetadata(Connection conn, String defaultSchemaName) {
    try {
        DatabaseMetaData dmd = conn.getMetaData();
        if ("Oracle".equals(dmd.getDatabaseProductName())) { // NOI18N
            return new OracleMetadata(conn, defaultSchemaName);
        }

        if ("mysql".equalsIgnoreCase(dmd.getDatabaseProductName())) { // NOI18N
            return new MySQLMetadata(conn, defaultSchemaName);
        }
        
        String driverName = dmd.getDriverName();
        if (driverName != null) {
            if ((driverName.contains("Microsoft") && driverName.contains("SQL Server")) //NOI18N
                    || driverName.contains("jTDS")) { //NOI18N
                return new MSSQLMetadata(conn, defaultSchemaName);
            }
        }
    } catch (SQLException e) {
        LOGGER.log(Level.INFO, null, e);
    }
    return new JDBCMetadata(conn, defaultSchemaName);
}
 
Example 5
Source File: JdbcFacade.java    From iaf with Apache License 2.0 6 votes vote down vote up
public String getDatasourceInfo() throws JdbcException {
	String dsinfo=null;
	try (Connection conn=getConnection()) {
		DatabaseMetaData md=conn.getMetaData();
		String product=md.getDatabaseProductName();
		String productVersion=md.getDatabaseProductVersion();
		String driver=md.getDriverName();
		String driverVersion=md.getDriverVersion();
		String url=md.getURL();
		String user=md.getUserName();
		if (getDatabaseType() == DbmsSupportFactory.DBMS_DB2 && "WAS".equals(IbisContext.getApplicationServerType()) && md.getResultSetHoldability() != ResultSet.HOLD_CURSORS_OVER_COMMIT) {
			// For (some?) combinations of WebShere and DB2 this seems to be
			// the default and result in the following exception when (for
			// example?) a ResultSetIteratingPipe is calling next() on the
			// ResultSet after it's sender has called a pipeline which
			// contains a GenericMessageSendingPipe using
			// transactionAttribute="NotSupported":
			//   com.ibm.websphere.ce.cm.ObjectClosedException: DSRA9110E: ResultSet is closed.
			ConfigurationWarnings.add(this, log, "The database's default holdability for ResultSet objects is " + md.getResultSetHoldability() + " instead of " + ResultSet.HOLD_CURSORS_OVER_COMMIT + " (ResultSet.HOLD_CURSORS_OVER_COMMIT)");
		}
		dsinfo ="user ["+user+"] url ["+url+"] product ["+product+"] version ["+productVersion+"] driver ["+driver+"] version ["+driverVersion+"]";
	} catch (SQLException e) {
		log.warn("Exception determining databaseinfo",e);
	}
	return dsinfo;
}
 
Example 6
Source File: MysqlPageDialect.java    From oxygen with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supported(DatabaseMetaData meta) throws SQLException {
  switch (meta.getDatabaseProductName()) {
    case "MySQL":
    case "CUBRID":
      return true;
    default:
  }
  String driver = meta.getDriverName();
  if (driver == null) {
    return false;
  }
  return driver.startsWith("MariaDB");
}
 
Example 7
Source File: DbSource.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the database platform type from the ResultSet
 * @param resultSet the result set
 * @return          the database type
 */
private SQLPlatform getPlatform(ResultSet resultSet) {
    try {
        final DatabaseMetaData metaData = resultSet.getStatement().getConnection().getMetaData();
        final String driverClassName = metaData.getDriverName();
        return SQLPlatform.getPlatform(driverClassName);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to detect database platform type, please use withPlatform() on request", ex);
    }
}
 
Example 8
Source File: DBInformation.java    From cloud-spring-boot-sample with Apache License 2.0 5 votes vote down vote up
public DBInformation(DatabaseMetaData metaData) throws SQLException
{
	url = metaData.getURL();
	dbName = metaData.getDatabaseProductName();
	dbMajorVersion = metaData.getDatabaseMajorVersion();
	dbMinorVersion = metaData.getDatabaseMinorVersion();

	driverName = metaData.getDriverName();
	driverVersion = metaData.getDriverVersion();

	userName = metaData.getUserName();
}
 
Example 9
Source File: DbConn.java    From XBatis-Code-Generator with Apache License 2.0 5 votes vote down vote up
private DatabaseMetaData printDatabaseMetaData() {
	DatabaseMetaData dbmd = null;
	ResultSet rs = null;
	try {
		dbmd = conn.getMetaData();
		/*
		 * 获取当前数据库的数据类型信息。返回18列数据,如下所示
		 */
		rs = dbmd.getTypeInfo();
		logger.info("===============================数据库支持的数据类型:");
		while (rs.next()) {
			logger.info("类型名称【"
					+ StringUtil.genLengthStr(rs.getString(1), 20)
					+ "】SqlType【"
					+ StringUtil.genLengthStr(rs.getString(2), 5)
					+ "】最大精度【"
					+ StringUtil.genLengthStr(rs.getString(3), 10) + "】");
		}
		/* 获取数据库信息 */
		String dbType = dbmd.getDatabaseProductName(); // 获取当前数据库是什么数据库。如mysql等。返回的是字符串。
		String dbVersion = dbmd.getDatabaseProductVersion(); // 获得数据库的版本。返回的字符串。
		String driverName = dbmd.getDriverName(); // 获得驱动程序的名称。返回字符串。
		String driverVersion = dbmd.getDriverVersion(); // 获得驱动程序的版本。返回字符串。
		logger.info("数据库类型【" + dbType + "】数据库版本【" + dbVersion + "】数据库驱动名称【"
				+ driverName + "】数据库驱动程序版本【" + driverVersion + "】");

	} catch (SQLException e) {
		logger.error("获取DataBaseMetaData元数据出错", e);
	}
	return dbmd;
}
 
Example 10
Source File: Constants.java    From DKO with GNU Lesser General Public License v2.1 5 votes vote down vote up
static DB_TYPE detect(final Connection conn) throws SQLException {

			if (conn instanceof UnClosableConnection) {
				return detect(((UnClosableConnection)conn).getUnderlyingConnection());
			}

			// try from the class name
			final String className = conn.getClass().getName();
			if (className.contains("SQLServer")) return SQLSERVER;
			if (className.contains("SQLDroidConnection")) return SQLITE3;
			if (className.contains("SQLiteJDBC")) return SQLITE3;
			if (className.startsWith("oracle")) return ORACLE;
			if (className.startsWith("org.apache.derby")) return DERBY;

			// try from the jdbc metadata
			final DatabaseMetaData metaData = conn.getMetaData();
			String driver = null;
			String url = null;
			if (metaData != null) {
				driver = metaData.getDriverName();
				if (driver.contains("sqlserver")) return SQLSERVER;
				if (driver.contains("hsqldb")) return HSQL;
				url = metaData.getURL();
				if (url.startsWith("jdbc:sqlserver")) return SQLSERVER;
				if (url.startsWith("jdbc:hsql")) return HSQL;
				if (url.startsWith("jdbc:derby")) return DERBY;
			}

			System.err.println("unknown db type for Connection: "+ conn
					+" (driver:"+ driver +", url:"+ url +")");
			return null;
		}
 
Example 11
Source File: StatementCreatorUtils.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Set the specified PreparedStatement parameter to null,
 * respecting database-specific peculiarities.
 */
private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName)
		throws SQLException {

	if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER && typeName == null)) {
		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);
	}
}
 
Example 12
Source File: StatementCreatorUtils.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Set the specified PreparedStatement parameter to null,
 * respecting database-specific peculiarities.
 */
private static void setNull(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName)
		throws SQLException {

	if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER && typeName == null)) {
		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);
	}
}
 
Example 13
Source File: OracleSchema.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Set<String> getRecycleBinObjects(DatabaseMetaData dmd, String... types) {
    String driverName = null;
    String driverVer = null;
    List<String> emptyList = Collections.emptyList();
    Set<String> result = new HashSet<String>();
    try {
        driverName = dmd.getDriverName();
        driverVer = dmd.getDriverVersion();
        int databaseMajorVersion = 0;
        try {
            databaseMajorVersion = dmd.getDatabaseMajorVersion();
        } catch (UnsupportedOperationException use) {
            LOGGER.log(Level.FINEST, "getDatabaseMajorVersion() on " + dmd, use);
        }
        if (databaseMajorVersion < 10 || types == null) {
            return Collections.emptySet();
        }
        Statement stmt = dmd.getConnection().createStatement();
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery("SELECT OBJECT_NAME, TYPE FROM SYS.DBA_RECYCLEBIN"); // NOI18N
        } catch (SQLException ex) {
            LOGGER.log(Level.FINE, ex.getMessage(), ex); 
            // try both
            rs = stmt.executeQuery("SELECT OBJECT_NAME, TYPE FROM RECYCLEBIN"); // NOI18N
        }
        if (rs != null) {
            List<String> typesL = types == null ? emptyList : Arrays.asList(types);
            try {
                while (rs.next()) {
                    String type = rs.getString("TYPE"); // NOI18N
                    if (typesL.isEmpty() || typesL.contains(type)) {
                        result.add(rs.getString("OBJECT_NAME")); // NOI18N
                    }
                }
            } finally {
                rs.close();
            }
        }
        stmt.close();
    } catch (Exception e) {
        LOGGER.log(Level.INFO, "Error while analyzing the recycle bin. JDBC Driver: " + driverName + "(" + driverVer + ")", e);
    }
    return result;
}
 
Example 14
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Methods that describe the version of the
 * driver and database.
 */
public void testVersionInfo() throws SQLException
{
    DatabaseMetaData dmd = getDMD();
    int databaseMajor = dmd.getDatabaseMajorVersion();
    int databaseMinor = dmd.getDatabaseMinorVersion();

    int driverMajor = dmd.getDriverMajorVersion();
    int driverMinor = dmd.getDriverMinorVersion();

    String databaseVersion = dmd.getDatabaseProductVersion();
    String driverVersion = dmd.getDriverVersion();

    if (usingEmbedded())
    {
        // Database *is* the driver.

        assertEquals("Embedded Major version ",
                databaseMajor, driverMajor);
        assertEquals("Embedded Minor version ",
                databaseMinor, driverMinor);

        assertEquals("Embedded version",
                databaseVersion, driverVersion);
    }
    
    assertEquals("GemFireXD", dmd.getDatabaseProductName());

    String driverName = dmd.getDriverName();
    if (usingEmbedded())
    {
        assertEquals("GemFireXD Embedded JDBC Driver",
                driverName);
    }
    else if (usingDerbyNetClient())
    {
        assertEquals("GemFireXD Network Client JDBC Driver",
                driverName);
    }

    int jdbcMajor = dmd.getJDBCMajorVersion();
    int jdbcMinor = dmd.getJDBCMinorVersion();

    int expectedJDBCMajor = -1;
    if (JDBC.vmSupportsJDBC4())
    {
        expectedJDBCMajor = 4;
    }
    else if (JDBC.vmSupportsJDBC3())
    {
        expectedJDBCMajor = 3;
    }
    else if (JDBC.vmSupportsJSR169())
    {
        // Not sure what is the correct output for JSR 169
        expectedJDBCMajor = -1;
    }

    if (expectedJDBCMajor != -1)
    {
        assertEquals("JDBC Major version",
                expectedJDBCMajor, jdbcMajor);
        assertEquals("JDBC Minor version", 0, jdbcMinor);
    }
}
 
Example 15
Source File: DatabaseMetaDataTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Methods that describe the version of the
 * driver and database.
 */
public void testVersionInfo() throws SQLException
{
    DatabaseMetaData dmd = getDMD();
    int databaseMajor = dmd.getDatabaseMajorVersion();
    int databaseMinor = dmd.getDatabaseMinorVersion();

    int driverMajor = dmd.getDriverMajorVersion();
    int driverMinor = dmd.getDriverMinorVersion();

    String databaseVersion = dmd.getDatabaseProductVersion();
    String driverVersion = dmd.getDriverVersion();

    if (usingEmbedded())
    {
        // Database *is* the driver.

        assertEquals("Embedded Major version ",
                databaseMajor, driverMajor);
        assertEquals("Embedded Minor version ",
                databaseMinor, driverMinor);

        assertEquals("Embedded version",
                databaseVersion, driverVersion);
    }
    
    assertEquals("GemFireXD", dmd.getDatabaseProductName());

    String driverName = dmd.getDriverName();
    if (usingEmbedded())
    {
        assertEquals("GemFireXD Embedded JDBC Driver",
                driverName);
    }
    else if (usingDerbyNetClient())
    {
        assertEquals("GemFireXD Network Client JDBC Driver",
                driverName);
    }

    int jdbcMajor = dmd.getJDBCMajorVersion();
    int jdbcMinor = dmd.getJDBCMinorVersion();

    int expectedJDBCMajor = -1;
    if (JDBC.vmSupportsJDBC4())
    {
        expectedJDBCMajor = 4;
    }
    else if (JDBC.vmSupportsJDBC3())
    {
        expectedJDBCMajor = 3;
    }
    else if (JDBC.vmSupportsJSR169())
    {
        // Not sure what is the correct output for JSR 169
        expectedJDBCMajor = -1;
    }

    if (expectedJDBCMajor != -1)
    {
        assertEquals("JDBC Major version",
                expectedJDBCMajor, jdbcMajor);
        assertEquals("JDBC Minor version", 0, jdbcMinor);
    }
}
 
Example 16
Source File: cfUPDATE.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
protected PreparedStatement prepareStatement( Connection Con, String updateString, List<metaColumn> columnList ) throws Exception {
	PreparedStatement	pStatmt	= Con.prepareStatement( updateString );
	
	int x=1;
	metaColumn MC;
	Iterator<metaColumn> iter = columnList.iterator();
	
	// Determine if the parameters should be set using only setObject() or
	// using setObject() and setString().
	DatabaseMetaData dbmd = Con.getMetaData();
	String driverName = dbmd.getDriverName();
	boolean useOnlySetObject = true;
	if ( ( com.nary.db.metaDatabase.isOracleDatabase( dbmd ) ) ||
	     ( ( driverName != null ) && ( driverName.startsWith( "JDBC-ODBC Bridge" ) ) ) )
	{
		// 1. With Oracle, calling setObject for certain types will cause a ClassCastException.
		// 2. With the JDBC-ODBC bridge, calling setObject for certain types will cause an exception.
		useOnlySetObject = false;
	}

	while ( iter.hasNext() ){
		MC	= iter.next();
		
		if ( useOnlySetObject )
		{
			// For Sybase using the BEA driver calling setString() for an INTEGER column
			// will cause a failed to cast CHAR to INT exception so use setObject instead.
			pStatmt.setObject( x++, MC.VALUE, MC.SQLTYPE );
		}
		else 
		{
			if ( ( MC.SQLTYPE == Types.VARCHAR ) ||
				 ( MC.SQLTYPE == Types.LONGVARCHAR ) ||
				 ((MC.SQLTYPE == Types.BIGINT) && (com.nary.db.metaDatabase.isMySQLDatabase(dbmd))))
				pStatmt.setObject( x++, MC.VALUE, MC.SQLTYPE );
			else
				pStatmt.setString( x++, MC.VALUE );
		}
	}
			
	return pStatmt;
}