1

I want to use SQLite with my Java Servlet Server. So I wrote a singleton database Controller which I initialize in my init function inside my servlet. When I reach the following line

Connection connection = DriverManager.getConnection(url);

I'm getting this Exception:

java.sql.SQLException: No suitable driver found for jdbc:sqlite:database.db
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Controller.Database.SQLiteDataBase.getInstance(SQLiteDataBase.java:22)
    at Controller.Servlets.login.loginController.init(loginController.java:45)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1174)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1090)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:770)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:417)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

I tested to get a connection in a new project outside my servlet Application and everything worked fine.

I'm using Eclipse Jee Oxygen and used Add External JARs.. to add sqlite-jdbc-3.21.0.jar

What can I do to fix this problem? I already tried absolute and relative paths to the .db file.

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Barney Stinson
  • 862
  • 9
  • 25
  • You got this error on deployed project inside tomcat? is it a war? inside the war there's the sqllite jar? – Black.Jack Jan 30 '18 at 20:40
  • 1
    sorry im new to java / programming i dont really understand. im getting this error on my console – Barney Stinson Jan 30 '18 at 20:50
  • Related: https://stackoverflow.com/questions/28119328/why-do-i-need-to-call-class-forname-in-tomcat-web-application-even-if-i-use-a-jd – Mark Rotteveel Jan 31 '18 at 09:51
  • You should either put the Connector/J lib in the Tomcat `lib` folder, or - if deployed with your application - you need to explicitly load the driver using `Class.forName`, as automatic driver loading doesn't work for drivers not on the system classpath. However, you shouldn't be using `DriverManager`, configure a datasource instead. – Mark Rotteveel Jan 31 '18 at 09:59

1 Answers1

1

You need to add the sqlite-jdbc-3.21.0.jar to your server classpath, you can put it in the lib folder of your tomcat server, or you can put this jar in the WEB-INF/lib folder of your project.

Cesar Loachamin
  • 2,680
  • 4
  • 24
  • 33
  • 1
    sadly doesnt change anything (screenshot maybe i did it wrong https://image.prntscr.com/image/rE9lgF4LQCWUIB4yNPDQ-A.png ) – Barney Stinson Jan 30 '18 at 21:46
  • Could you plase try adding this code `Class.forName("org.sqlite.JDBC");` before the line `Connection connection = DriverManager.getConnection(url);` – Cesar Loachamin Jan 30 '18 at 21:56
  • Also could you please share the URL string that you're using to establish the connection – Cesar Loachamin Jan 30 '18 at 21:58
  • 1
    String url = "jdbc:sqlite:database.db"; Connection connection = DriverManager.getConnection(url); – Barney Stinson Jan 30 '18 at 22:16
  • 1
    after adding clss.forname... im getting java.sql.SQLException: opening db: 'database.db': Zugriff verweigert (german for access denied) – Barney Stinson Jan 30 '18 at 22:19
  • when i initialize my controller outside of servlets everything is working fine even w/o class forname etc :( – Barney Stinson Jan 30 '18 at 22:19
  • It could be then a problem on the path of your database could you please try using an absolute path to your database to see if it works `DriverManager.getConnection("jdbc:sqlite:C:\\path\\to\\your\\database\\file\\yourdatabasefile.db");` – Cesar Loachamin Jan 30 '18 at 22:27
  • its working thanks! any tipps where i should put my file? – Barney Stinson Jan 30 '18 at 22:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164215/discussion-between-cesar-loachamin-and-barney-stinson). – Cesar Loachamin Jan 30 '18 at 23:23
  • Great! I definitely I don’t put my dB file in the project path I woul create a folder like /opt/data or C:\data if you mind if that solves the problem could you please select this as the answer for your question – Cesar Loachamin Jan 30 '18 at 23:24