Not SQL Driver Found - but Driver JAR File is implemented - java

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

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

SQLite can't connect to DB from Jar

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.

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.

java.sql.SQLException: No suitable driver on Mac OS X Attempting to use Derby

I am getting a java.sql.SQLException: No suitable driver when I am trying to connect to a database with Java. I am on Mac OS 10.5 using the NetBeans IDE. It seems to be having trouble with the EmbeddedDriver, but I'm not sure what I am missing:
public class A
{
Connection conn = null;
public A(String URL, String username, String password) throws SQLException
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
conn = DriverManager.getConnection(URL, username, password);
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
invalidate();
}
catch (ClassNotFoundException classNotFound)
{
classNotFound.printStackTrace();
invalidate();
}
}
}
"No suitable driver" usually means that the URL you've supplied to connect has incorrect syntax. What is your URL?
The server version would have a host and port; I believe the embedded URL should be "jdbc:derby:flixnet", according to these docs: http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
Use "org.apache.derby.jdbc.ClientDriver".
As i see you are accessing a derby server not an embedded database.
It is declared as a constant here:
final String DATABASE_URL = "jdbc:derby://localhost:1527/flixnet";
Don't ask about the name... I got this URL by right-clicking the database in NetBeans and then going to Properties -> Database URL.
And I have added the derby.jar and derbyclient.jar files from /Applications/NetBeans/glassfish-v3-prelude/javadb/lib/derby.jar and derbyclient.jar from the same directory.
Did you add the derbyclient.jar to the "Libraries" folder in your Netbeans project?
I had the same problem.
Try this:
Make sure you update your JDK and Netbeans to latest version
Right click your project's Libraries
Choosing "Add Library..."
Find and choose "Java DB Driver", then click "Add Library"
That's how I solved the problem, I hope this will help some new programmer like me:)
PS It will add "derbyclient.jar" for you

Categories

Resources