/*
 * @(#)TestODBCBridge.java
 *
 * Summary: TestODBCBridge: demonstrate the how to connect to a Microsoft Access via JDBC and the ODBC bridge. You will get.
 *
 * Copyright: (c) 2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2007-09-22
 */
package com.mindprod.example;

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

import static java.lang.System.err;

/**
 * TestODBCBridge: demonstrate the how to connect to a Microsoft Access via JDBC and the ODBC bridge. You will get.
 * <p/>
 * better performance with a direct JDBC bridge. see http://developers.sun.com/product/jdbc/drivers
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2007-09-22
 * @since 2007-09-22
 */
@SuppressWarnings( { "WeakerAccess", "UnusedDeclaration" } )
public class TestODBCBridge
    {
    // ------------------------------ CONSTANTS ------------------------------

    /**
     * which database
     */
    private static final String DATABASENAME = "SQUIRRELS";

    /**
     * class name of the JDBC driver
     */
    private static final String DRIVERCLASSNAME =
            "sun.jdbc.odbc.JdbcOdbcDriver";

    /**
     * access PASSWORD
     */
    private static final String PASSWORD = "sesame";

    /**
     * basic URL to access the database
     */
    @SuppressWarnings( { "ConstantNamingConvention" } )
    private static final String URL = "jdbc:odbc:";

    /**
     * login name of user
     */
    private static final String USERNAME = "charlie";

    // ------------------------------ FIELDS ------------------------------

    /**
     * The connection.  Handle to the database
     */
    @SuppressWarnings( { "FieldCanBeLocal", "UnusedDeclaration" } )
    private Connection conn;

    // -------------------------- STATIC METHODS --------------------------

    /**
     * glue parts of connection URL together.
     *
     * @return complete URL to access database.
     */
    private static String getConnectionUrl()
        {
        return URL + ":" + DATABASENAME;
        }

    // -------------------------- OTHER METHODS --------------------------

    /**
     * connect to the database
     */
    private void init()
        {
        try
            {
            Class.forName( DRIVERCLASSNAME );
            }
        catch ( Exception e )
            {
            err.println( "can't load ODBC bridge JDBC driver: "
                         + e.getMessage() );
            }
        try
            {
            conn =
                    DriverManager.getConnection( getConnectionUrl(),
                            USERNAME,
                            PASSWORD );
            }
        catch ( SQLException e )
            {
            err.println( "can't connect to Access via the JDBC/ODBC bridge: "
                         + e.getMessage() );
            }
        }
    }