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;
    }
}
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
soad el-hayek
  • 63
  • 1
  • 6
  • 4
    Did you try asking google for `jdbc msaccess`? – home Dec 14 '11 at 12:11
  • yes :) but i still have a problem – soad el-hayek Dec 14 '11 at 12:13
  • 2
    So maybe you can tell us about your problem, what did you try so far? – home Dec 14 '11 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; } – soad el-hayek Dec 14 '11 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. – Buhake Sindi Dec 14 '11 at 12:26

6 Answers6

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

Hemant Metalia
  • 25,838
  • 17
  • 67
  • 86
  • 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; } – soad el-hayek Dec 14 '11 at 12:25
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

ANAND
  • 11
  • 2
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

Community
  • 1
  • 1
Gord Thompson
  • 98,607
  • 26
  • 164
  • 342
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).

PhilW
  • 731
  • 6
  • 19
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

Garry M. Biggs
  • 232
  • 1
  • 1
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 ");}
}
}
}
LynAs
  • 5,503
  • 11
  • 41
  • 77