3

I am connecting to Oracle 11g/12c using java code by providing correct URL, User & Password. But now i want the oracle version like 11g/12c after successful connection through java code.

Please help to get that.

3 Answers3

5

Apart from querying the database, that information is also supplied by the JDBC driver, through DatabaseMetaData

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

e.g. Oracle 11.2 would result in majorVersion=11 and minorVersion=2

  • 1
    `getDatabaseProductVersion()` might give more information (not sure if that is the case for Oracle though). – Mark Rotteveel Sep 12 '18 at 09:24
  • @MarkRotteveel: that is true, but it returns a String which is really hard to parse, e.g.: `Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production`. I guess for most cases, major/minor should be enough –  Sep 12 '18 at 09:27
4

You can use the below code to get the version details,

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;
  }

}
Sathish
  • 196
  • 7
  • Your repeated `catch (Exception e)` around each and every method is 1) unnecessary (given these are required JDBC features to implement, and if they fail, subsequent calls will also fail, so one try-catch around all would be enough) and 2) don't catch `Exception` when a more specific exception (in this case `SQLException`) suffices. – Mark Rotteveel Sep 12 '18 at 09:29
2

you can use following sql

SELECT * FROM V$VERSION

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

Zheng Kun
  • 56
  • 4