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 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 4: Perform SQL Operations This article is one of the series of Accessing Access 2007 on NetBeans 6.5 using JDBC. You can see the...
- 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 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 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 0x139c DBC 0x3ce8004
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).
March 14th, 2010 at 1:39 pm
hi!! i get an error when i run the program it says…[Microsoft][ODBC Microsoft Access Driver] Not a valid file name.
March 20th, 2010 at 7:16 pm
Hi jay
When i first ran my program it gave me that exact error and i realised that i have to correctly specify the file location in line 3 & 4:-
String url = “jdbc:odbc:Driver={Microsoft Access Driver ” +
“(*.mdb, *.accdb)};DBQ=C:\\Database\\Northwind 2007.accdb”;
you have to spercify where your database phisically sits in your hard drive, for example mine is:-
String url = “jdbc:odbc:Driver={Microsoft Access Driver ” +
“(*.mdb, *.accdb)};DBQ=C:\\Documents and Settings\\DoctorV\\My Documents\\My Access Db\\Northwind 2007.accdb”;
hope this helps..
thanks
April 12th, 2010 at 2:25 pm
hi, i follow this steps, but when im run this program, java tell me this:
SQL Exception: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Could not use ‘(unknown)’; file already in use.
April 18th, 2010 at 6:55 pm
Hi, I made it simple, Copy and paste the code an the errors are the same:
java.lang.ClassFormatError: Duplicate field name&signature in class file northwindsample/Main
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: northwindsample.Main. Program will exit.
Exception in thread “main” Java Result: 1
ODBC driver unadministrative tools are OK
May 8th, 2010 at 10:55 am
hai
Unexpected error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
my code is
Class.forName( “sun.jdbc.odbc.JdbcOdbcDriver” );
String db = “jdbc:odbc:Driver={Microsoft Access Driver (iti*.mdb)};DBQ=C:\\Java\\tomcat-6.0.20\\webapps\\test\\bharathi.mdb”;
conn = DriverManager.getConnection( db );
stmt = conn.createStatement();
stmt2 = conn.createStatement();
rs = stmt.executeQuery( “SELECT * FROM BHARATHI1TABLE” );
rs2 = stmt2.executeQuery( “SELECT * FROM BHARATHI1TABLE” );
am using netbeans 6.1
May 11th, 2010 at 11:09 am
Hi,
The problem should be on the driver name – “Microsoft Access Driver (iti*.mdb)”. How did you get the name?
June 5th, 2010 at 5:53 pm
i have tried according to given example on this site but i got this kind of exception….plz give me correct solution for it..
SQL Exception: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
June 7th, 2010 at 10:22 am
Hi, Vaishali
Try to follow the step on the comment no.2.
June 8th, 2010 at 11:43 pm
Exception:java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
My code:
package javaapplication1;
import java.sql.*;
/**
*
* @author sai saripalli
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Connection con= null;
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
String url = “jdbc:odbc:Driver={msdb ” +
“(*.mdb, *.accdb)};DBQ=C:/Logindatabase”;
con = DriverManager.getConnection(url);
}
catch(Exception e)
{
System.out.println(“Exception:” + e);
}
}
}
Can you please explain String url clearly? i have created data source name from control panel–>administrator—>Data sources for UserDSN & SysDSN. there i have selected already created MS Access file called Logindatabase.mdb;I am using netbeans 6.8 & MSAccess 2007.
June 18th, 2010 at 10:41 am
Hi, Sai
You don’t need to create any data source on that. You just check if there is a driver name which contains extension *.accdb on list of ODBC driver. And copy that driver name to attribute of String url (jdbc:odbc:Driver={…..}). For example, jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}
And the attribute DBQ of String url is the location of your Access 2007 database. For example, DBQ=C:\\Database\\Northwind 2007.accdb
July 14th, 2010 at 8:31 pm
Hi linglom,
Now I checked that there is a driver name which contains extension *.accdb on list of ODBC driver int my PC.
I have tried the follow source code, but I got “Exception:java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified” error message.
String strUrl=”jdbc:odbc:Driver={Microsoft Access Driver (*mdb,*.accdb)};DBQ=c:/mydb2007.accdb”;
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con=DriverManager.getConnection(strUrl);
…..
Would you tell me what’s wrong with my case?
July 15th, 2010 at 11:15 am
Hi, Jason
Is the exactly that you type in code? I see that you miss . (dot) on *mdb -> *.mdb
July 15th, 2010 at 1:07 pm
Hi linglom,
Thanks for your answer.
I’m so sorry for missed typing the dot in the my question.
In my source code I didn’t miss the dot.
Is there any other reason for the error message “Exception:java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified”?
July 15th, 2010 at 1:32 pm
To Jason,
I think the problem is that it can’t find the data source as you specify. Have you try to install 2007 Office System Driver: Data Connectivity Components? The download link is on comment No. 2.
You have to ensure that you type exactly as you saw on ODBC drivers list. If you miss a space, it won’t match.