0

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

  • I read some of the documentation on oracles site here, https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html, and it seems I'm doing the compilation correctly and adding the classpath correctly. –  Aug 21 '18 at 19:40
  • 1
    Is this on Linux? If so, then this is a duplicate of [Classpath does not work under linux](https://stackoverflow.com/questions/4528438/classpath-does-not-work-under-linux) (Use a colon as the separator). – rgettman Aug 21 '18 at 19:41
  • 1
    I thought you use colons between locations in the classpath argument? – Shawn Aug 21 '18 at 19:42
  • @rgettman yes I fixed that issues but now it's complaining that it can't find com.sqlite.JDBC class. I'll prpobably delete this question and look into why it can't find it. Thanks guys –  Aug 21 '18 at 19:44
  • 2
    BTW you don't need to use `Class.forName()` anymore (unless using some really legacy drivers). – Kayaman Aug 21 '18 at 19:45
  • @Kayaman Yes i did read a little about that on the doc. I figured I would just follow the tutorial and see why/what the preferred way to register the JDBC is. Thank you –  Aug 21 '18 at 19:50
  • @Kayaman I deleted Class.forName("com.sqlite.JDBC"); and it worked. Now I'm trying to figure out why. From what I read so far the drivers that you would get by initializing the JDBC are now loaded some other, more automatic way? Can you shed some light on this? –  Aug 21 '18 at 20:04

2 Answers2

0

My classpath option was incorrect. I was on linux and was trying to do:

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

the correct way was

java -classpath ".:sqlite-jdbc-3.23.1.jar" Test

colon not semicolon. Unfortunately now it's giving me and error" ClassNotFoundException: com.sqlite.JDBC;

I will look into this. Thanks for the comments which helped me find the error

  • I fixed the error regarding the com.sqlite.JDBC class not found. Unforutnely I don't know why that is working. I deleted the Class.forName() and it worked. Will look into why it worked –  Aug 21 '18 at 20:00
0

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.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • ok thank you. Just to make sure i beat this into my head. You're saying the fact that I was trying to load the class here...Class.forName("com.sqlite.JDBC");... is prone to errors because what probably happened is it got changed to something like...."com.sqlite.JDBCExample"...meaning it would never find it. –  Aug 21 '18 at 20:24
  • 1
    @Kapaz exactly. Now that I [checked](https://bitbucket.org/xerial/sqlite-jdbc), the driver class seems to be named **org** `.sqlite.JDBC`. So, close but no cigar. Note that the link is a bit outdated, but you see they're using `Class.forName()`. In the [more recent](https://github.com/xerial/sqlite-jdbc) example you'll see that they've left it out. – Kayaman Aug 21 '18 at 20:35