Friday, August 12, 2011

JAVA Program for JDBC Driver Version and Database Information

I had seen people ask Questions about finding the edb-jdbc/postgresql driver version with Database Version. So, I thought to give one java script which can be use to find Database Details with edb-jdbc version.

Following is a JAVA Code which can be use to find the Database Version and EDB-JDBC/postgresql-jdbc Version:
File Name: DBinfo.java

import java.sql.*;

public class DBinfo
{
  public static void main(String[] args)
  {
    try
    {
	  Class.forName("com.edb.Driver");
	  Connection con = 
DriverManager.getConnection("jdbc:edb://localhost:5444/edb",
	  "enterprisedb","edb"); // Advanced Server Database Connection Information	
 DatabaseMetaData dbmd = con.getMetaData();  
    
     System.out.println("=====  Database info =====");  
     System.out.println("DatabaseProductName: " + dbmd.getDatabaseProductName() );  
     System.out.println("DatabaseProductVersion: " + dbmd.getDatabaseProductVersion() );  
     System.out.println("DatabaseMajorVersion: " + dbmd.getDatabaseMajorVersion() );  
     System.out.println("DatabaseMinorVersion: " + dbmd.getDatabaseMinorVersion() );  
     System.out.println("=====  Driver info =====");  
     System.out.println("DriverName: " + dbmd.getDriverName() );  
     System.out.println("DriverVersion: " + dbmd.getDriverVersion() );  
     System.out.println("DriverMajorVersion: " + dbmd.getDriverMajorVersion() );  
     System.out.println("DriverMinorVersion: " + dbmd.getDriverMinorVersion() );  
     System.out.println("=====  JDBC/DB attributes =====");  
     System.out.print("Supports getGeneratedKeys(): ");  
     if (dbmd.supportsGetGeneratedKeys() )  
       System.out.println("true");  
     else  
       System.out.println("false");  
	  con.close();
	  System.out.println("Command successfully executed");
     }

     catch(ClassNotFoundException e)
     {
	   System.out.println("Class Not Found : " + e.getMessage()); 
     }
	
     catch(SQLException exp) {
	System.out.println("SQL Exception: " + exp.getMessage());
	System.out.println("SQL State: " + exp.getSQLState());
	System.out.println("Vendor Error: " + exp.getErrorCode());    
     }	
  }
}


To compile this program, user has to copy edb-jdbc14.jar file in $JAVA_HOME/jre/lib/ext directory .

After copying the edb-jdbc driver use following command to compile the program:
javac DBinfo.java
Following is output of above java program:
vibhor@ubuntu:~$ java DBinfo 
=====  Database info =====
DatabaseProductName: EnterpriseDB
DatabaseProductVersion: 9.0.4.14
DatabaseMajorVersion: 9
DatabaseMinorVersion: 0
=====  Driver info =====
DriverName: Postgres Plus Advanced Server Native Driver
DriverVersion: Postgres Plus Advanced Server 9.0 (9.0.4.14)
DriverMajorVersion: 9
DriverMinorVersion: 0
=====  JDBC/DB attributes =====
Supports getGeneratedKeys(): true
Command successfully executed


Above java code can also be use for find postgreSQL jdbc driver version.

For using with postgresql-jdbc driver, User has to change following
Class.forName("com.edb.Driver");
Connection con = 
DriverManager.getConnection("jdbc:edb://localhost:5444/edb",
	  "enterprisedb","edb"); // Advanced Server Database Connection Information	
with
Class.forName("org.postgresql.Driver");
Connection con = 
DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres",
	  "postgres","postgres"); // PostgreSQL Database Connection Information	
And has copy the postgresql-jdbc driver in $JAVA_HOME/jre/lib/ext

Then compile as mentioned above.
Following is output:
=====  Database info =====
DatabaseProductName: PostgreSQL
DatabaseProductVersion: 9.0.4.14
DatabaseMajorVersion: 9
DatabaseMinorVersion: 0
=====  Driver info =====
DriverName: PostgreSQL Native Driver
DriverVersion: PostgreSQL 8.4 JDBC4 (build 701)
DriverMajorVersion: 8
DriverMinorVersion: 4
=====  JDBC/DB attributes =====
Supports getGeneratedKeys(): true
Command successfully executed

No comments:

Post a Comment