import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
public class TestDatabaseMetaDataToolDatabaseInformation {
public static void main(String[] args) throws Exception {
Connection conn = getMySqlConnection();
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);
} catch (Exception e) {
System.out.println("major Version: unsupported feature");
}
try {
int minorVersion = meta.getDatabaseMinorVersion();
System.out.println("minorVersion" + minorVersion);
} catch (Exception e) {
System.out.println("minorVersion unsupported feature");
}
String productName = meta.getDatabaseProductName();
String productVersion = meta.getDatabaseProductVersion();
System.out.println("productName" + productName);
System.out.println("productVersion" + productVersion);
conn.close();
}
public static Connection getMySqlConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/databaseName";
String username = "root";
String password = "root";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
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;
}
}
|