1

How can I connect to an Access database from Java using JDBC?

Code provided in OP's comment

public static Connection getConnection() throws SQLException { 
    // connection object
    Connection con = null;

    // database url
    String connectionString = "jdbc:odbc:Driver= " 
            + "{Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + dbPath;

    try {
        Class.forName(driver);
        con = DriverManager.getConnection(connectionString);
    } catch (ClassNotFoundException ex) {
        System.out.println("connot load driver class");
        return con;
    }
}
5
  • 4
    Did you try asking google for jdbc msaccess? Commented Dec 14, 2011 at 12:11
  • yes :) but i still have a problem Commented Dec 14, 2011 at 12:13
  • 2
    So maybe you can tell us about your problem, what did you try so far? Commented Dec 14, 2011 at 12:15
  • public static Connection getConnection() throws SQLException { // connection object Connection con = null; // database url String connectionString = "jdbc:odbc:Driver= " + "{Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + dbPath; try { Class.forName(driver); con = DriverManager.getConnection(connectionString); } catch (ClassNotFoundException ex) { System.out.println("connot load driver class"); } return con; } Commented Dec 14, 2011 at 12:23
  • 1
    @soad el-hayek, instead of posting your code on a post section, update your question rather with your code and the stacktrace that you're getting. Commented Dec 14, 2011 at 12:26

6 Answers 6

1

you can use

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// set this to a MS Access DB you have on your machine

String filename = "d:/java/mdbTEST.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end 
// now we can get the connection from the DriverManager
Connection con = DriverManager.getConnection( database ,"",""); 

refer http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2

Sign up to request clarification or add additional context in comments.

1 Comment

I'VE use the same public static Connection getConnection() throws SQLException { Connection con = null; String connectionString = "jdbc:odbc:Driver= " + "{Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + dbPath; try { // load the driver class Class.forName(driver); con = DriverManager.getConnection(connectionString); } catch (ClassNotFoundException ex) { System.out.println("connot load driver class"); } // return the connection object return con; }
0

You'll need the ODBC-JDBC bridge for that.

See http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2 for sample source code. and http://www.youtube.com/watch?v=iXkGMu70HuM so see how to setup the data source (DSN).

1 Comment

for the video , i've win 7 64 bit
0

An alternative - JDBC to ODBC Bridge - which has a client/server architecture is also available...

This means the Java application and Access database can reside on different machines.

The following link will give you an idea of what goes where --

OpenLink Milti-tier JDBC to ODBC Bridge

Comments

0
public class NewClass {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/databasename";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
public void dataQuery(String queryData) {
    Class.forName(DRIVER);
    connection = DriverManager.getConnection(DATABASE_URL, "root", "");
    statement = connection.createStatement();
    resultSet = statement.executeQuery(queryData);
    while (resultSet.next()) {
        System.out.println(resultSet.getObject(1));
    }
}    
catch (Exception e) {            
    System.out.println("error Accessing");
}finally {
    try {
        resultSet.close();
        statement.close();
        connection.close();
    } catch (Exception exception) {
        System.out.println("error closing ");}
}
}
}

Comments

0

You can use ODBC connection to connect to Access Database from Java.

See the following example

http://www.csnotes32.com/2014/11/how-to-read-write-update-and-list-data.html

Comments

0

Now that the JDBC-ODBC Bridge has been removed from Java (since Java SE 8), future readers should consider using the UCanAccess JDBC driver instead. For more information see

Manipulating an Access database from Java without ODBC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.