Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No suitable driver found (SQLite)

My connection code:

try {           
  Connection con = DriverManager.getConnection("jdbc:sqlite:myDB.sqlite");
  PreparedStatement pstm = con.prepareStatement("insert into hell(username,pssword) " +
"values ('"+tfUname.getText()+"','"+tfUpass.getText()+"')");

  pstm.close();
  con.close();
  JOptionPane.showMessageDialog(null,"Congrats, you have been registered succesfully");
  RegisterWindow rw = new RegisterWindow();
  rw.setVisible(false);
  pack();
  dispose();
} catch(SQLException ex) {
  setTitle(ex.toString());
}

This is just a window to insert a user name and password into the database. When I click the button following exception appears:

"java.sql.SQLException: No suitable driver found for jdbc:sqlite:C\\LoginJava2\\myDB.sqlite" 

I found an example of how to connect to an SQLite database in Java which works well. I'm doing this in WindowBuilder (Eclipse), using the same driver from the example. I've tried different drivers but that message still appears.

like image 634
Fenrir86 Avatar asked May 23 '13 23:05

Fenrir86


3 Answers

Your classpath is missing the jar(s) that contain the sqlite classes and driver. You need something like sqlite-jdbc-3.7.2.jar or your applicable version.

If you are sure the jar is there, try adding this line of code before you create a connection:

Class.forName("org.sqlite.JDBC");
like image 162
JamesB Avatar answered Nov 08 '22 14:11

JamesB


I got the same problem. I used maven and added dependency:

    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.15.1
        </version>
    </dependency>

It could be compiled and I got:

No suitable driver found for jdbc:sqlite:xx.db

I Checked the classpath and I was sure sqlite-jdbc-3.15.1.jar was there. I guess that for some reason, the Class was not loaded, I don't know why. so I added

Class.forName("org.sqlite.JDBC");

at the beginning of my code. It worked!

And ,I delete the line above. It still works! I cleaned the project and rebuild it, no more Class.forName() is needed!!! I still Don't know why. But the problem is solved. I think Class.forName() can be used for diagnose if the class you need is in the classpath.

like image 31
Lucas Liu Avatar answered Nov 08 '22 15:11

Lucas Liu


There is something more than just Class.forName.

In the case you did both things below: - Added the sqlite jar library to lib folder under your project, reference to it in the project build path. - Added Class.forName("org.sqlite.JDBC") statement. And the error message "No suit driver" still appear, its may caused by your database path. If you are using Windows: Instead of:

DriverManager.getConnection("D:\\db\\my-db.sqlite").

You should use:

DriverManager.getConnection("jdbc:sqlite:D:\\db\\my-db.sqlite").

The "jdbc:sqlite:" will do the trick.

If you are using Linux, just change the seperator character: DriverManager.getConnection("jdbc:sqlite:/your/somepath/my-db.sqlite").

like image 4
Andiana Avatar answered Nov 08 '22 15:11

Andiana