This 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

  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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Reddit
  • Slashdot
  • Technorati
  • Google Bookmarks
  • Live
  • MSN Reporter
  • RSS
  • Twitter
  • email
  • Facebook
  • Netvibes
  • PDF
  • Yahoo! Buzz

Related post