javasqlitemainclass

No main class found


Language: Java

Program: Connecting to a database

Question: I'm trying to connect the sqlite database by following TutorialsPoint tutorial but I keep getting the main class not found error.

Implementation: My code is below followed by my terminal commands and folder structure screenshot. But basically all my files are located in one folder including the sqlite jar file.

import java.sql.*;

public class Test {
 public static void main(String[] args) {

 Connection c = null;

 try{
   Class.forName("com.sqlite.JDBC");
   c = DriverManager.getConnection("jdbc:sqlite:test.db");
 } catch(Exception e) {
   System.err.println(e.getClass().getName() + ": " + e.getMessage());
   System.exit(0);
 }

 System.out.println("Opened database successfully!");
 }
}

Terminal Commands

javac Test.java
java -classpath ".;sqlite-jdbc-3.23.1.jar" Test

enter image description here


Solution

  • Your problem was that you're explicitly trying to load the class com.sqlite.JDBC, whereas the driver class name must've changed somewhere along the way.

    JDBC Type 4 drivers have added cleverness which allows you to specify only the connection URL, and the driver loads itself based on the beginning (i.e. jdbc:sqlite). No need to wonder what was the driver class's name.


    Rant unrelated to the issue at hand:

    Unfortunately people read old tutorials written by less than experts, so we constantly see Class.forName() being used, as well as the more serious issue, which is using Statement instead of PreparedStatement.