如何在java代码中连接后检查oracle数据库版本

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

请帮助得到那个。


qq_遁去的一_1
浏览 241回答 3
3回答

慕哥6287543

除了查询数据库之外,该信息还由 JDBC 驱动程序提供,通过 DatabaseMetaDataConnection con = DriverManager.connect(...);DatabaseMetaData meta = con.getMetaData();int majorVersion = meta.getDatabaseMajorVersion();int minorVersion = meta.getDatabaseMinorVersion();例如 Oracle 11.2 将导致majorVersion=11和minorVersion=2

MM们

您可以使用以下代码获取版本详细信息,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;  }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java