Unable to connect to database (No suitable driver found)
Asked Answered
B

8

37

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.

Bundle answered 23/5, 2013 at 23:11 Comment(3)
See here JDBC PreparedStatement ExampleSari
JDBC + SQLite: DriveManager does not load needed driverSari
That doesn't look like a valid url.Zoophyte
O
61

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");
Orlan answered 23/5, 2013 at 23:14 Comment(6)
it doesn't work with sqlite-jdbc-3.7.2.jar... and if I add the line "Class.forName("org.sqlite.JDBC");" appears an error in that line with this message: "Unhandled exception type ClassNotFoundException"Bundle
@Bundle simply surround it with a try{ .......}catch(ClassNotFoundException e){ ....}Austral
And make sure you have the sqlite-jdbc-xxxxxxx.jar file in WEB-INF/lib/ if you are running a server like openshiftAustral
It's curious that in my application, which I built as a Macintosh application bundle, I didn't need this line. But I did need it for the executable jar file used by other platforms. I don't know why, but it's good to know the behavior difference between the two platforms.Topnotch
i had the same issue it was relatedt o sqlite-jbdc-3.7.2 i have switched to "org.xerial:sqlite-jdbc:3.8.11" ( using gradle) and it works fineAnkylostomiasis
this answer really healped me, i put the sqlite-jdbc-xxx.jar in WEB-INF/lib/ and got the exception. but what does Class.forName("org.sqlite.JDBC"); do, why does it solve the problem. can anyone explain...Detergency
B
17

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.

Backstitch answered 9/12, 2016 at 1:55 Comment(2)
for me the problem appears again when removing Class.forName("org.sqlite.JDBC")Shieh
I had the dependency but still saw the error. Resolved after updating the version to 3.30.1.Substrate
S
6

Also check your database connection string references an existing database.

In the case you:

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

If the error message "No suit driver" still appears, it's most likely caused by your incorrect database path. Check that path exists.

A properly formatted connection string referencing an existing db file:

Windows

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

Linux

DriverManager.getConnection("jdbc:sqlite:/your/somepath/my-db.sqlite").
Slobber answered 4/4, 2017 at 9:42 Comment(0)
M
2

If you use Maven and want to build an executable jar, you could decide to import the content of the sqlite jar into your own produced jar:

<plugins>
  <!-- any other plugins -->
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <archive>
        <manifest>
          <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
          <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
          <addClasspath>true</addClasspath>
          <mainClass>MyPackage.Main</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>
</plugins>

You will not have to add specific classpath or implicit usage as proposed in the others answers.

Mountain answered 20/2, 2018 at 9:40 Comment(0)
C
2

Below is the link of central repository from where you can download latest driver files.

https://repo1.maven.org/maven2/org/xerial/sqlite-jdbc/

Make sure that you download sqlite-jdbc-version.jar file and not other files. Hope this might help

Cosgrove answered 18/2, 2021 at 10:41 Comment(0)
P
0

I was facing similar issues using a simple gradle configuration as follows

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'

    compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.23.1'

}

jar {
    manifest {
        attributes 'Main-Class': 'rewards.simulator.MainSimulator'

    }
}

I later found that gradle build was creating a jar which was not including any external dependencies. Following configuration is to be used to include all your dependent libraries in the resulting jar file, along with your source files, to create a fat-jar:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'

    compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.23.1'

}

jar {
    manifest {
        attributes 'Main-Class': 'rewards.simulator.MainSimulator'

    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
Profanity answered 6/8, 2018 at 15:23 Comment(0)
T
0

problem of version 3.7.2 try to install 3.21.0 it

Telegonus answered 9/7, 2022 at 16:56 Comment(0)
H
0

I have had the same issue with sqlite-jdbc-3.45.3.0.jar I solved it adding slf4j-api-1.7.36.jar Because that's how it mentions his documentation in the Usage section. Check here.

Hopson answered 6/5 at 23:55 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Sitzmark

© 2022 - 2024 — McMap. All rights reserved.