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

The following examples show how to use java.sql.DatabaseMetaData#getDriverVersion() . 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: DatabaseMetaDataIT.java    From snowflake-jdbc with Apache License 2.0 6 votes vote down vote up
@Test
public void testDatabaseAndDriverInfo() throws SQLException
{
  try (Connection connection = getConnection())
  {
    DatabaseMetaData metaData = connection.getMetaData();

    // JDBC x.x compatible
    assertEquals(1, metaData.getJDBCMajorVersion());
    assertEquals(0, metaData.getJDBCMinorVersion());

    // identifiers
    assertEquals("Snowflake", metaData.getDatabaseProductName());
    assertEquals("Snowflake", metaData.getDriverName());

    // Snowflake JDBC driver version
    String driverVersion = metaData.getDriverVersion();
    Matcher m = VERSION_PATTERN.matcher(driverVersion);
    assertTrue(m.matches());
    int majorVersion = metaData.getDriverMajorVersion();
    int minorVersion = metaData.getDriverMinorVersion();
    assertEquals(m.group(1), String.valueOf(majorVersion));
    assertEquals(m.group(2), String.valueOf(minorVersion));
  }
}
 
Example 2
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 3
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 4
Source File: Jdbc42DateTime.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
public static void main(String[] args) throws SQLException {
    Connection connection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);

    final DatabaseMetaData metaData = connection.getMetaData();

    final String driverVersion = metaData.getDriverVersion();
    System.out.println("driverVersion = " + driverVersion);
    System.out.println("jdbcVersion = " + metaData.getJDBCMajorVersion()
    + "." + metaData.getJDBCMinorVersion());

    final String timeDateFunctions = metaData.getTimeDateFunctions();
    System.out.println("timeDateFunctions = " + timeDateFunctions);

    ResultSet rs = metaData.getTypeInfo();

    while(rs.next()) {
        for(int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
            System.out.print(rs.getMetaData().getColumnName(i + 1) + " = " + rs.getObject(i + 1));

            System.out.print(", ");
        }
        System.out.println("");
    }

    final Statement statement = connection.createStatement();

    final ResultSet resultSet = statement.executeQuery("SELECT current_timestamp");

    while (resultSet.next()) {
        final String columnTypeName = resultSet.getMetaData().getColumnTypeName(1);
        System.out.println("columnTypeName = " + columnTypeName);
        final Timestamp timestamp = resultSet.getTimestamp(1);
        System.out.println("resultSet = " + timestamp);
        final Object object = resultSet.getObject(1, OffsetDateTime.class);
        System.out.println("resultSet = " + object + "/" + object.getClass());
    }

}
 
Example 5
Source File: AlternateDataSourceTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
private static String getVersion(final DataSource ds) throws SQLException {
    Connection con = null;
    try {
        con = ds.getConnection();
        final DatabaseMetaData md = con.getMetaData();
        return md.getDriverVersion();
    } finally {
        if (con != null) {
            con.close();
        }
    }
}
 
Example 6
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 7
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 8
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 9
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 10
Source File: AlternateDriverJarEmbeddedDemo.java    From tomee with Apache License 2.0 4 votes vote down vote up
public String getDriverVersion() throws Exception {

            final Connection con = ds.getConnection();
            final DatabaseMetaData md = con.getMetaData();
            return md.getDriverVersion();
        }
 
Example 11
Source File: AlternateDriverJarEmbeddedDemo.java    From tomee with Apache License 2.0 4 votes vote down vote up
public String getDriverVersion() throws Exception {

            final Connection con = ds.getConnection();
            final DatabaseMetaData md = con.getMetaData();
            return md.getDriverVersion();
        }
 
Example 12
Source File: AlternateDriverJarTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public String getDriverVersion() throws Exception {

            final Connection con = ds.getConnection();
            final DatabaseMetaData md = con.getMetaData();
            return md.getDriverVersion();
        }
 
Example 13
Source File: AlternateDriverJarTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
public String getDriverVersion() throws Exception {

            final Connection con = ds.getConnection();
            final DatabaseMetaData md = con.getMetaData();
            return md.getDriverVersion();
        }
 
Example 14
Source File: MssqlIntermediateFacade.java    From dekaf with Apache License 2.0 4 votes vote down vote up
@Override
public ConnectionInfo obtainConnectionInfoNatively() {
  String[] env;
  Version serverVersion, driverVersion;

  final JdbcIntermediateSession session = openSession();
  try {
    // environment
    env = session.queryOneRow(CONNECTION_INFO_QUERY, 4, String.class);
    if (env == null) env = new String[] {null, null, null, null};

    // versions
    String rdbmsName, serverVersionStr, driverVersionStr;
    try {
      DatabaseMetaData md = session.getConnection().getMetaData();
      if (env[3] != null && env[3].contains("Azure")) {
        rdbmsName = Mssql.AZURE_FLAVOUR;
      }
      else {
        rdbmsName = md.getDatabaseProductName();
        if (rdbmsName == null) rdbmsName = session.getConnection().getClass().getName();
      }
      serverVersionStr = md.getDatabaseProductVersion();
      driverVersionStr = md.getDriverVersion();
    }
    catch (SQLException sqle) {
      throw getExceptionRecognizer().recognizeException(sqle, "getting versions using JDBC metadata");
    }

    serverVersion =
        extractVersion(serverVersionStr, SIMPLE_VERSION_PATTERN, 1);
    driverVersion =
        extractVersion(driverVersionStr, SIMPLE_VERSION_PATTERN, 1);

    // ok
    return new ConnectionInfo(rdbmsName, env[0], env[1], env[2], serverVersion, driverVersion);
  }
  finally {
    session.close();
  }
}
 
Example 15
Source File: MysqlIntermediateFacade.java    From dekaf with Apache License 2.0 4 votes vote down vote up
@Override
public ConnectionInfo obtainConnectionInfoNatively() {
  String[] env;
  Version serverVersion = null, driverVersion;

  final JdbcIntermediateSession session = openSession();
  try {
    // environment
    env = session.queryOneRow(CONNECTION_INFO_QUERY, 4, String.class);
    String verComment = env == null ? null : env[3];
    if (verComment != null) verComment = verComment.toLowerCase(Locale.ENGLISH);
    if (env == null) env = new String[] {null, null, null};

    // versions
    String rdbmsName, serverVersionStr, driverVersionStr;
    try {
      DatabaseMetaData md = session.getConnection().getMetaData();
      serverVersionStr = md.getDatabaseProductVersion();
      driverVersionStr = md.getDriverVersion();
      rdbmsName = md.getDatabaseProductName();
      if (rdbmsName == null) rdbmsName = session.getConnection().getClass().getName();
    }
    catch (SQLException sqle) {
      throw getExceptionRecognizer().recognizeException(sqle, "getting versions using JDBC metadata");
    }

    if (rdbmsName.equals("MySQL")) {
      int pos = serverVersionStr.indexOf(Mysql.MARIADB_FLAVOUR);
      if (pos != -1) {
        serverVersion = extractMariaVersionImpl(pos, serverVersionStr);
        if (serverVersion != Version.ZERO) rdbmsName = Mysql.MARIADB_FLAVOUR;
        else serverVersion = null;
      }
      else if (verComment != null) {
        if (verComment.startsWith("memsql")) {
          rdbmsName = Mysql.MEMSQL_FLAVOUR;
          String[] memVer = session.queryOneRow("select @@memsql_version", 1, String.class);
          if (memVer != null && memVer[0] != null) serverVersion = extractVersion(memVer[0], SIMPLE_VERSION_PATTERN, 1);
        }
        else if (verComment.startsWith("mariadb")) rdbmsName = Mysql.MARIADB_FLAVOUR;
      }
    }

    if (serverVersion == null) serverVersion = extractVersion(serverVersionStr, SIMPLE_VERSION_PATTERN, 1);
    driverVersion = extractVersion(driverVersionStr, SIMPLE_VERSION_PATTERN, 1);

    // ok
    return new ConnectionInfo(rdbmsName, env[0], env[1], env[2], serverVersion, driverVersion);
  }
  finally {
    session.close();
  }
}