SQLite can't connect to DB from Jar - java

I'm writing a java program which needs to load a local SQLite database. Everything works as expected until I package it and try it on another computer. The database queries seem to be the only problem. Attempts return "No suitable driver found for jdbc:sqlite:C:\Data\Test.db". The error makes me think it's accessing the SQL classes but is not properly loading the drivers themselves.
Windows 7
Coded in IntelliJ 12
Zentus' SQLite JDBC 3.7.2
Java SE 7 Update 25
String db_string = "C:\Data\Test.db";
try{
connection = DriverManager.getConnection("jdbc:sqlite:" + db_string);
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // 30 seconds
ResultSet rs = statement.executeQuery("select * from " + table);
while(rs.next()){
// Read the results
fileList.add(rs.getLong("modifieddate"));
}
} catch (SQLException e){
System.err.println(e.getMessage());
}
// Close the connection
finally {
try{
if(connection != null)
connection.close();
} catch (SQLException e){
System.err.println(e);
}
}
In IntelliJ I've built the Jar by creating a new Artifact and specifying the main class. The SQLite package I've used to read/write database information is extracted into the Jar by IntelliJ. When I open the Jar I can see my code and the SQLite code along with its JDBC drivers.
Manifest-Version: 1.0
Main-Class: com.myproject
The other computers I've tried it on are running Windows 8 and Windows 7. Both of them give the same errors.
What could possibly be wrong?

You're not loading the driver class's static initializer by calling Class.forName(...) and since you repacked the JAR, the SPI manifest needed to automatically detect the driver class probably isn't included.
Either manually invoke the driver or add the original SQLite driver JAR to your classpath instead of bundling it into a single JAR.

Related

I am Connecting to data Base After Making Jar File in java But "ClassNotFoundException" after making jar file Below is code

Here IS Code Any Suggestions When i am trying to access the data base
it shows error after making jar file
File f = new File("E:\\DB\\**\\***.mdb");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
JOptionPane.showMessageDialog(null, "Driver loded succesfully");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+f.getAbsolutePath();
connection = DriverManager.getConnection( database ,"","");
JOptionPane.showMessageDialog(null, "connection is"+connection);
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"erroris"+ e);;
}
ClassNotFoundException means that your program can't find the odbc driver class, try including the jar file in your path.
Its Only java Compatibility Issue . ODBC connection is not available in new versions of Java . So it work on old version on which i made. Thanks to
#Gord Thompson

Exception on loading JDBC-ODBC driver

I am getting java.lang.ClassNotFoundException on loading sun.jdbc.odbc.JdbcOdbcDriver using Class.forName().
I am using MySQL as Data Source and I have added Data Source Name in ODBC Data Source Administrator (on Windows 8).
Here is the code:
class Connect {
check() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Output:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Are you using Java 8? The class is no longer present there (more info). You could install Java 7 if you need to use it.
This happened to me once, and what i did was importing the mysql jdbc library that came with the product when i downloaded it, after that i used the driver as it is explained in the page:
http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html
hope this could help you

Exception: No suitable driver found for jdbc:mysql

I am trying to connect to a mysql database by using this simple code.
import java.sql.*;
public class OdbcAccessConnection_1 {
public static void main(String [] args) {
Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
// Connect with a url string
con = DriverManager.getConnection("jdbc:mysql://localhost/books","root","1234");
System.out.println("Connection ok.");
con.close();
} catch (Exception e) {
System.err.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
}
All it does is tell me if the connection is working. There is no problem with my database and this code/connection work on netbeans. The StackTrace i am getting is -
the java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at OdbcAccessConnection_1.main(OdbcAccessConnection_1.java:13)
I am working on 64 bit windows 7 and using 5.1 versions of the Connector/ODBC driver 64 bit. On the ODBC all seems to connect and the test was successful. But when i run the code i get the stack trace above. I am missing something very simple so any input and help would be very much appreciated.
Thank you:)
Go to Run menu in netbeans or whatever IDE you are using
=>Set Project Configuration then Customize.Then choose the Libraries on left dropdown menu
Add your appropriate driver file either jar or folder.
Click OK.
jdbc:mysql://localhost/books is a URL that you use to connect to MySQL directly, using the MySQL JDBC driver. The URL used by the JDBC/ODBC driver is different (see http://docs.oracle.com/javase/1.3/docs/guide/jdbc/getstart/bridge.doc.html).
The usage of this JDBC/ODBC bridge is discouraged, and should only be used to access a database that doesn't provide any JDBC driver. This is not the case of MySQL. Use Connector/J, their JDBC driver. Once you have this driver in your classpath, you can use the URL you're currently using, and remove the JDBC/ODBC driver from your classpath (and its loading from your code).
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root","1234");
This error creeped on me because I forgot to add the Class.forName line. The mysql driver jar was on the classpath but no one implicitly loads the driver class, so the session factory can't find any driver classes loaded. Thus the purpose of this line.
In your case, you're loading the wrong thing. It should be Class.forName("com.mysql.jdbc.Driver") if you intend to use it with a jdbc:mysql:// connection.
best solution bhai logo -:
go to JCreator configure menu then click to options then JDK profiles then double click to whatever the version u r using automatically mention there then click to add archive then go to that path -> C:\Program Files\MySQL\MySQL Tools for 5.0\java\lib\mysql-connector-java-5.0.4-bin.jar press ok.

Not SQL Driver Found - but Driver JAR File is implemented

I connect to a database:
void connectToDataBase(){
dataManager_ref = new DataBaseConfigurationManager();
try
{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dataBase","root","");
System.out.println("Connection successful");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
I implemented a JAR File for the driver:
mysql-connector-java-5.1.17-bin.jar
and imported it into the servlet
import java.sql.DriverManager;
this isnt the first time I use this database (tho the first time with Java EE web). This time I get the following exception:
No suitable driver found for jdbc:mysql://localhost:3306/dataBase
The application is running on a glassfish server 3.1, can I even use a database on a mysql server here? Can somebody help please
thanks in advance,
Daniel
You sometimes need to load the Driver class explicitly in order for the DriverManager to be aware of it.
Try this
Class.forName("com.mysql.jdbc.Driver");
Before you call the DriverManager
You can add a CLASSPATH variable in Environmental System variables, and set the path to your connector, path including name of connector.jar.
Also mysql-connector-java-5.1.17-bin.jar is showing some incompatibilities in accessing. it gave me lots of errors, so i had to go bac to 5.0.x versions

Java JDBC | Cannot run from console

I have developed a program where I do some database connections and send some queries with JDBC.
I have used MySQL, NetBeans 6.9 under Ubuntu 11.04 as platform. When I run the app from NetBeans, it works perfectly but when I try to run it from terminal I get SQL Exception. This is the function that produces that SQL Exception. The program terminates before "Establish is ending" line.
public Connection Establish(String iname, String ipassword) throws SQLException
{
System.out.println("Establish...");
if(conn == null)
{
conn = DriverManager.getConnection("jdbc:mysql://localhost/ANU",
iname, ipassword);
}
else
System.out.println("Connection Already Established!");
System.out.println("Establish is ending...");
return conn;
} // End of Establish
Make sure the MySQL Connector .jar file is in your classpath environment variable. IDEs, like NetBeans, sometimes help you out with putting .jar files in the classpath while you're in the IDE. You'll either need to run your app with the -cp option, or add it to your classpath environment variable.

Categories

Resources