3

I have a Jbutton (GetDataFromDB) in a simple java application that is suppose to load the data from the database depicted in the path in the code below into a Jtable in the application.

Edited answer into code:

private void GetDataFromDBActionPerformed(java.awt.event.ActionEvent evt) {                                              
    Connection con;
    ResultSet rs = null;
    Statement stmt;

    try {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection("jdbc:odbc:Driver={MS Access Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\Bruger\\Documents\\Database11.accdb");

    stmt = con.createStatement();
    String query = null;
    query = "select * from cost";

    rs = stmt.executeQuery(query);

    i = 0;

    while (rs.next()){
        i = i + 1;
        jTable.getModel().setValueAt(rs.getString(1), i, 1);
        jTable.getModel().setValueAt(rs.getString(2), i, 2);
    }


    rs.close();
    stmt.close();
    con.close();
    } catch(Exception err){
    System.out.println(err.getMessage());
}

}  

When I press the button I get the following message in the run output window:

No suitable driver found for jdbc:odbc:Driver={Microsoft Access Driver (.mdb, .accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb

I have at the top of my code the import:

import java.sql.*;

I have also tried changing from "Microsoft Access Driver" to "MS Access Driver" but I get the same message in the run output window i.e.

No suitable driver found for jdbc:odbc:Driver={MS Access Driver (.mdb, .accdb)};Dbq=C:\Users\Bruger\Documents\Database11.accdb

I'm really thankful for all your help, input and feedback.

steinbitur
  • 331
  • 4
  • 8
  • 18

1 Answers1

7

Depending on the driver and If you are pre JDK 6**!

You need to register the driver.

Try adding:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

So:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\Users\\Bruger\\Documents\\Database11.accdb");

It's also worth mentioning you don't need to do this every time you get a connection, just once to make sure the class is loaded.

There are a lot of stackoverflow questions relating to this, but the reason for it is below:

Source - From The Java Tutorial:

In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. This methods required an object of type java.sql.Driver. Each JDBC driver contains one or more classes that implements the interface java.sql.Driver. ... Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)


On a related and very important note.

I would also recommend looking up how to handle connections. External Resources like database connections and cursors are easy to leak if you are not familiar with them. Look up 'try finally blocks', or more recently in java 7+ 'try-with-resources'.

GoldenJam
  • 1,380
  • 1
  • 10
  • 15
  • thank you for your answer GoldenJam, it solved this problem, thank you also for pointing to a search strategy to make more sophisticated connections – steinbitur Apr 24 '14 at 07:49
  • Now there is another problem. When I start the application and push the button. Nothing happens. The table is not populated like I was hoping it would do. I get this string you pointed out in the run output window i.e. *sun.jdbc.odbc.JdbcOdbcDriver* – steinbitur Apr 24 '14 at 07:53