Question

I hope someone can help me. I'm working on a simple application which connects with an SQLite database. Following is 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 load a user name and password in the database. The problem I have is that when I click on the button the following exception appears:

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

(I found an example about how to connect to an SQLite database in Java, the example I found works well)

This program I'm doing it in window builder (eclipse). I'm using the same driver I use in the example I found. I don't know if I have to use another driver. In fact, I've tried with different drivers but that message still appears.

Was it helpful?

Solution

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");

OTHER TIPS

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.

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").

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.

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

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

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

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) }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top