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

The following examples show how to use java.sql.DatabaseMetaData#getDriverMajorVersion() . 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: MySqlDatabaseMeta.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the column name for a MySQL field checking if the driver major version is "greater than" or "lower or equal" to 3.
 *
 * @param dbMetaData
 * @param rsMetaData
 * @param index
 * @return The column label if version is greater than 3 or the column name if version is lower or equal to 3.
 * @throws HopDatabaseException
 */
@Override
public String getLegacyColumnName( DatabaseMetaData dbMetaData, ResultSetMetaData rsMetaData, int index ) throws HopDatabaseException {
  if ( dbMetaData == null ) {
    throw new HopDatabaseException( BaseMessages.getString( PKG, "MySQLDatabaseMeta.Exception.LegacyColumnNameNoDBMetaDataException" ) );
  }

  if ( rsMetaData == null ) {
    throw new HopDatabaseException( BaseMessages.getString( PKG, "MySQLDatabaseMeta.Exception.LegacyColumnNameNoRSMetaDataException" ) );
  }

  try {
    return dbMetaData.getDriverMajorVersion() > 3 ? rsMetaData.getColumnLabel( index ) : rsMetaData.getColumnName( index );
  } catch ( Exception e ) {
    throw new HopDatabaseException( String.format( "%s: %s", BaseMessages.getString( PKG, "MySQLDatabaseMeta.Exception.LegacyColumnNameException" ), e.getMessage() ), e );
  }
}
 
Example 2
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 3
Source File: MySQLDatabaseMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the column name for a MySQL field checking if the driver major version is "greater than" or "lower or equal" to 3.
 *
 * @param dbMetaData
 * @param rsMetaData
 * @param index
 * @return The column label if version is greater than 3 or the column name if version is lower or equal to 3.
 * @throws KettleDatabaseException
 */
public String getLegacyColumnName( DatabaseMetaData dbMetaData, ResultSetMetaData rsMetaData, int index ) throws KettleDatabaseException {
  if ( dbMetaData == null ) {
    throw new KettleDatabaseException( BaseMessages.getString( PKG, "MySQLDatabaseMeta.Exception.LegacyColumnNameNoDBMetaDataException" ) );
  }

  if ( rsMetaData == null ) {
    throw new KettleDatabaseException( BaseMessages.getString( PKG, "MySQLDatabaseMeta.Exception.LegacyColumnNameNoRSMetaDataException" ) );
  }

  try {
    return dbMetaData.getDriverMajorVersion() > 3 ? rsMetaData.getColumnLabel( index ) : rsMetaData.getColumnName( index );
  } catch ( Exception e ) {
    throw new KettleDatabaseException( String.format( "%s: %s", BaseMessages.getString( PKG, "MySQLDatabaseMeta.Exception.LegacyColumnNameException" ), e.getMessage() ), e );
  }
}
 
Example 4
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 5
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);
    }
}