ClassNotFoundException when connecting to Oracle Database - java

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.

Related

Servlet and JAR on Apache Tomcat

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);
}
}
}

No suitable driver found for jdbc:oracle:thin:#localhost:1521:XE when running web application

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.

No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [duplicate]

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.

com.mysql.jdbc.Driver ClassNotFoundException

I'm getting ClassNotFoundException on Class.forname("com.mysql.jdbc.Driver")
I'm using Windows Vista 64-bit, Eclipse Galileo, GWT framework. I downloaded mysql-connector-java-5.1.6-bin, but what is the exact path I should put this file in? I'm getting this exception while I'm in gwt-projects, but in normal projects it works good. Any idea how that should be done?
Finally its worked, the problem was not in the classpath, but from the .jar file itself, im using mysql-connector-java-5.1.6-bin which was not working, but when i tried mysql-connector-java-3.0.17-ga-bin everything works good, i hope to fix the new version soon
anyway thanks BalusC for helping :)
Just put the JAR file in the runtime classpath of the application in question.
In case of a Servlet based webapplication, you normally put it in /WEB-INF/lib folder. It's by default covered by the webapp's runtime classpath.
Disable Google App Engine. Its a setting in Eclipse.
Google App Engine doesn't allow you to open Sockets. When you try to load the JDBC driver, it makes a socket connection in a static block. An exception in the static block leads to a ClassNotFoundException, which is what you are seeing.

Postgres JDBC connection in Eclipse Help

I'm trying to get a postgres jdbc connection working in eclipse. It would be nice to use the Data Source Explorer, but for now I'm just trying to get a basic connection. What I have done so far is download the postgres JDBC connector. I then tried two different things. First, Preferences-> Data Management, I tried to add the postgres connector. Second, I added the jar to my project and tried to load the driver using Class.forName("org.postgresql.Driver"); but neither worked. Does anyone have any ideas?
Thanks,
Charlie
This is how I have made a connection: (I do not know if this is "best practice", but it works.)
Importing the driver:
Right click on your project
Choose property
Choose Java build path
Choose Add external JARS.. and select the location to the JDBC driver.
Here is my code:
try{
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe){
System.out.println("Could not find the JDBC driver!");
System.exit(1);
}
Connection conn = null;
try {
conn = DriverManager.getConnection
(String url, String user, String password);
} catch (SQLException sqle) {
System.out.println("Could not connect");
System.exit(1);
}
The url can be of one of the following formats:
jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
I was also having this problem as well and Vjeux's answer helped point me in the right direction.
I have a local copy of Tomcat6 that was installed and is managed by Eclipse. It was installed into '$HOME/bin/tomcat6'. To get the PostgreSQL JDBC driver working I simply copied my postgresql.jar file into the '$HOME/bin/tomcat6/lib' directory.
Also, if you don't know where to get the driver from in the first place, try this. I'm running Ubuntu so I ran 'sudo apt-get install libpg-java' which installed the driver into '/usr/share/java/postgresql.jar' and so I just copied it from there.
I had the same problem using GWT.
I fixed it by copying the jar file inside the "lib" folder: (Project\war\WEB-INF\lib). When you add a jar to the Build Path it seems to do the link statically, however we want the lib at run time!
Hope it fixes your problem.
you can write this code in persistence.xml
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/yourDataBaseName"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="yourPassword"/>
Here's one way to get PostgreSQL connectivity to your application:
Get an instance of org.postgresql.ds.PGSimpleDataSource
Setup it with values matching to your database (see methods below)
Proceed using the DataSource as you would use any other, I'd assume at this point you'd be interested in the DataSource.getConnection() method.
The proprietary methods for configuring this particular DataSource are setServerName(), setDatabaseName(), setUser() and setPassword().
I wouldn't recommend doing this for anything else than testing though and it's possible your problem lies in the way you're trying to get an instance of the object using Class.forName() There's almost a dozen different ways to get an instance of an object with subtle differences, I suggest Googling for it since it is a subject a lot of people have already written about all over the Internet.

Categories