1

I'm trying to edit an MS Access database using some Java code (running NetBeans 7.2.1). I set up the data source and linked it to my database ProjectDatabase using the ODBC tool and named the data source DB, then i run the following code:

import java.sql.*; public class NewMain {

public static void main(String[] args) {
    try{
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       Connection con = DriverManager.getConnection("jdbc:odbc:DB");
       Statement st=con.createStatement();
       String name="roseindia";
       String address="delhi";
       int i=st.executeUpdate("insert into user(name,address)      values('"+name+"','"+address+"')");
       System.out.println("Row is added");
       }
    catch(Exception e){
        System.out.println(e);
    }
}
} 

The code runs without and error and returns the "Row is added" message. The problem is that when I go back to view the database the changes have not taken effect. I have tried this with a code for deleting the data, also to no effect. Has anybody had this problem and knows how to solve it?

I'm running Windows 7 64-bit, Microsoft Office 64-bit with all the 64-bit drivers and I have been unable to find any mention of this problem through web searches.

Thanks in advance for any help =)

1
  • check how many rows get added by printing i after the executeUpdate() statement Commented Feb 22, 2013 at 10:08

1 Answer 1

2

First of all you are not closing the connection, so that is one problem. Also change your code to:

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con = DriverManager.getConnection("jdbc:odbc:DB");
   Statement st=con.createStatement();
   con.setAutoCommit(false); //Notice change here
   String name="roseindia";
   String address="delhi";
   int i=st.executeUpdate("insert into user(name,address)      values('"+name+"','"+address+"')");
   con.commit(); //Notice change here
   System.out.println("Row is added");
   con.close(); //Notice change here

This will commit the changes to access database, so now you should be able to see data in MS Access.

Read here to know more about best practices for Closing and Releasing JDBC resources

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

2 Comments

It will auto-commit anyway, agree?
@SudhanshuUmalkar, yes but for MS-Access it is always better to manually commit

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.