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 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 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 the...
- 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.
September 29th, 2010 at 11:17 pm
How do I connect the database with a relative address, the database file is in the same package, or just another package within the project.
October 4th, 2010 at 2:08 pm
hello sir
I have a problem in java netbeans that i want to display the records in the textboxes so that i have to enter the number in which the records of the particular number should be displayed.
could you please help me with syntax
October 12th, 2010 at 8:58 am
Hi
How to connect jsp to Ms access, I have created a jsp page now i need to connect with the ms access database. This is my code:
Login Page
Welome to Create Item Page
Add User
SNo
IP Address
Subnet Mask
VLAN ID
Allocated Hostname
Aggregated Switch
Headed Name
Remarks
Date Issued
In the first line i have an error called class,interface or enum expected.
Please kindly correct my code and send it to me ASAP.
Thanks
Lissy.
October 28th, 2010 at 3:39 am
We use Netbeans 6.5 at school. The teacher gave us this method of connecting to Microsoft Access Database and to use it.
Connection conn;
public DatabaseConnection()
{
connect();
}
void connect()
{
try
{
Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);
System.out.println (“Driver successfully loaded”);
}
catch (ClassNotFoundException c)
{
System.out.println (“Unable to load database driver”);
}
try
{
String database = “jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=”;
database += “TwinklerDatabase.mdb;DriverID=22;READONLY=true}”;
conn = DriverManager.getConnection (database, “”, “”);
System.out.println (“Connection to database successfully established”);
}
catch (Exception e)
{
System.out.println (“Unable to connect to the database” + e);
}
}
In Netbeans 6.5 the method works 100% but when I use the same method in Netbeans 6.9 it gives me the following
“Driver successfully loaded
Unable to connect to the databasejava.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified”
Now this is the same code that I used in Netbeans 6.5 and now it doesn’t work. I know there is another way of connecting to a database via using the “Services” and then “Databases” in the window on the left, but we are getting marks for the Database connection code. Is there any other program I can download so Netbeans 6.9 can use the code and can you please give me the “newer” method of connecting to a Database with Netbeans 6.9 via using the code.
I also have a problem with Netbeans 6.5 on my computer. It looks like it doesn’t find the JDK pakage, because when I want to open a project at home that I created at school it simply says it can’t find the project. The school uses Netbeans 6.5. Any solution to this problem will be appriciated. I have already tried downloading it directly from the site, both Netbeans 6.5 and JDK 6 update11, but it gives me the same error. I have tried installing JDK first and it didn’t work. I tried it the other way around and it still didn’t work. Installing newer versions of JDK also doesn’t work with the Netbeans 6.5. I am running on Windows 7 x64, if this is the problem to it, just let me know.
GleameXtreme
November 1st, 2010 at 6:37 am
Hi linglom,
My questions is same as K.Sunil Raghu Vamsee:
How do I connect the database with a relative address, the database file is in the same package, or just another package within the project??
Can u please answer this.
November 18th, 2010 at 2:11 am
I had to make a UserDNS to make it work….
Before that I had the same errors connecting to the DB
http://www.developer.com/db/article.php/10920_3571661_2/Connecting-to-a-Database-with-JDBC.htm
Explains setting up a UserDNS very nicely
November 29th, 2010 at 9:38 am
I am still unable to connect.
my code is
public static void main(String[] args) {
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
String url =”jdbc:odbc:Driver={Microsoft Access Driver(*.mdb, *.accdb)};DBQ=reqdb.accdb”;
Connection con = DriverManager.getConnection(url);
System.out.println(“True!”);
con.close();
}catch(SQLException e){
System.out.println(“SQL Exception: “+e.toString());
}catch(ClassNotFoundException ce){
System.out.println(“ClassNotFound Exception”+ce.toString());
}
}
Error i am getting;
run:
SQL Exception: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
BUILD SUCCESSFUL (total time: 0 seconds)
-I have downloaded the micrsoft driver, in comment2
-i have windows 7, MS access2007,netbeans 6.9.1
Please help, i need it.
December 18th, 2010 at 9:42 am
Hi, Rehman
Have you check your spelling of driver name is exactly match in ODBC? E.g. the white space should be the same as shown on ODBC.
If it still not fix your problem, try to follow link on comment 28.
December 29th, 2010 at 2:16 pm
i’am sorry i can’t ran connected access to netbean can you help ..
December 30th, 2010 at 3:29 am
hi linglom,
comment 28 ??? that is your last reply to me …..?
Does this has something to do with my Windows7 being 64x and Office being not 64x
January 3rd, 2011 at 9:48 am
Hi, rehman
Sorry, I’ve mistyped. I meant the link on comment 26.
I didn’t test it in x64 environment, but I don’t think it is the cause of problem.
February 22nd, 2011 at 3:39 pm
hi
i am working on project
i am using msaccess 2007
connection is established but its shows error connected database doesnt has table but its shows table in service ->databse->database->name->tables->table
i am using netbeans 6.9
March 7th, 2011 at 6:36 am
I have had the same problem with NetBeans 6. 7. 1
The error was in that had not respected the space among Driver and the parenthesis.
This code NOT RUN: Driver(*.mdb, *.accdb)
This code RUN OK: Driver (*.mdb, *.accdb)
Verify the spaces and the syntax carefully in the controller’s name.
José Manuel
April 7th, 2011 at 1:20 am
In response to the many people who seem to be having the following problem and SQL Exception:
SQL Exception: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
And in response to the person/people with the following question:
Does this has something to do with my Windows7 being 64x and Office being not 64x?
I was struggling with this problem too. The answer I found is thus. I am running Win7 64-bit with JDK 64-bit and Office 2007 32-bit. The problem is the interaction between 64-bit java app and 32-bit office. I couldn’t install the 64-bit office connectivity engine because office is 32-bit. Therefore you have to either uninstall office 32-bit and use 64-bit (as both cannot be installed side-by-side), or use 32-bit java instead, which worked for me on my system.
Therefore, 32-bit java & 32-bit office with 32-bit connectivity engine, or
64-bit java with 64-bit office and 64-bit connectivity engine.
Both work on 64-bit win7, OS irrelevant.
May 1st, 2011 at 1:04 am
Hi At first “thanks” for this Instructions.It Runs.
But I have a query,can u help me please.
the problem is:- “String url = “jdbc:odbc:Driver={Microsoft Access Driver ” +
“(*.mdb, *.accdb)};DBQ=C:\\Database\\Northwind 2007.accdb”; ” by this line you specify the path of the database but is there any process by which we don’t have to require to mention the path.
May 1st, 2011 at 7:50 pm
“Is there any process by which we don’t have to require to mention the path?”
If what you mean by this is that you do not wish to specify a path but would like to use the current working directory that the program is running in and just provide a file name of the database you wish to access then you can use the following string:
String url = “jdbc:odbc:Driver={Microsoft Access Driver ” +
“(*.mdb, *.accdb)};DBQ=” + System.getProperty(“user.dir”) + “\\Northwind 2007.accdb”;
System.getProperty(“user.dir”) – returns the current working directory that the program is running in as a String. The file name is then appended to this to complete the URL.
Is this helpful?
May 8th, 2011 at 5:08 pm
Hi,
my code :
import java.sql.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String url = “jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\db1.mdb”;
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverManager.getConnection(url);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(“Select * from db1″);
while(rs.next()){
System.out.println(rs.getString(1));
}
}
catch (Exception ex) { }
}
}
d MS Access database name is db1 and the table name is also db1. it is present in D drive.
The build is coming as “successful” but no resuts is being displayed. Please help. Do i have to save the access file with any particular extension? I also went to:
Control Panel>Administrative Tools>Data Sources(ODBC)>System DSN> Add> Microsoft Access Driver(*.mdb, *.accdb)> Data Source Name: db1> Select- db1.accdb> OK.
Still, no result!
Am i doing something wrong?
Thanks
May 8th, 2011 at 5:13 pm
sorry, i meant i put url as :
String url = “jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\db1.accdb”
Anything wrong? Do help
May 8th, 2011 at 6:25 pm
Hi Rose,
at first you put some data on this table then run this query in java..I think It will run
May 8th, 2011 at 6:50 pm
no, its not showing any result. therere 5 records in the database already.
i cant figure out what the problem is.
i also tried “jdbc:odbc:db1″ as url. still not working though the build is successful.
Thanks anyway.
May 8th, 2011 at 9:29 pm
Hi Rose
try url as such type
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
String url = “jdbc:odbc:Driver={Microsoft Access Driver ” +
“(*.mdb, *.accdb)};DBQ=C:\\Database\\Student.accdb”;
Connection con = DriverManager.getConnection(url);
System.out.println(“Connected!”);
con.close();
try this
hope this will be work
waiting for your reply
} catch (SQLException e) {
System.out.println(“SQL Exception: “+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println(“Class Not Found Exception: “+
cE.toString());
}
May 8th, 2011 at 9:31 pm
Hi Rose
try url as such type
try {
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
String url = “jdbc:odbc:Driver={Microsoft Access Driver ” +
“(*.mdb, *.accdb)};DBQ=C:\\Database\\Student.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());
}
try this
hope this will be work
waiting for your reply
May 10th, 2011 at 12:53 am
Hey, thanks. It worked!!
Can u help me with one more thing?
import java.sql.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverManager.getConnection(“jdbc:odbc:bful”);
Statement st=con.createStatement();
String name=”roseindia”;
int age= 17;
int i=st.executeUpdate(“insert into user1 (name1,age)
values(‘”+name+”‘,’”+age+”‘)”);
System.out.println(“Row is added”);
}
catch(Exception e){
System.out.println(e);
}
}
}
OUTPUT:
run:
Row is added
BUILD SUCCESSFUL (total time: 2 seconds)
The problem is am not getting the “added” row in the access database even though the build is “successful”. How do i get to see the row in the databse?
Help plz!
And am using MS Access 2007. Do i have to enable External Data or something in Access? if so, how?
May 20th, 2011 at 10:26 pm
can you help me please
error:
SQL Exception: java.sql.SQLException: [Microsoft][?????? ????? ODBC Microsoft Access] ???? ??????? ‘(??? ?????)’? ????? ??? ????????? ??????.
program is:
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());
}
thank you
May 21st, 2011 at 12:23 am
Hi
I think your JDBC-ODBC from control Panel (Administrative tools) is not rightly arranged.
at first check this
November 26th, 2011 at 9:33 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][Pilote ODBC Microsoft Access]Erreur g?n?rale Impossible d’ouvrir la cl? de Registre ??Temporary (volatile) Ace DSN for process 0×1470 Thread 0×1720 DBC 0x4566fc4
thx