This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
I couldn't find out why it shows error. I have created table named books in Oracle priorly.
create table books(num number);
Then I wrote code in Java:
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(
"jdbc:oracle:thin:#CF:1521:orcl",
"scott",
"tiger");
Statement s=con.createStatement();
s.execute("INSERT INTO BOOKS VALUES(123)");
s.close();
con.close();
It shows error as "No suitable driver"
It seems that the oracle driver (v 1.6) is actually "called":
oracle.jdbc.OracleDriver
You need to set your classpath to point to the jar file. The one I used years ago was "ojdbc14.jar". Find the one suitable for you here.
If you are executing code from command line - make sure that the classpath is set using either $CLASSPATH environment variable or command line argument -cp [pathTo:]ojdbc14.jar or setting the ClassPath: attribute in Manifest.mf of the current executing jar
With Eclipse IDE : Check in the build path the presence of jar / or if build issues exist - check for them in the problems tab.
The version of the jar file (which can be determined from Manifest.mf file) will confirm if you are using the right package for the driver Check if your jar is not corrupt by doing a simple jar -tvf.
It shows error as "No suitable driver"
You apparently did System.out.println(e.getMessage()) instead of a e.printStackTrace(). Then you will indeed get that little information. But this message is recognizeable as being a SQLException which can basically have two causes:
The driver is not loaded.
The (wrong) JDBC URL didn't return true for Driver#acceptsURL() for any of the loaded drivers.
To fix 1, you need to ensure that you have a
Class.forName("com.example.jdbc.Driver");
in your code prior to DriverManager#getConnection() call and that you do not swallow/ignore any ClassNotFoundException which can be thrown by it by putting for example an empty catch block or just doing a System.out.println() instead of throwing it.
To fix 2, you need to ensure that the JDBC URL syntax conforms the one as specified in the JDBC driver documentation. In case of Oracle it is located here. Here's a quote:
In JDBC all url's begin with jdbc:protocol: This is the standard. After this is driver specific, and no two drivers are the same.
What is the form of a URL?
The general form of a URL is
jdbc:oracle:<drivertype>:<username/password>#<database>
The <drivertype> is one of
thin
oci
kprb
The <username/password> is either empty or of the form <username>/<password>
In your specific case, the URL looks okay, so you might have not loaded the right driver or plain ignored the ClassNotFoundException thrown by it.
Related
This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
Using Java, I get this error when attempting to connect to a mysql database:
java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/mysql at
java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MyTest1.main(MyTest1.java:28)
I'm using the mysql-connector-java-5.1.18-bin.jar driver. It is in my build path. I have restarted MySQL. I've also logged on from the command line with root and no password and it connected fine. I'm not currently seeing a port 3306 in netstat. Previously I was getting a different error (I didn't change the code). The error was "jdbc mysql Access denied for user 'root'#'localhost password NO"
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url, "root", "");
}
catch (Exception e){
e.printStackTrace();
}
In this particular case (assuming that the Class#forName() didn't throw an exception; your code is namely continuing with running instead of throwing the exception), this SQLException means that Driver#acceptsURL() has returned false for any of the loaded drivers.
And indeed, your JDBC URL is wrong:
String url = "'jdbc:mysql://localhost:3306/mysql";
Remove the singlequote:
String url = "jdbc:mysql://localhost:3306/mysql";
See also:
Mini tutorial on MySQL + JDBC connectivity
You have to set classpath for mysql-connector.jar
In eclipse, use the build path
If you are developing any web app, you have to put mysql-connector to the lib folder of WEB-INF Directory of your web-app
When using Netbean, go under project tab and click the dropdown button there to select Libraries folder. Right Click on d Library folder and select 'Add JAR/Folder'. Locate the mysql-connectore-java.*.jar file where u have it on ur system.
This worked for me and I hope it does for u too.
Revert if u encounter any problem
This error happened to me, generally it'll be a problem due to not including the mysql-connector.jar in your eclipse project (or your IDE).
In my case, it was because of a problem on the OS.
I was editing a table in phpmyadmin, and mysql hung, I restarted Ubuntu. I cleaned the project without being successful. This morning, when I've tried the web server, it work perfectly the first time.
At the first reboot, the OS recognized that there was a problem, and after the second one, it was fixed. I hope this will save some time to somebody that "could" have this problem!
A typographical error in the string describing the database driver can also produce the error.
A string specified as:
"jdbc:mysql//localhost:3307/dbname,"usrname","password"
can result in a "no suitable driver found" error. The colon following "mysql" is missing in this example.
The correct driver string would be:
jdbc:mysql://localhost:3307/dbname,"usrname","password"
i had same problem i fix this using if developing jsp, put mysql connetor into WEB-INF->lib folder after puting that in eclipse right click and go build-path -> configure build patha in library tab add external jar file give location where lib folder is
Just telling my resolution: in my case, the libraries and projects weren't being added automatically to the classpath (i don't know why), even clicking at the "add to build path" option. So I went on run -> run configurations -> classpath and added everything I needed through there.
( If your url is correct and still get that error messege )
Do following steps to setup the Classpath in netbeans,
Create a new folder in your project workspace and add the downloaded .jar file(eg:- mysql-connector-java-5.1.35-bin.jar )
Right click your project > properties > Libraries > ADD jar/Folder
Select the jar file in that folder you just make. And click OK.
Now you will see that .jar file will be included under the libraries. Now you will not need to use the line, Class.forName("com.mysql.jdbc.Driver"); also.
If above method did not work, check the mysql-connector version (eg:- 5.1.35) and try a newer or a suitable version for you.
I am trying to use the Squirrel SQL client to connect to a H2 server instance.
The problem I am facing is that when I hit the "connect" button of the server I want to connect to, then I am getting a:
ClassNotFoundException: org.h2.Driver
I thought it was because h2.jar was not in the classpath. Then I edited squirrel-sql.bar to try two things.
The first thing I tried is to add the JAR into the PATH like this:
SET PATH=%PATH%;C:\Program Files\squirrel-sql-3.5.3\plugins\h2.jar
Then I restarted Squirrel. Same error.
Then I tried adding:
set TMP_CP=%TMP_CP%;"C:\Program Files\squirrel-sql-3.5.3\plugins\h2.jar"
Then I restarted Squirrel. Same error.
When I look at the console, I can see that h2.jar is in the PATH and/or the SQUIRREL_CP but the same error occurs all the time.
I am wondering if I am not forgetting something bout how to install/configure SQuirrel and/or H2?
Rather than modifying the batch files every time you use a different driver just modify the drivers classpath through the GUI.
Double click on the appropriate driver in the Drivers tab then click on the "Extra Class Path" tab and add the driver jar.
You can find more information under "How to connect to a Database" in the SQuirreL section in the Help file. Press F1 in SQuirreL to see the Help file. You can see a screenshot at http://www.squirrelsql.org/screenshots/driver.html
I found the issue to my problem. I was linking to the h2 file in Squirrel SQL. However, I had to link to the h2 file from the H2 application... What I did to solve my issue is to add a line to the addpath.bat file such that the file looks like:
set TMP_CP=%TMP_CP%;%1
set TMP_CP=%TMP_CP%;"C:\Program Files (x86)\H2\bin\h2-1.4.181.jar"
Then I restarted Squirrel SQL and everything was working as expected!
I'm listing the exact steps I followed to add the driver as even though Colin Bell has already given a good answer, without following the final step 4. I still couldn't get this to work.
Download http://www.h2database.com/automated/h2-latest.jar and save it somewhere, in my case ~/drivers/h2-latest.jar
Edit the existing h2 driver.
On the 'Extra Class Path' tab add the driver
Click 'List Drivers' - org.h2.Driver should appear in the 'Class Name' field (as detailed in the link from Colin's answer: http://www.squirrelsql.org/screenshots/driver.html)
I was missing the final step of clicking 'List Drivers' since it already had org.h2.Driver in the 'Class Name' before I tried to add it. But without this step it was still giving the error:
Could not find class org.h2.Driver in neither the Java class path nor the Extra class path of the H2 driver definition:
java.lang.ClassNotFoundException: org.h2.Driver
I am using JAVA (with eclipse juno) and try to create an executable JAR file which include sqlite DB file.
I've try to get connection to the DB by this line:
DriverManager.getConnection("jdbc:sqlite:"+DataController.class.getResource("test.sqlite").getPath())
The DataController is a class that located where the sqlite located.
and i keep get an error:
java.sql.SQLException: invalid database address
Does someone can help and give step by step instructions about how to include sqlite DB inside an executable JAR file?
Apparently sqlite-jdbc can open resources on it's own. Per this thread https://groups.google.com/forum/?fromgroups=#!topic/xerial/Oayzj5nrJGk, add :resource to the path. So try the following:
DriverManager.getConnection("jdbc:sqlite::resource:package/test.sqlite");
or depends on version of sqlite
DriverManager.getConnection("jdbc:sqlite::resource:package/test.db");
Replacing package with the '/' separated path to the package this file is in.
Be aware that it will actually copy the file to a tmp directory.-
The ::resource way is right. And these explanations will help if you are using ::resource but still get errors like resource database.db not found: java.net.MalformedURLException: no protocol: database.db like verdana.
Most common anwsers are:
DriverManager.getConnection("jdbc:sqlite::resource:path/to/database.db")
However the path/to/database.db must be the exact path(the Path in Real File System but not in Jar) to your jar file.
I recommend to use getClass().getResource():
DriverManager.getConnection("jdbc:sqlite::resource:" +
getClass().getResource("/path/to/db/in/the/jar/file"))
NOTE:the /path/to/db/in/the/jar/file part must start with /
I'm not sure if this has changed in recent JDBC versions, but after fiddling with it for about an hour and getting various exceptions (MalformedURLException and "Database has been closed"), I found that the following worked for me:
DriverManager.getConnection("jdbc:sqlite::resource:file:/path/to/database.db")
The :file: portion seems to be missing from other answers and I couldn't get it to work without it.
Also, note that the
/path/to/database.db
is the absolute path starting from the root of the .jar file, rather than a normal resource root.
jdbc:sqlite::resource:notes_app.db
worked for me. My database(notes_app.db) was in the root of generated jar file.
public class Database {
public static Connection con () throws SQLException {
String url = "jdbc:sqlite::resource:notes_app.db";
return DriverManager.getConnection(url);
}
}
My notes_app.db is in the root of generated jar. I'm developing with maven and notepadd++
I have written a code to check whether connection is successful or not.
But its giving error.
I have a oracle 10g express edition instsalled on my computer.
try{
String url="jdbc:oracle:thin:#//localhost:1521:XE";
String driver= "oracle.jdbc.driver.OracleDriver";
Class.forName(driver);
System.out.println(" Driver loaded ");
Connection con = DriverManager.getConnection(url,"system:,"manager");
System.out.println("Connection Successful");
} //catch block
The error given is:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Thanks for help.
you need to add oracle jdbc driver (jar) to your class path.
Looks like the JAR file containing the oracle.jdbc.driver.OracleDriver class is simply not on your classpath. Find it and fix that problem by adding the location (e.g., via the -cp option to java; the details of how to fix it will vary by the kind of application you're building).
Add ojdbc.jar in classpath.
Check this for How to Add JARs to Project Build Paths in Eclipse
You have to put the oracle_jdbc.jar file in the same folder of your code, or anywhere else and add that folder to your classpath.
I am trying to have an OSGI bundle access a MYSQL DB, using Eclipse as my IDE (Windows 7 x64). I am able to load the jdbc connector. The actual .jar is placed in all \bin folders in the java install directories, along with the \bin folder of the bundle. I have set the environment classpath variable to this folder also. I have an error stating that the driver is not suitable. I know OSGI has some issues with drivers etc. Can someone recommend a way to circumvent this?
ClassLoader DBHCL = ClassLoader.getSystemClassLoader();
DBHCL.loadClass("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver", true, DBHCL).newInstance();
System.out.println("Class Loaded");
//DriverManager.getDriver("jdbc:mysql://localhost/timedb");
//System.out.println("Driver Gotten");
conn = DriverManager.getConnection(URL + DBName,username,password);
System.out.println("Connection Created");
stmt = conn.createStatement();
System.out.println("Statement Created");
connFlag = true;
Console Output, Error: osgi> start 7 Data Base Service (MYSQL) Starting Class Loaded No suitable driver found for jdbc:mysql://localhost/timedb Exception in thread "Thread-1" INSERT INTO appliance1...
Does anybody have any insight into this problem?
I have tried making a separate bundle solely for the jdbc driver and exporting/importing this to the appropriate bundle, but no luck.
Thanks
In your code snippet, you get the SystemClassLoader, and you ask it for the "com.mysql.jdbc.Driver". Given that that call doesn't give you a ClassNotFoundException, we can conclude the system classloader can find the class for you; the driver will then register itself to the DriverManager.
However, you don't see the same DriverManager that the MySQL driver does! The MySQL driver sees the one from the system classloader, but your code (conn = DriverManager. ...) uses the one from the bundle's own classloader. These are two different classes, hence, no suitable driver is found.
My solution would be to not use the SystemClassLoader (which you shouldn't do in OSGi anyway, unless you know exactly what you're doing), but use the bundle's classloader. So, I would
not put the MySQL jar on the system classpath, but let OSGi do the hard work. You can put the jar in a bundle, and put the jar on the Bundle-ClassPath. You can then choose either to keep it private to your bundle (if you're the only one using it), or export the packages.
In stead of using the system classloader, use the bundle's classloader. This can be as simple as using Class.forName("com.mysql.jdbc.Driver"); this will do the right thing.