JDBCInfo

Here is a small Java application which connects to a database using JDBC and prints some information.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCInfo {
private Connection conn;

public JDBCInfo(String driver, String url) {
if (driver != null) {
try {
Class.forName(driver);
System.out.println("Registered JDBC driver '" + driver + "'");
} catch (ClassNotFoundException e) {
System.err.println("Failed to register JDBC driver '" +
e.getMessage() + "'");
System.exit(1);
}
}

if (url != null) {
try {
this.conn = DriverManager.getConnection(url);
} catch (SQLException e) {
System.err.println("Failed to create database connection: " +
e.getMessage());
System.exit(1);
}
}
}

public static void main(String[] args) {
JDBCInfo test = new JDBCInfo(args[0], args[1]);
test.printDatabaseMetaData();
}

private void printDatabaseMetaData() {
try {
String databaseProductName =
this.conn.getMetaData().getDatabaseProductName();
String databaseProductVersion =
this.conn.getMetaData().getDatabaseProductVersion();

System.out.println("Database Product Name: " + databaseProductName);
System.out.println("Database Product Version: " + databaseProductVersion);

int maximumColumnNameChars = this.conn.getMetaData().getMaxColumnNameLength();
System.out.println("Max number of chars for a column name: " + maximumColumnNameChars);
int maximumTableNameChars = this.conn.getMetaData().getMaxTableNameLength();
System.out.println("Max number of chars for a table name: " + maximumTableNameChars);

System.out.println("Supported Database Types:");
ResultSet databaseTypes = this.conn.getMetaData().getTypeInfo();
while (databaseTypes.next())
{
String typeName = databaseTypes.getString("TYPE_NAME");
System.out.println(typeName);
}
}
catch (SQLException e) {
System.err.println("Could not get Database meta data!");
}

}

}