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:
/**
* MySqlDatabaseInfo.java
* Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
*/
import java.sql.*;
import javax.sql.*;
public class MySqlDatabaseInfo {
public static void main(String [] args) {
Connection con = null;
try {
// Setting up the DataSource object
com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds
= new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
ds.setServerName("localhost");
ds.setPortNumber(3306);
ds.setDatabaseName("HerongDB");
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 confirms that JDBC driver mysql-connector-java-5.0.7 a JDBC 3.0 driver:
C:\>javac -cp .;\local\lib\mysql-connector-java-5.0.7-bin.jar
MySqlDatabaseInfo.java
C:\>java -cp .;\local\lib\mysql-connector-java-5.0.7-bin.jar
MySqlDatabaseInfo
Server name: MySQL
Server version: 5.0.45-community-nt
Driver name: MySQL-AB JDBC Driver
Driver version: mysql-connector-java-5.0.7
( $Date: 2007-03-09 22:13:57 +0100 (Fri, 09 Mar 2007)
$, $Revision: 6341 $ )
JDBC major version: 3
JDBC minor version: 0