/**
 * Abschnitt 16.6.1
 */

package model;

public class Connect {
    
    private java.sql.Connection con = null;
    private final String url = "jdbc:jtds:sqlserver://";
    private final String serverName = "213.47.212.61";
    private final String portNumber = "1433";
    private final String databaseName =
                        "AdventureWorksLT2008R2";
    private final String userName = "dbsql";
    private final String password = "";

    // Informs the driver to use server a side-cursor,
    // which permits more than one active statement
    // on a connection.
    private final String selectMethod = "cursor";

    private String getConnectionUrl() {
        return url + serverName + ":" + portNumber +
                ";databaseName=" + databaseName + ";";
    }

    private java.sql.Connection getConnection() {
        try {
            Class.forName( 
                    "net.sourceforge.jtds.jdbc.Driver");
            con = java.sql.DriverManager.getConnection(
               getConnectionUrl(), userName, password);

            if (con != null) {
                System.out.println("Conn. success!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println( "Error Trace in
                  getConnection() : " + e.getMessage());
        }

        return con;
    }

    /**
     *    Displays driver props and db details.
     */
    public void displayDbProperties() {
        java.sql.DatabaseMetaData dm = null;
        java.sql.ResultSet rs = null;

        try {
            con = this.getConnection();

            if (con != null) {
                dm = con.getMetaData();
                System.out.println("Driver Info");
                System.out.println("\tDriver Name: " +
                        dm.getDriverName());
                System.out.println("\tDriver Version: "
                      + dm.getDriverVersion());
                System.out.println("\nDb Info ");
                System.out.println("\tDb Name: " +
                        dm.getDatabaseProductName());
                System.out.println("\tDb Version: " +
                       dm.getDatabaseProductVersion());
                System.out.println("Catalogs ");
                rs = dm.getCatalogs();

                while (rs.next()) {
                    System.out.println("\tcatalog: " +
                         rs.getString(1));
                }

                rs.close();
                rs = null;
                closeConnection();
            } else {
                System.out.println(
                     "Error: No active Connection");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        dm = null;
    }

    private void closeConnection() {
        try {
            if (con != null) {
                con.close();
            }

            con = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) 
    throws Exception {
        Connect theDb = new Connect();
        theDb.displayDbProperties();
    }
}


//~ Formatted by Jindent --- http://www.jindent.com

