Use Sqlite JDBC when the programm is exportet in JARs (Intellij) - java

I am writing my code in Intellij and created a Sqlite Database for some data. I connected it with the sqlite-jdbc-3.23.1.jar driver inside of intellij and can run it successfully there.
When I create a JAR (or multiple Jars because of the Modules) and start the programm, I get this error message:
java -jar ServerAbschalteHotline.jar java.sql.SQLException: No
suitable driver found for jdbc:sqlite:src/Resources/credentials.db at
java.sql.DriverManager.getConnection(DriverManager.java:689) at
java.sql.DriverManager.getConnection(DriverManager.java:270) at
com.company.SQLiteHandler.connect(SQLiteHandler.java:20) at
com.company.SQLiteHandler.getCredentials(SQLiteHandler.java:30) at
com.company.HttpsServer.makeServer(HttpsServer.java:64) at
com.company.Main.main(Main.java:21)
So for me it looks like the programm couldnt find the driver after it get exported. I checked that the .jar file is there (with all the other .jar files) and tried to include it as a libary too.
I guess I missed one place where I have to add the jar, so where is it?

In the end a clean jar (downloaded the same version of the jar from the same page) and a recompiling solved the problem. I dont know where the real problem was, maybe the jar contains error(s)

I don't know if this will help, but when creating the jar I make sure that the JDBC driver is included.

Related

How to connect to database without opening NetBeans?

I have created a Java application that uses a database, and I now want to build it into a jar file and make it so that the database works without NetBeans. I thought NetBeans would automatically include the database, but it doesn't.
The files that NetBeans include in the 'dist' folder when I build it look like this:
dist
lib
derby.jar (Executable Jar File)
derbyclient.jar (Executable Jar File)
README
WordFinder.jar (Executable Jar File)
Everything except the database-related code works when I launch the WordFinder.jar file after closing NetBeans.
I noticed that the database is using 'org.apache.derby.jdbc.ClientDriver', and I'm wondering, does that need to be 'org.apache.derby.jdbc.EmbeddedDriver' in order for it to include the database?
Are there any files that I need to include? I'm new to Java databases, so try to keep the instructions simple.
You need to start the database server in you computer, for doing that you need to go to your derby path (C:\Program Files\Java\jdk1.8.0_45\db\bin) and run the startNetworkServer.bat file
I may be a year late but seeing the comments, I'm assuming the issue hasn't been resolved yet. I had the same problem and would like to show how I worked around it. Note I'm not using an embedded derby database.
1) I changed my connection string. Instead of the usual "jdbc:derby://localhost:1527/myDB;create=true;...." , I put the exact location of the database which can be found usually under C:\Users\myPC\.netbeans-derby\myDB
2) Clean and build the project which should give you the "dist" folder which would include the jar file of the project together with the "lib" folder.
3) Start the StartNetworkServer.bat which is located in the bin folder under derby folder. A cmd would show that will say something "Apache Derby Network Server blah blah blah started and ready to accept connections on port 1527"
4) After doing those, run the jar file and it should run even with the netbeans IDE closed.
That's about it. That's how I got mine to work without opening Netbeans IDE. Hope it helps.

SQLite JDBC Class Not Found Exception

I've been trying to make use of the SQLite jdbc class from Xerial. Every time I try to run what I have, it fails, giving me the following error:
java.lang.ClassNotFoundException: org.sqlite.Driver
I'm using Ivy, and have the following in my ivy.xml file.
<dependency org="org.xerial" name="sqlite-jdbc" rev="3.7.2"/>
This includes the sqlite-jdbc-3.7.2.jar file in my class path. I've also tried manually adding it, to no avail.
The command that runs this also contains the jar on the command line:
java -cp ... sqlite-jdbc-3.7.2.jar ...
I still get the class not found exception on the following line:
Class.forName("org.sqlite.Driver");
I've tried searching around for this particular issue, but it seems most of the time it's resolved simply by including the jar in the class path, which shouldn't be the issue here, as Eclipse lists the jar in the class path.
Can you confirm that the jar version sqlite-jdbc-3.7.2 has class org.sqlite.Driver?
You can check it by unzipping the jar file and check if unzippied folder has directory structure org\sqlite\Driver.class?
I think the sqlLite Driver name is org.sqlite.JDBC and not org.sqlite.Driver . Source.

java sqlite exception

I made a Java application using netbeans and used sqlite as database
Everything works in netbeans but when i export the program to jar file and run it on another machine, an exception appeared when i try to connect sqlite database
The exception is about this line: Class.forName("org.sqlite.JDBC");
and the exception is: java.lang.ClassNotFoundException: org.sqlite.JDBC
I looked at all related threads in stackoverflow and no pure answer about this.
Thanks in advanced,
Just add the sqlite driver jar to your class path.
You application can't find the class org.sqlite.JDBC so it means a jar is missing in your class path.
Either include the sqlite jdbc driver in the jar file or make sure that you have it in your classpath. Add it as -classpath sqlite driver
Problem Solved
I had import the sqlite jar file for both compile time and run time. From netbeans -> project properties -> Libraries.
and Then i moved the exported jar file from dist folder in addition to lib folder to another machine .. and worked smoothly .. Thanks Guys up

Where to place the sqljdbc.jar

I'm developing this Java app, everything works fine in eclipse
but this program will not run with Apache Tomcat 7.0 because it cannot find the sqlserver driver.
This app is suppose to connect to a sqlserver 2005 database, but it will not get past the Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"), on the server side.
I know it's that line because I found:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
in my log file. I placed the sqljdbc.jar in every catalinaHome/lib, catalinaHome/shared/lib, and web-inf/lib.
I also edited the catalina.properties so it reads from the shared/lib folder.
I'm out of ideas, any help will be awesome!
UPDATE #1 - problem solved
I manually included the .class files into the war file and now it works.
CATALINA_HOME/lib should work. Have you restarted Tomcat after that though?
And SQL driver needs some DLLs, it seems. Do you have them setup correctly? The visible error may still be ClassNotFound because if class fails to initialize statically, it is viewed as class not found at all.
Before deploying the driver to Tomcat, make a small local test - a standalone Java class that loads the driver and tries to connect. May point you to the issue more clearly.
The correct place for it is application/WEB-INF/lib/. Note the upper-case WEB-INF.

What files needed for JDBC connection to MySQL?

I get the following error when attempting to connect to the MySQL database on my employer's local server:
Unable to load database driver
Details : java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
BUILD SUCCESSFUL (total time: 0 seconds)
I think it's pretty clear that this is because I don't have my files set up properly. Only problem is, I don't know what I need, and where it needs to go. Is the Driver a .class file? Where can I download it? Where in my filesystem (ubuntu, fwiw) do I put the file so that "Class.forName("com.mysql.jdbc.Driver").newInstance();" works?
Thanks for all your help, m8s.
Yes, it is a .class file that uses a others .class files. You call all these collections of files, a library and in this particular case, the library is also named: "jdbc driver".
These libraries are usually .jar files, so in your case you may try:
http://www.mysql.com/products/connector/j/
To download the MySQL JDBC driver.
You should put it in your classpath
You can download the connector here (the .jar you need is inside the .zip/.tar.gz):
http://dev.mysql.com/downloads/connector/j/
The .jar needs to be in a classpath available to your application.

Categories