Java with MS Access database

MS Access database is available with all MS (Microsoft) office. Its a very simple database to use for small business or home maintenance. Here the demonstration for using MS Access with java. Open MS Access and create a new data with name nsu and save that. Then use the following java code in the same directory, compile and run. It will create a new table named student, insert data and retrieve data from the table.

import java.sql.*;

public class Testac
{
public static void main(String[] args){
try {

System.out.println(“Begining conn”);

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

String accessFileName = “nsu”;

String connURL=”jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=”+accessFileName+”.mdb;PWD=”;

Connection con = DriverManager.getConnection(connURL, “”,””);

Statement stmt = con.createStatement();

System.out.println(“Conn done succesfully”);

stmt.execute(“create table student ( Name string, ID integer )”); // create a student

stmt.execute(“insert into student values(‘Md. SHAHJALAL’, ’02223540′)”); // insert data into student

stmt.execute(“select * from student”); // execute query in table student

ResultSet rs = stmt.getResultSet(); // get any Resultt that came from our query

if (rs != null)
while ( rs.next() ){

System.out.println(“Name: ” + rs.getString(“Name”) + ” ID: “+rs.getString(“ID”));
}

stmt.close();
con.close();
}
catch (Exception err) {
System.out.println(“ERROR: ” + err);
}
}
}

By: Md. Shahjalal

About these ads

19 Responses to Java with MS Access database

  1. maas says:

    What a great lesson, but an error occurred while doing the lesson
    It begins the connection
    and then prints the connection done successfully and then prints the error

  2. Jalal says:

    Dear Maas!
    Here 2 statement for example
    stmt.execute(”insert into student values(’Md. SHAHJALAL’, ‘02223540′)”);
    for insert and for checking
    stmt.execute(”select * from student”); // execute query in table student

    after inserting data one time if u going to insert same (’Md. SHAHJALAL’, ‘02223540′) that will nt be allowed and error msg will be shown after insert just compile and run with

    stmt.execute(”select * from student”); // execute query in table student

  3. hafizstar says:

    hi…

    i’m very new to java programming…
    can this java publish to the web…i mean display in browser…such as IE…

  4. Yes if you have you install JVM for your brwoser.

  5. farid says:

    Hi
    I am facing a problem related to MS Access database connection in java.

    The error shows that: java.sql.SQLException:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

  6. Checkout your DB name and make sure DB is running before you run the code

  7. Tyson says:

    Where will that student database will be created

  8. Mark says:

    It should be noted by anyone using this example that this will NOT WORK on Windows Vista/Windows 7 64 bit editions, with 64 bit Java. This is because the 64 bit edition of ODBC has no driver for Access – there is only the 32 bit Access ODBC driver. Be aware of this.

    • james says:

      THANK YOU! IT ONLY TOOK ME 3 HOURS TO FIND THIS OUT! ^.^

    • Smauel Nii Odoi Devine says:

      Interesting Commnets and Samples here and am glad we all share to attain the common goal of experience and growth.

      Well Mark, though this is true it can easily be resolved by changing some parameters and resetting the focus of the target driver location of the odbc driver , which by default or installation setting is set to the 64 driver.

      However this can be done by simpy going to Control Panel, then access the Administrative Tools. Then Right-Click on the ODBC Manager and select Properties. Place this reference in the Target Area;

      “%windir%\SysWOW64\odbcad32.exe”

      which Replaces

      “%windir%\System32\odbcad32.exe”

      On the Properties Page, Change the focus of the targeted driver path from the 64bit (in the SysWOW64 folder) to 32bit (by accessing the System32 which houses all 32bit system files).

      As you are aware when the 64bit is in use, in the list of available datasource drivers only the default SQL Server driver would be available (in most cases).

      Try this and see if it works!

      SHALOM!

  9. Thank you for sharing the note. I didnt know this.

  10. Amitesh says:

    hi
    can u please also show how i can alter & update data in the MS Access table.when i am using statement.executeUpdate(“update tablename set field=value where “) then it is showing error.
    Also when i want to use executeUpdate(“alter table add column datatype “) it shows error that it cannot execute data definition statements on linked data sources.
    please help
    Thanks

  11. johnlewis says:

    hi? thanks for this sample code.may you please help me out in how to pull data from a enum field in mysql database to java.More to this the enum has more than one value.

  12. CHITHRA says:

    sir,
    how to update a particular value in the column based on certain condition in access using java. please show one example code for that…. please ..if u give means it s very much useful for developing my project.

  13. RajKumar says:

    its not working in jdk1.3
    plz if u r getting me
    plz give me appropriate code with instruction
    ur regards

  14. jual kaos,desain kaos,kaos murah,kaos bola…

    [...]Java with MS Access database « Programming and Algorithm Tutorial[...]…

  15. Thanks, I want to write a program to save and document classes, functions or code snippets and using a ms access db seems to be an easy way to do this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: