2

How do I connect to an Access database in Java?

I have done like this:

package inspection.management.system;

import java.sql.*;

/**
 *
 * @author Fuhans
 */

public class Database 
{
    public static void DatabaseConnectivity()
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "d:\\program files\\project\\program\\inspection management system\\db1.accdb";

            Connection conn = DriverManager.getConnection(url);

            System.out.println("Connection Successful");
            InfoBox.ShowMessageBox("Connection Successful!", "Success");
        } 

        catch (Exception e) 
        {
            System.err.println("Got an exception!");
            System.err.println(e.getMessage());

            InfoBox.ShowMessageBox("Got an Exception!", "Error");
            InfoBox.ShowMessageBox(e.getMessage(), "Error");
        }
    }
}

if (_textField1.equals("Fuhans") && _passwordField1.equals("Xavega"))
        {
            Sound.PlaySound(1);
            InfoBox.ShowMessageBox("Successfully Login!", "Success");
            Database.DatabaseConnectivity();
        }

When i successfully login, it gave me error on database:

enter image description here

What have i done wrong?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Kaoru
  • 2,853
  • 14
  • 34
  • 68
  • Did you create the data source using the ODBC Administrator app? – OldProgrammer Nov 15 '13 at 17:46
  • not, in the ODBC Administrator app, i don't have Ms access Driver, i just have SQL Driver. But right now for this app, i am using Ms Access. – Kaoru Nov 15 '13 at 17:48
  • So if there is no Access ODBC driver installed, how do you expect to connect to an Access db then? – OldProgrammer Nov 15 '13 at 17:49
  • in c#, i just connect it through code, no need to set all the things, just create a database and access it through code. But now i am using Java and i don't know how to connect my application in java to the database. Could you please help me sir? – Kaoru Nov 15 '13 at 17:51

3 Answers3

2

in the ODBC Administrator app, i don't have Ms access Driver, i just have SQL Driver.

Now that the JDBC-ODBC Bridge has been removed from Java (since Java 8) you should consider using the UCanAccess JDBC driver. It is a pure Java implementation so it works on non-Windows platforms, too.

For more information see

Manipulating an Access database from Java without ODBC

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
1

You should create a DSN(Data Source Name) first.

In control panel, if there is no drivers for accessing even though you have installed then there is a possibility that you may not getting odbcad32.exe file path. Choose your path from this, and then right click Data Sources(ODBC)[where you are creating DSN], and paste one of following path there.

  1. The 32-bit version of the Odbcad32.exe file is located at:

    %WinDir%\Windows\SysWoW64

  2. The 64-bit version of the Odbcad32.exe file is located at:

    %WinDir%\Windows\System32

and while accessing, do like this:

String url = "jdbc:odbc:dsn_name";
Connection conn = DriverManager.getConnection(url);
Ysr Shk
  • 224
  • 5
  • 16
0

change the statement:

String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "d:\\program files\\project\\program\\inspection management system\\db1.accdb";

to:

String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C://program files//project//program//inspection management system//db1.accdb";
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
Mr J
  • 1