0
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=student.mdb;DriverID=22;READONLY=true
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at withoutdsn.Main.main(Main.java:26)
Java Result: 1
Marc B
  • 340,537
  • 37
  • 382
  • 468

2 Answers2

0

In order to successfully execute a connection with a database through java,for example if you are using mysql follow the steps below:

Go to mysql website and download the appropriate driver for Java. Then go to Project -> Properties -> Java Build Path -> Libraries (in Eclipse) and click on "add external Jars". Add the .jar you downloaded. Before doing anything else you should be sure to set your connection right.

For example:

//the default port of mysql is 3306
String url = "jdbc:mysql://127.0.0.1:3306/mydb";
String login = "root";
String passwd = "toor";

Connection cn = null;
Statement st = null;
ResultSet rs = null;

System.out.println("Connecting to database..");

try {

    cn = DriverManager.getConnection(url, login, passwd);
    System.out.println("Database Connected");

    st = cn.createStatement();
    String sql = "SELECT * FROM impacts";
    rs = st.executeQuery(sql);
    while (rs.next()){
            //do something
      }
  }catch(Exception e){
       System.out.println("Exception");
  }finally{
       if (cn != null ){
       cn.close();
        }         
  }
Ioanna Katsanou
  • 330
  • 1
  • 4
  • 22
0

Are you using Java 8 and a JDBC-ODBC Bridge Driver? Bridge drivers are deprecated and not available in Java 8. See here.

If you want to run simple JDBC programs, instead of MS Access you could try using Java DB. Probably, Java DB works best with Netbeans IDE. There is nice tutorial here.