/*
 * MdbSoilDatabase.java
 *
 * Created on January 10, 2008, 4:44 PM
 *
 */
package usda.weru.soil;

import de.schlichtherle.truezip.file.TFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.openide.util.Exceptions;

/**
 *
 * @author Joseph Levin
 */
public class MdbSoilDatabase extends JdbcSoilDatabase {

    private static final String ODBC_PREFIX = "JDBC:ODBC:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
    private static final String ODBC_SUFFIX = ";READONLY=true;DriverID=22";

    /** Creates a new instance of MdbSoilDatabase
     * @param file */
    public MdbSoilDatabase(TFile file) {
        this(file, file.getName());
    }

    /**
     *
     * @param file
     * @param name
     */
    public MdbSoilDatabase(TFile file, String name) {
        super(name);
        c_file = file;
    }

    /**
     *
     * @return
     */
    @Override
    protected Connection createConnection() {
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            String url = "jdbc:ucanaccess://" + c_file.getAbsolutePath() + ";readonly=true";
            Boolean recordTime = false;
            long startTime = 0;
            if (recordTime) {
                System.out.println("begin connection");
                startTime = System.currentTimeMillis();
            }
            Connection c = DriverManager.getConnection(url, "sa", "");
            if (recordTime) {
                long endTime = System.currentTimeMillis();
                double seconds = (endTime - startTime)/1000.0;
                double minutes = seconds / 60;
                System.out.println("connection created; it took " + (int)minutes
                        + " minutes and " + (int)seconds%60 + " seconds.");
            }
            
            /*
            try {
            try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            } catch (ClassNotFoundException ex) {
            Logger.getLogger(MdbSoilDatabase.class.getName()).log(Level.SEVERE, null, ex);
            return null;
            }
            String url = ODBC_PREFIX + c_file.getAbsolutePath() + ODBC_SUFFIX;
            
            // Attempt to connect to a driver.
            return DriverManager.getConnection(url);
            //Error creating connection to mdb
            } catch (SQLException ex) {
            Logger.getLogger(MdbSoilDatabase.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
            */
            return c;
        } catch (ClassNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        } catch (SQLException ex) {
            Exceptions.printStackTrace(ex);
        }
        return null;
    }

    /**
     *
     */
    @Override
    public void dispose() {
        super.dispose();
    }

}
