What Is the JDBC's Driver Information? JDBC

DatabaseMetaData has four driver-related methods, which are discussed in this
section. We will combine all of them into a single method called
getDriverInformation() and return the result as XML (serialized as a String object).

DatabaseMetaData Methods Supporting Driver Information

int getJDBCMajorVersion()

// Retrieves the major JDBC version number for this driver.
int getJDBCMinorVersion()
// Retrieves the minor JDBC version number for this driver.
String getDriverName()
// Retrieves the name of this JDBC driver.
String getDriverVersion()
// Retrieves the version number of this JDBC driver as a String.
XML Syntax for Output (Driver Information)
<?xml version='1.0'>
<DriverInformation>
<driverName>driver name</driverName>
<driverVersion>driver version</driverVersion>
<jdbcMajorVersion>JDBC major version</jdbcMajorVersion>
<jdbcMinorVersion>JDBC minor version</jdbcMinorVersion>
</DriverInformation>

The Solution

The solution is generic enough and can support MySQL, Oracle, and other relational databases.

/**
* Get driver name and version information.
* This method calls 4 methods (getDriverName(),
* getDriverVersion(), getJDBCMajorVersion(),
* getJDBCMinorVersion()) to get the required information
* and it returns the information as XML.
*
* @param conn the Connection object
* @return driver name and version information
* as an XML document (represented as a String object).
*
*/
public static String getDriverInformation(java.sql.Connection conn)
throws Exception {
try {
DatabaseMetaData meta = conn.getMetaData();
if (meta == null) {
return null;
}
StringBuffer sb = new StringBuffer("<?xml version='1.0'>");
sb.append("<DriverInformation>");
// Oracle (and some other vendors) do not support
// some the following methods; therefore, we need
// to use a try-catch block.
try {
int jdbcMajorVersion = meta.getJDBCMajorVersion();
appendXMLTag(sb, "jdbcMajorVersion", jdbcMajorVersion);
}
catch(Exception e) {
appendXMLTag(sb, "jdbcMajorVersion", "unsupported feature");
}
try {
int jdbcMinorVersion = meta.getJDBCMinorVersion();
appendXMLTag(sb, "jdbcMinorVersion", jdbcMinorVersion);
}
catch(Exception e) {
appendXMLTag(sb, "jdbcMinorVersion", "unsupported feature");
}
String driverName = meta.getDriverName();
String driverVersion = meta.getDriverVersion();
appendXMLTag(sb, "driverName", driverName);
appendXMLTag(sb, "driverVersion", driverVersion);
sb.append("</DriverInformation>");
return sb.toString();
}
catch(Exception e) {
// handle exception
e.printStackTrace();
throw new Exception("could not get the database information:"+
e.toString());
}
}

Discussion

To get the driver information (such as the name and version), we call the methods (listed earlier) and the result is returned as XML. The advantage of our solution is that you get the required information with a single call and the result (as XML) can be used by any kind of client. Note that
oracle.jdbc.OracleDatabaseMetaData.getJDBCMajorVersion() and
oracle.jdbc.OracleDatabaseMetaData.getJDBCMinorVersion() are unsupported
features; therefore, we have to use a try-catch block. If the method returns a SQLException, we return the message “unsupported feature” in the XML result. The driver information does not change frequently and therefore it can be cached in the server-side.

Client Using MySQL

import java.util.*;
import java.io.*;
import java.sql.*;
import jcb.db.*;
import jcb.meta.*;
public class TestMySqlDatabaseMetaDataTool_DriverInformation {
public static Connection getConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/octopus";
String username = "root";
String password = "root";
Class.forName(driver); // load MySQL driver
Return DriverManager.getConnection(url, username, password);
}
public static void main(String[] args) {
Connection conn = null;
try {
conn = getConnection();
System.out.println("-------- getDriverInformation -------------");
System.out.println("conn="+conn);
String driverInfo = DatabaseMetaDataTool.getDriverInformation(conn);
System.out.println(driverInfo);
System.out.println("------------------------------------");
}
catch(Exception e){
e.printStackTrace();
System.exit(1);
}
finally {
DatabaseUtil.close(conn);
}
}
}

Output Using MySQL

-------- getDriverInformation ------
[email protected]
<?xml version='1.0'>
<DriverInformation>
<jdbcMajorVersion>3</jdbcMajorVersion>
<jdbcMinorVersion>0</jdbcMinorVersion>
<driverName>MySQL-AB JDBC Driver</driverName>
<driverVersion>3.0.5-gamma</driverVersion>
</DriverInformation>
------------------------------------

Client Using Oracle

import java.util.*;
import java.io.*;
import java.sql.*;
import jcb.db.*;
import jcb.meta.*;
public class TestOracleDatabaseMetaDataTool_DriverInformation {
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:maui";
String username = "octopus";
String password = "octopus";
Class.forName(driver); // load Oracle driver
return DriverManager.getConnection(url, username, password);
}
public static void main(String[] args) {
Connection conn = null;
try {
conn = getConnection();
System.out.println("-------- getDriverInformation -------------");
System.out.println("conn="+conn);
String driverInfo = DatabaseMetaDataTool.getDriverInformation(conn);
System.out.println(driverInfo);
System.out.println("------------------------------------");
}
catch (Exception e){
e.printStackTrace();
System.exit(1);
}
finally {
DatabaseUtil.close(conn);
}
}
}

Output Using Oracle

The following output is formatted to fit the page:

-------- getDriverInformation ------
[email protected]
<?xml version='1.0'>
<DriverInformation>
<jdbcMajorVersion>unsupported feature</jdbcMajorVersion>
<jdbcMinorVersion>unsupported feature</jdbcMinorVersion>
<driverName>Oracle JDBC driver</driverName>
<driverVersion>9.2.0.1.0</driverVersion>
</DriverInformation>
-----------------------------------


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics