Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {

    public static void main(String[] args) throws Exception {
        String url = "jdbc:odbc:databaseName";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, user, password);

            // Get the MetaData
            DatabaseMetaData metaData = conn.getMetaData();

            // Get driver information
            System.out.println("Driver Informaion");
            System.out.println(metaData.getDriverName());
            System.out.println(metaData.getDriverVersion());
            // Get schema information
            System.out.println("Schemas");
            ResultSet schemas = metaData.getSchemas();
            while (schemas.next()) {
                System.out.println(schemas.getString(1));
            }
            // Get table information
            System.out.println("Tables");
            ResultSet tables = metaData.getTables("", "", "", null);
            while (tables.next()) {
                System.out.println(tables.getString(3));
            }
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}