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

65 Comments

  1. Dorian September 23, 2009
  2. linglom September 25, 2009
  3. mark February 10, 2010
  4. linglom February 11, 2010
  5. veethia February 26, 2010
  6. linglom March 2, 2010
  7. Jay-ann March 14, 2010
  8. Doctor March 20, 2010
  9. manhtien April 12, 2010
  10. Anarkin April 18, 2010
  11. honey May 8, 2010
  12. linglom May 11, 2010
  13. vaishali June 5, 2010
  14. linglom June 7, 2010
  15. Sai June 8, 2010
  16. linglom June 18, 2010
  17. Jason July 14, 2010
  18. linglom July 15, 2010
  19. Jason July 15, 2010
  20. linglom July 15, 2010
  21. K.Sunil Raghu Vamsee September 29, 2010
  22. udhaya shankar October 4, 2010
  23. Lissy October 12, 2010
  24. GleameXtreme October 28, 2010
  25. Rai November 1, 2010
  26. Bman November 18, 2010
  27. rehman November 29, 2010
  28. linglom December 18, 2010
  29. faizal December 29, 2010
  30. rehman December 30, 2010
  31. linglom January 3, 2011
  32. shonali February 22, 2011
  33. José Manuel March 7, 2011
  34. Greas3x20 April 7, 2011
  35. PrithviRaj May 1, 2011
  36. Greas3x20 May 1, 2011
  37. Rose May 8, 2011
  38. Rose May 8, 2011
  39. PrithviRaj May 8, 2011
  40. Rose May 8, 2011
  41. PrithviRaj May 8, 2011
  42. PrithviRaj May 8, 2011
  43. Rose May 10, 2011
  44. mm May 20, 2011
  45. PrithviRaj May 21, 2011
  46. enis November 26, 2011
  47. Sumeet April 4, 2012
  48. Partha April 28, 2012
  49. chirag May 29, 2012
  50. fifi June 28, 2012
  51. Ajay Kumar July 2, 2012
  52. linglom July 6, 2012
  53. dilshi October 29, 2012
  54. RAJIB December 21, 2012
  55. shaurya February 5, 2013
  56. shaurya February 5, 2013
  57. sumit November 6, 2015
  58. linglom November 8, 2015
  59. prakash March 7, 2016
  60. linglom March 7, 2016
  61. Ady March 23, 2016
  62. linglom March 24, 2016
  63. meriem April 18, 2016
  64. linglom April 20, 2016
  65. sampath May 18, 2016

Leave a Reply