Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create Connection

This entry is part 3 of 4 in the series Accessing MS Access 2007 on NetBeans 6.5 using JDBC

Create a Connection

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

  1. Create a new Java Application project on NetBeans. Open NetBeans 6.5.1 and select File -> New Project.
    New Project on NetBeans
  2. On New Project, select Java -> Java Application. Click Next.
    Select Java Application
  3. On New Java Application, type the project name “NorthwindSample” and click Finish.
    Type Project Name for New Java Application
  4. The project “NorthwindSample” is created.
    NorthwindSample's Project
  5. Let’s begin the programming part. First, I need to import some libraries.
    import java.sql.*;

    Import Libraries

  6. 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.

    Code to Connect To MS Access 2007

  7. Compile and run the project. You see the text “Connected!” on the output window. Otherwise, you should see an exception message.
    Connected to MS Access 2007's Database

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.

Series Navigation<< Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 2: Prepare Sample DatabaseAccessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 4: Perform SQL Operations >>

56 Responses to “Accessing MS Access 2007 on NetBeans 6.5 using JDBC, Part 3: Create Connection”

  1. Dorian

    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.

  2. linglom

    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.

  3. mark

    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.

  4. linglom

    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.

  5. veethia

    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 0x1084 Thread 0x139c DBC 0x3ce8004

  6. linglom

    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).

  7. Jay-ann

    hi!! i get an error when i run the program it says…[Microsoft][ODBC Microsoft Access Driver] Not a valid file name.

  8. Doctor

    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

  9. manhtien

    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.

  10. Anarkin

    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

  11. honey

    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

  12. linglom

    Hi,

    The problem should be on the driver name – “Microsoft Access Driver (iti*.mdb)”. How did you get the name?

  13. vaishali

    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

  14. Sai

    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.

  15. linglom

    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

  16. Jason

    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?

  17. Jason

    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”?

  18. linglom

    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.

  19. 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.

  20. udhaya shankar

    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

  21. Lissy

    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.

  22. GleameXtreme

    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

  23. Rai

    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.

  24. rehman

    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.

  25. linglom

    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.

  26. rehman

    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

  27. linglom

    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.

  28. shonali

    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

  29. José Manuel

    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

  30. Greas3x20

    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.

  31. PrithviRaj

    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.

  32. Greas3x20

    “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?

  33. Rose

    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 :)

  34. Rose

    sorry, i meant i put url as :
    String url = “jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\db1.accdb”

    Anything wrong? Do help

  35. PrithviRaj

    Hi Rose,

    at first you put some data on this table then run this query in java..I think It will run

  36. Rose

    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.

  37. PrithviRaj

    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());
    }

  38. PrithviRaj

    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

  39. Rose

    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?

  40. mm

    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

  41. PrithviRaj

    Hi
    I think your JDBC-ODBC from control Panel (Administrative tools) is not rightly arranged.
    at first check this

  42. enis

    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 0x1470 Thread 0x1720 DBC 0x4566fc4
    thx

  43. Sumeet

    hi!!
    i am trying to build a database project. for that i have to create system dsn but whenever i am going to create it,i can’t see mdb driver option under system DSN option.
    can u please suggest the way to work on.

  44. Partha

    Sir
    I am using Netbeans 7.1.1 with MS Access 2010 and trying to connect to a database but I keep on getting this error:
    [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0×11f8 Thread 0x784 DBC 0x5176fec

    Please help

  45. chirag

    hi!!!
    I follow the above instruction for database connection with msaccess.. bt i had error saying

    SQL Exception :java.sql.SQLException: No suitable driver found for jdbc.odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\database\pro.mdb
    pls help me

  46. fifi

    i type all code above….all ok…but when i run the program…pop up this mcg at output…

    SQL Exception: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] ‘(unknown)’ is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

  47. Ajay Kumar

    Hello Sir, I have the following source code for Connect.java

    import java.sql.*;
    public class Connect
    {
    private static String info;
    public static Connection getConnection()
    {
    try

    {
    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
    String dataSourceName = “RVB.mdb”;
    String dbURL = “jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=RVB.mdb”;
    dbURL += dataSourceName + “;DriverID=22;READONLY=true)”;
    return DriverManager.getConnection(dbURL,””,””);

    }

    catch ( ClassNotFoundException cnfex )

    {
    cnfex.printStackTrace();
    info=info+”Connection unsuccessful\n” + cnfex.toString();
    }

    catch ( SQLException sqlex )

    {
    sqlex.printStackTrace();
    //info=info+”Connection unsuccessful\n” +sqlex.toString();
    }

    catch ( Exception excp )

    {
    excp.printStackTrace();
    info=info+excp.toString();

    }
    return null;
    }
    }

    I have done all the settings in control panel, ODBC data sources. But when I run this file , i get the following messages in output box:

    java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at Connect.getConnection(Connect.java:14)
    at LoginScreen.(LoginScreen.java:69)
    at Main.main(Main.java:271)
    java.lang.NullPointerException
    at LoginScreen.(LoginScreen.java:69)
    at Main.main(Main.java:271)
    java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at Connect.getConnection(Connect.java:14)
    at LoginScreen.login(LoginScreen.java:100)
    at LoginScreen$ButtonListener.actionPerformed(LoginScreen.java:148)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
    BUILD SUCCESSFUL (total time: 11 seconds)

    Please help!

  48. dilshi

    Class.forName(“sun.jdbc.odbc.jdbcodbcDriver”);
    String url = “jdbc:odbc:Driver={Microsoft Access Driver ”
    + “(*.mdb, *.accdb)};DBQ=F:\\New Folder (2)\\Database2.accdb”;
    con = DriverManager.getConnection(url);
    This is my code sample.I follow the above instruction for database connection.but i have error “java.lang.ClassNotFoundException: sun.jdbc.odbc.jdbcodbcDriver”

Comments are now closed for this article. If you still have issue and would like to discuss, you can post in the forum or send me a message.