Wednesday, February 15, 2012

Connect To Access Database Using Java

11 comments
package jsupport.com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jsupport
 */
public class AccessDatabaseConnection {

    public static Connection connect() {

        Connection con;
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Data.mdb;";
            con = DriverManager.getConnection(database, "Admin", "64");

        } catch (Exception ex) {
            return null;
        }
        return con;
    }

    public void getData(){

        try {

            Statement stmt = connect().createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM tblData");

            if (rset.next()) {

                String name     = rset.getString("user_name");
                String email    = rset.getString("user_email");

                System.out.println("Name  : " +name +"  Email : "+email);
            }


        } catch (SQLException ex) {
            
        }
    }

    public static void main(String[] args) {
        new AccessDatabaseConnection().getData();
    }
}



Access Database table


     Data.mdb




11 comments:

DCarrot said...

Slightly unrelated but ...
If the Access db is created by a Java program, populated by a Java porgram and emailed by a Java program WITHOUT the user EVER opening up the db using the Access application (or indeed any other application),
is a MS Access license required?

Dev said...

Yes, MS Access required licence.you must create access database using MS Access. you cannot create Access database using Java

Unknown said...

Hi there

I am getting a NullPointerException from the getData() method. with the following error message:

Exception in thread "main" java.lang.NullPointerException
at LanguageTest.getData(LanguageTest.java:45)
at LanguageTest.main(LanguageTest.java:63)

**Notes:
* (LanguageTest) is the name of my class
* I have a *.accdb database so I changed that database string to
"jdbc:odbc:Driver={Microsoft Access Driver (*.accdb)};DBQ=MailMergeProjcet1.accdb;"

* my database is not password protected so I changed the Connection variable to
con = DriverManager.getConnection(database);

So could you please guide me through and thanks in advanced

Unknown said...

@Aamer use con = DriverManager.getConnection(database, "", "");

Unknown said...

I am getting the same error..

Exception in thread "main" java.lang.NullPointerException
at DbConnection.getData(DbConnection.java:36)
at DbConnection.main(DbConnection.java:54)

How could solve this???

Unknown said...

Hi there,

This is my modified code:




import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Jsupport
*/
public class DbConnection {

public static Connection connect() {

Connection con;
try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Serverdb.mdb;";
con = DriverManager.getConnection(database, "", "");

} catch (Exception ex) {
return null;
}
return con;
}

public void getData(){

try {

Statement stmt = connect().createStatement();
ResultSet rset = stmt.executeQuery("SELECT * FROM tblData");

if (rset.next()) {

String name = rset.getString("user_name");
String email = rset.getString("user_email");

System.out.println("Name : " +name +" Email : "+email);
}


} catch (SQLException ex) {

}
}

public static void main(String[] args) {
new DbConnection().getData();
}
}


I am getting Exception error like this

Exception in thread "main" java.lang.NullPointerException
at DbConnection.getData(DbConnection.java:36)
at DbConnection.main(DbConnection.java:54)


How to solve this please help me

Dev said...

check whether you added the odbc/jdbc driver library

Unknown said...

Sorry for the late reply... Actually I Now its working fine in my code. Thanks for your valuable reply. And If you don't mind will u help me to develope a desktop application using MS-Access db. I created a registration form and now i wan to make it as a jar file or .exe file. And I succeeded in creating jar file by exporting the project. But the jar file didnt getting the db data values. Could u pls help me to create that application. Thanks in advance.

Unknown said...

Is any drivers available for Microsoft Access??? if so please give me the link too pls..

Dev said...

Find the driver form here Java JDBC Driver implementation

Dev said...

Send me your code ill check and let you know

Post a Comment