I'm currently trying to get a servlet to perform a SQL query and return the results on a web page. I am using SQLite and the JAR has been added to my project class path. However, Eclipse informs me that the code:
Class.forName("org.sqlite.JDBC");
has a "ClassNotFoundException"
When I run the code the server gives me a message about the ClassNotFoundException.
When I first looked up help I read that I should put the JAR into the "lib" folder of Apache, which I did.
I also clicked on the Servers in my Eclipse Project, clicked "Profile As -> Profile Configurations" and added the JAR to the Classpath as well.
I also tried adding the JAR to the WEB-inf/lib as well as another answer suggested, but the problem still occurs.
It makes sense that the JAR needs to be on the server somewhere so that the servlet can find the class, I just don't know how to fix the problem.
"I also tried adding the JAR to the WEB-inf/lib as well as another answer suggested, but the problem still occurs."
That is where the JAR should physiclly be.
Then go properties> build path > add JARS and add the JAR
If you are using eclispe you dont need to setup by yourself paths
you need to add jdbc driver to right folder and then
by right folder I mean (WebContent -> WEB-INF -> lib) you can make this without using eclipse
find your workspace and pase the driver right there, after this refresh(F5) your project in eclpise and it should be there
to be sure, create new DynamicWebProject and paste this code
import java.sql.*;
public class SQLiteJDBC {
public static void main(String args[]) {
Connection conn = null;
Statement stat = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:test.db");
conn.setAutoCommit(false);
System.out.println("sqlite opened successfully");
stat = conn.createStatement();
// sql is the query check it online on sqlite page or sql queries
String sql = "INSERT INTO 'name of db and parameters to eqecute'";
stat.executeUpdate(sql);
stat.close();
conn.commit();
conn.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
}
Related
This question already has answers here:
UCanAccess driver not found when converted to JAR file
(2 answers)
Closed 6 years ago.
I have developed a small game in which the text input by the user needs to be posted in a MS Access Database.
It all works fine in NetBeans but whenever I access it from the JAR file (which is ultimately what I need to hand to my client), it does not post anything to the database. In fact it returns the following error:
java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:\Users\Paul\Desktop\Spelli\RispostiDB.mdb
This is the relevant code:
public void postAnsDB()
{
String tableName = "Form"+studentGroup +"_"+studentSurname+"_"+studentName+"";
answerModifier();
try{
String strurl="jdbc:ucanaccess://C:\\Users\\Paul\\Desktop\\Spelli\\RispostiDB.mdb";
Connection conn=DriverManager.getConnection(strurl, "", "");
Statement stmt=conn.createStatement();
//Post Student Details to DB
String post = "INSERT INTO "+tableName+"(ID, responses, Correct_Response, Valid_Invalid, Marks) VALUES ('"+ansID+"', '"+answer+"', '"+correct+"', '"+valid+"', '"+marks+"');";
stmt.executeUpdate(post);
}
catch(Exception e)
{
System.out.println("Exception found in postAnsDB: "+e);
}
}
When you build your project in NetBeans it creates a dist folder in the home folder of your project, puts the JAR file for your code in that folder, and also puts the dependencies (JAR files for required components like UCanAccess, Jackcess, HSQLDB, and Apache Commons bits) into a subfolder named lib. You need to ensure that the lib folder gets copied to the target machine and placed in the same folder as your main JAR file. Look here for details.
You could go for the fat-jar approach.
You can see it here if you use ant.
If have a maven project you could have this plugin to create a fat-jar
The benefit of a fat-jar application is that you could have everything inside one jar and don't need to worry with the lib dir like it was mentioned in another answer.
Also be careful with hard-coded paths like:
String
strurl="jdbc:ucanaccess://C:\\Users\\Paul\\Desktop\\Spelli\\RispostiDB.mdb";
that you have in your code.
I've written a simple Program to test if the Oracle databse connection works. I'm not doing it in netbeans or eclipse, I'm just writing it in notepad++ and compilation,run by command prompt.
This is my program
import java.sql.*;
public class OracleCon{
public static void main(String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:ORCL","SYSTEM","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from user_details");
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getInt(2)+" "+rs.getString(3));
}
con.close();
}catch(Exception e){
System.out.println(e);
}
}
}
The error is ClassNotfoundexception oracle.jdbc.driver.oracledriver when running the program.
I searched and found lot of suggestion in stackoverflow, even I set the class path in environment variables too. Still not working. So I've no other choice than creating this thread. I know solving these kind of problems will be very frustrating than solving errors in programs. I've worked with mysql previously, but this is my first time in Oracle database. My installed version is 12c.
Download odbc jar from fllowing website:
http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html
and add into your build path.
put this jar in your projects's lib folder.
then right click the jar and click 'Add to Build Path' option [referencing Eclipse as an IDE]
Hope this resolve your query.
Solved the Problem. thank for your reply guys. I mistyped the name as ojdbc.jar instead of ojdbc6.jar. Wasted more than 3 hours for my silly mistake. grrrr.. I would not have noticed it If I didnt copied it for pasting it here.
I am experiencing the strange problem with No suitable driver found for jdbc:oracle:thin:#localhost:1521:XE
when i run JUNIT Test case of any DAO it is inserting fine. But when i run in server it is giving No suitable driver found for jdbc:oracle:thin:#localhost:1521:XE
i have added ojdbc14.jar to WEB-INF/lib folder.
here is the jdbc.properties
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:#127.0.0.1:1521:XE
jdbc.username=system
jdbc.password=pwd
please help me.
The problem is its not loading the Oracle Driver
added this in Controller, every thing is working fine.
static{
try {
Class.forName ("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
If your database connection module is in the server, you must add the driver jar to the container, and not deploy it with your WAR. It is a Classpath problem.
you have to import ojdbc6.jar to the libraries like i did
in the left side of netbeans theres a project bar
right click on the libraries and select "add JAR/FOLDER"
then select the ojdbc.jar
then voilaa :D
For beginners. Download the ojdbc14.jar file
then right click on the folder you are working on and go to its properties
Click library, Then find the downloaded ojdbc14.jar file to add it to the library
From there you are good to go
used this as the solution, worked like a charm!! thanks Ramesh Kotha
static{
try {
Class.forName ("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} }
In WEB-INF/lib ojdbc14-10.2.0.2.0, ojdbc8 or ojdbc6 should be there.
I have faced the same issue and resolved by this way.
See the attached.
I am developing a simple plugin for eclipse, which has one view connecting to Postgres. I have installed postgres which is running fine. I have a driver:
postgresql-9.0-801.jdbc4.jar
which was provided with the installation package.
Now I'm trying to connect to database using the code:
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return false;
}
Of course, I've added mentioned JAR to class path:
right click on project > preferences > Java Build Path
It is added to "Libraries".
Still I get an error:
java.lang.ClassNotFoundException: org.postgresql.Driver
I have also checked the box in "Order and Export" tab.
What am I doing wrong?
You said it's a plugin? Then you should NOT add it to lib path directly like that. You are developing an OSGi bundle and you must open MANIFEST.MF in the manifest editor, then at the Runtime tab, Classpath section, add your JAR. Also check at the Build tab that it's included in the build. Yeah, OSGi, another name for simplicity. Enjoy.
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.