get Jdbc Odbc Access Connection - Java java.sql

Java examples for java.sql:ODBC

Description

get Jdbc Odbc Access Connection

Demo Code


//package com.java2s;
import java.sql.*;

public class Main {
    public static Connection getAccessCon(String file) {
        try {/*ww w.j a  v  a 2 s.co m*/
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // set this to a MS Access DB you have on your machine
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
            database += file + ";DriverID=22;READONLY=true}";
            // now we can get the connection from the DriverManager
            return DriverManager.getConnection(database, "", "");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials