This section describes how to get JDBC driver and database information through the DatabaseMetaData object.
Once you have created a database connection object, you can obtain some version information
about the JDBC driver and database server through the DatabaseMetaData object
as shown in the following program:
/**
* OracleDatabaseInfo.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.sql.*;
import javax.sql.*;
public class OracleDatabaseInfo {
public static void main(String [] args) {
Connection con = null;
try {
// Setting up the DataSource object
oracle.jdbc.pool.OracleDataSource ds
= new oracle.jdbc.pool.OracleDataSource();
ds.setDriverType("thin");
ds.setServerName("localhost");
ds.setPortNumber(1521);
ds.setDatabaseName("XE"); // Oracle SID
ds.setUser("Herong");
ds.setPassword("TopSecret");
// Getting a connection object
con = ds.getConnection();
// Getting driver and database info
DatabaseMetaData meta = con.getMetaData();
System.out.println("Server name: "
+ meta.getDatabaseProductName());
System.out.println("Server version: "
+ meta.getDatabaseProductVersion());
System.out.println("Driver name: "
+ meta.getDriverName());
System.out.println("Driver version: "
+ meta.getDriverVersion());
System.out.println("JDBC major version: "
+ meta.getJDBCMajorVersion());
System.out.println("JDBC minor version: "
+ meta.getJDBCMinorVersion());
// Closing the connection
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
}
}
}
The output reported that ojdbc14.jar is a JDBC 10.2 driver. But that report is wrong.
My guess is that it is a JDBC 3.0 driver.
C:\>javac -cp .;\local\lib\ojdbc14.jar OracleDatabaseInfo.java
C:\>java -cp .;\local\lib\ojdbc14.jar OracleDatabaseInfo
Server name: Oracle
Server version: Oracle Database 10g Express Edition Release 10.2.0.1.0
- Production
Driver name: Oracle JDBC driver
Driver version: 10.2.0.3.0
JDBC major version: 10
JDBC minor version: 2