Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create a Connection
Java, NetBeans, Programming August 31st, 2009This article is one of the series of Accessing MS 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
Create a Connection
From Part 2: Prepare Sample Database, I show how to setup Northwind database for Microsoft Access 2007. Now let’s start programming. On this post, you see how to create a connection from NetBeans 6.5 to the Northwind database of Microsoft Access 2007 which located at “c:\database\Northwind 2007.accdb”.
Step-by-step
- Create a new Java Application project on NetBeans. Open NetBeans 6.5.1 and select File -> New Project.

- On New Project, select Java -> Java Application. Click Next.

- On New Java Application, type the project name “NorthwindSample” and click Finish.

- The project “NorthwindSample” is created.

- Let’s begin the programming part. First, I need to import some libraries.
import java.sql.*;
- Type the code below to the main method.
1 2 3 4 5 6 7 8 9 10 11 12 13
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb, *.accdb)};DBQ=C:\\Database\\Northwind 2007.accdb"; Connection con = DriverManager.getConnection(url); System.out.println("Connected!"); con.close(); } catch (SQLException e) { System.out.println("SQL Exception: "+ e.toString()); } catch (ClassNotFoundException cE) { System.out.println("Class Not Found Exception: "+ cE.toString()); }
Code Explanation:
- Line 2: Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); means load the JDBC-ODBC driver.
- Line 3-4: String url = “jdbc:odbc:Driver={Microsoft Access Driver ” + “(*.mdb, *.accdb)};DBQ=C:\\Database\\Northwind 2007.accdb”; declare a variable which store a string of the driver name for Microsoft Access 2007 and the location of the Northwind database.
- Line 5: Make a connection using information on the variable which was created before.
- Line 6: Display “Connected!” on console window.
- Line 7: Close the connection.
- Compile and run the project. You see the text “Connected!” on the output window. Otherwise, you should see an exception message.

What’s Next?
Now you have connected to the Northwind database of Microsoft Access 2007. Next, I will show how to perform some basic SQL operations such as SELECT, INSERT, UPDATE and DELETE to the Northwind database. See Part 4: Perform SQL Operations.
Related post
- Accessing SQL Server on NetBeans using JDBC, Part I: Create a connection Introduction This tutorial show you how to use NetBeans to connect SQL Server (2000 and 2005) by using Microsoft SQL...
- Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations This article is one of the series of Accessing 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...
- 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 MySQL on NetBeans using JDBC, Part I: Create a connection Introduction This tutorial show you how to use NetBeans to connect MySQL by using MySQL Connector/J, MySQL AB’s JDBC Driver...
Related posts:






September 23rd, 2009 at 11:04 pm
hi, I do all steps that you write here, I’m using NetBeans 6.7 and Microsoft Access 2007, but when I run the program, java tell me this error:
java.sql.SQLException: [Microsoft][Administrador de controladores ODBC] No se encuentra el nombre del origen de datos y no se especific? ning?n controlador predeterminado
sorry it’s in spanish, in English may be like this: DonĀ“t find the name of the origen and not specific a predeterminated controller, can you help me, thanks.
September 25th, 2009 at 10:59 am
Hi, Dorian
Try to verify if your driver name is the same as the example above, Microsoft Access Driver (*.mdb, *.accdb).
You can check the driver name by open Data Sources (ODBC) by Control Panel -> Administrative Tools -> Data Sources (ODBC). Then, click Add. You will see list of ODBC drivers, browse a driver name which contains extension *.accdb. If you can’t find one, you may need to install the driver. If you found and the driver name isn’t match as the example above, change it on the code (step 6 – line 3-4).
To install the driver, see 2007 Office System Driver: Data Connectivity Components.
February 10th, 2010 at 10:25 am
hi, i follow this steps, but when im run this program, java tell me this:
SQLException : java.sql.SQLException: No Suitable driver
hoping for your answer.
February 11th, 2010 at 10:01 am
Hi, Mark
Try to follow the step on the comment above (comment 2). You have to type exactly the same as the driver name show on ODBC Data Source.
February 26th, 2010 at 4:24 pm
Hi sir i followed the step to connect but i get this error below when i run the file.Can you please guide me how to solve this..
[Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0×1084 Thread 0×139c DBC 0×3ce8004
March 2nd, 2010 at 9:32 am
Hi, Veethia
Can you show your code?
I would suggest you to re-check the connection string to see if it type correctly (both driver name and database location).