用Java代码连接后如何检查Oracle数据库版本

时间:2018-09-12 08:55:55

标签: java oracle jdbc

我通过使用Java代码通过提供正确的URL,用户名和密码来连接到Oracle 11g / 12c。 但是现在通过Java代码成功连接后,我想要像11g / 12c这样的oracle版本。

请帮助获得它。

3 个答案:

答案 0 :(得分:5)

除了查询数据库外,该信息还由JDBC驱动程序通过DatabaseMetaData

提供
Connection con = DriverManager.connect(...);
DatabaseMetaData meta = con.getMetaData();
int majorVersion = meta.getDatabaseMajorVersion();
int minorVersion = meta.getDatabaseMinorVersion();

例如Oracle 11.2将导致majorVersion=11minorVersion=2

答案 1 :(得分:1)

您可以使用以下sql

SELECT * FROM V$VERSION

https://community.oracle.com/thread/2250946

答案 2 :(得分:1)

您可以使用以下代码获取版本详细信息,

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;

public class TestDatabaseMetaDataToolDatabaseInformation {
  public static void main(String[] args) {
    Connection conn = getOracleConnection();
    DatabaseMetaData meta = conn.getMetaData();
    // Oracle (and some other vendors) do not support
    // some the following methods; therefore, we need
    // to use try-catch block.
    try {
      int majorVersion = meta.getDatabaseMajorVersion();
      System.out.println("major Version: " + majorVersion);
      int minorVersion = meta.getDatabaseMinorVersion();
      System.out.println("minorVersion" + minorVersion);
      String productName = meta.getDatabaseProductName();
      String productVersion = meta.getDatabaseProductVersion();
      System.out.println("productName" + productName);
      System.out.println("productVersion" + productVersion);
    } catch (SQLException e) {
      System.out.println("minorVersion unsupported feature");
      System.out.println("major Version: unsupported feature");
    } finally {
        conn.close();
    }
  }

  public static Connection getOracleConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "name";
    String password = "password";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

}