3
votes

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.

1
Use Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); before con = DriverManager.getConnection(...) .moskito-x

1 Answers

7
votes

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