Why does Connection to SQLite fail although I added jar file to classpath (Eclipse)?

Matthias Neubert :

This problem is afflicting me for three days. Found a lot at Google but nothing helped for my issue: I can not establish a connection to a SQLite-Database. What I tried so far:

public class RegelfragenConnection {

    Connection con = null;

    public void connect() {
        try {
            Class.forName("org.sqlite.JDBC");
            String url = "jdbc:sqlite:C:/Webfreigabe/Regel.db";
            con = DriverManager.getConnection(url);
            System.out.println("connected");
        } catch (SQLException | ClassNotFoundException e) {
            System.out.println("not connected");
            System.out.println(e.getMessage());
        }
    }

    public void close() {
        [...]
    }

    public List<Frage> getRegeltest(int anzahlFragen) {
        [...]
    }
}

Of course I added the .jar file to the classpath. See eclipse-project-tree:

SQLite in classpath

This is the path where the DB is (C:\Webfreigabe):

enter image description here

The original database is Regelfragen.sqlite. I found a lot of descriptions where the ending is .db so I tried to make the same DB as Regel.db. But this does not work either.

The error I get is

not connected org.sqlite.JDBC
Dez 30, 2018 8:38:06 PM org.apache.catalina.core.StandardWrapperValve invoke
SCHWERWIEGEND: Servlet.service() for servlet [de.matthiasneubert.regelfragen.RegelfragenServlet] in context with path [/Regelfragen_Web] threw exception
java.lang.NullPointerException
    at de.matthiasneubert.regelfragen.RegelfragenConnection.getRegeltest(RegelfragenConnection.java:42)
    at de.matthiasneubert.regelfragen.RegelfragenServlet.service(RegelfragenServlet.java:24)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

It appears in the method getRegeltest when it tries to use the connection. The first two statements are the println from the catch clause.

This problem is weird what am I missing? I use Eclipse IDE for Enterprise Java Developers version 2018-12 (4.10.0), SQLite-jdbc-3.23.1, Tomcat v8.0 Server

Thank you for your help

MWiesner :

As pointed out correctly by @guleryuz in the question comments, you have to provide the database driver jar file via the WEB-INF/lib folder which you will find in WebContent directory of the project.

Note: You have encountered a java.lang.ClassNotFoundException: org.sqlite.JDBC with the original code posted in the question. It brings you to the core of the problem. General advice: Do not hide exceptions with "simple messages". Always print stack traces while you are still developing!

For this change you will have to refer to the sqlite-jdbc-<version>.jar via the Web-Library that manages the web/application container libraries (i.e., classpath of the web application).

This way, you will get the driver loaded at runtime and the call

con = DriverManager.getConnection(url);

will succeed in establishing a working Connection instance for your "Regel.db" database file.

Hope it helps and provides a solution for other readers of this question.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109561&siteId=1
Recommended