Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations
Java, NetBeans, Programming September 7th, 2009
This article is one of the series of Accessing Access 2007 on NetBeans 6.5 using JDBC. You can see the index of this series at Accessing Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction
Perform SQL Operations
From Part 3: Create a Connection, I show how to create a connection from NetBeans 6.5 to the Northwind database of Microsoft Access 2007. On this post, you see how to perform basic SQL operations such as SELECT, INSERT, DELETE and UPDATE to the Customers table on Northwind database.
Retrieve data from database
Copy and paste the code below on main method between these lines:
System.out.println("Connected!");
and
con.close();
The Code
1 2 3 4 5 6 7 8 9 10 11 12 | Statement stmt = null; ResultSet rs = null; // SQL query command String SQL = "SELECT * FROM Customers"; stmt = con.createStatement(); rs = stmt.executeQuery(SQL); while (rs.next()) { System.out.println(rs.getString("Company") + " : " + rs.getString("First Name")+ " : " + rs.getString("Last Name")); } |
Code Explanation:
- Line 1-2: Declare some variables.
- Line 5: Define SQL query string.
- Line 6: Create a statement object for sending SQL statements to the database.
- Line 7: Executes the SQL statement and returns a single ResultSet object.
- Line 8-12: Iterative in the ResultSet object to show results on output window
Next, run the project. You see the result on the output window. It displays records from Customers table.

Back to top
Update data on database
To insert, update and delete records on MS Access 2007 databases, you can re-use the code from the previous section. But you need to replace the SQL statement and code on this line:
rs = stmt.executeQuery(SQL); while (rs.next()) { System.out.println(rs.getString("Company") + " : " + rs.getString("First Name")+ " : " + rs.getString("Last Name")); }
to
int rowsEffected = stmt.executeUpdate(SQL); System.out.println(rowsEffected + " rows effected");
Code Explanation:
Executes the given SQL statement which may be INSERT, UPDATE or DELETE statement and returns the row count for the update.
INSERT command
// SQL INSERT command String SQL = "INSERT INTO Customers (Company,[First Name]," + "[Last Name]) VALUES ('MyCompany','Linglom','My Last Name')"; int rowsEffected = stmt.executeUpdate(SQL); System.out.println(rowsEffected + " rows effected");
UPDATE command
// SQL UPDATE command String SQL = "UPDATE Customers SET [Last Name] = 'New Last Name'" + "WHERE [First Name] = 'Linglom'"; int rowsEffected = stmt.executeUpdate(SQL); System.out.println(rowsEffected + " rows effected");
DELETE command
// SQL DELETE command String SQL = "DELETE FROM Customers WHERE [First Name] = 'Linglom'"; int rowsEffected = stmt.executeUpdate(SQL); System.out.println(rowsEffected + " rows effected");
Summary
You can download the source code of this series at SampleAccess2007.java. The example code will create a connection to Northwind database on Microsoft Access 2007. Then, retrieve records, insert a new record, update and delete the record from Customer table. Here is output of the example code.

Back to top
Related post
- Accessing MySQL on NetBeans using JDBC, Part II: Perform SQL Operations From Part I, I have only established a connection with local MySQL. Next I’ll show how to retrieve and modify...
- Accessing SQL Server on NetBeans using JDBC, Part II: Perform SQL Operations From Part I, I have only established a connection with local SQL Server. Next I’ll show how to retrieve and...
- Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 1: Introduction Introduction Here comes again, a tutorial about accessing database on NetBeans. In 2007, I wrote two tutorials which are accessing...
- Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection This article is one of the series of Accessing MS Access 2007 on NetBeans 6.5 using JDBC. You can see...
- Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample Database This article is one of the series of Accessing MS Access 2007 on NetBeans 6.5 using JDBC. You can see...
Related posts:





October 2nd, 2009 at 11:35 pm
Thanks a lot !
this excellent tip was very useful !
December 7th, 2009 at 1:15 am
Very important series for a beginner.
really saved me a lot of time.
April 25th, 2010 at 1:53 pm
// SQL DELETE command
String SQL = “DELETE FROM Customers WHERE [First Name] = ‘Linglom’”);
int rowsEffected = stmt.executeUpdate(SQL);
System.out.println(rowsEffected + ” rows effected”);
——————————
in this code after ‘Linglom’” there is a closing brackets which is not required.
the correct code is
———————
// SQL DELETE command
String SQL = “DELETE FROM Customers WHERE [First Name] = ‘Linglom’”;
int rowsEffected = stmt.executeUpdate(SQL);
System.out.println(rowsEffected + ” rows effected”);
April 25th, 2010 at 11:17 pm
Hi, Madhu
Thanks for correcting the error, it was my mistake. Already fixed.
June 24th, 2010 at 3:56 pm
how to create separate class to cannect , reade , write , update the data base ????
August 1st, 2010 at 3:41 pm
This article is super best..helped me a lot..
August 20th, 2010 at 5:56 am
Thank you so much! This has been very helpful. I was so confused before I read this serie of articles!
August 27th, 2010 at 12:11 pm
How to crate complete java class to only the data base action such as read,write,update,and delete
January 9th, 2011 at 10:32 pm
hi sir….
this tutorial is very goood n helping for beginners thank you so much
March 10th, 2011 at 12:41 am
[Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for proces how to decide this error
March 26th, 2011 at 7:48 pm
why this is giving error cannot find method executeQuery()….please anyonr tell me..
March 26th, 2011 at 7:49 pm
why this is giving error cannot find method executeQuery()….please anyone tell me..
April 9th, 2011 at 9:16 pm
Hi all,
I liked the tutorial very much. It is very helpful and the first one which worked for me, however, I need one more help. I have a jFrame form which has 4 textboxes, one combo and one list box. I have 4 jbuttons (add, edit, delete, search). I would like to display data in the text boxes. I dont know how to proceed with this tutorial to achieve that. I am new in Java and NetBeans both.
It has been more than a month that I am trying something or the other from google but nothing works.
Please help.
August 2nd, 2011 at 2:24 am
thanks……….friend this is very helpfull for me……i learn lots of from this artical…………very good……
August 2nd, 2011 at 2:52 am
hi sir……….i have problem…i m trying to run insert query….then
SQL Exception: java.sql.SQLException: No ResultSet was produced
BUILD SUCCESSFUL (total time: 1 second)
why this error occure?
August 21st, 2011 at 2:33 am
sir i want to really thanks you U have solved my 5 days running problem.Hope to communicate further.
October 19th, 2011 at 7:41 pm
Thank you a lot Sir..
Helped a great deal.
November 19th, 2011 at 2:21 am
hi sirr..thanks a lott for this tutorial..can u tell me how do i display the same data into an html page now in netbeans…
November 19th, 2011 at 2:22 am
i need this data to be displayed into a webpage..can u pls help me..
January 8th, 2012 at 11:07 pm
This code help me to solve my problems.Thanks
January 20th, 2012 at 10:33 pm
thanks a lot …