JDBCInfo

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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!");
        }
        
    }

}