Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassNotFoundException while trying to connect to Oracle

 
Kiran kk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi my program is unable to connect to the database it shows the ClassNotFound Exception
Below is the Program

package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ManageConnectionClass {
private String dbURL="jdbcracle:thin:@localhost:1521:ORCL";
private String dbUserName="system";
private String dbPassword="default";

public Connection getConnection(){
Connection conn=null;
try{
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("Class not found");
System.out.println(e);
}
conn=DriverManager.getConnection(dbURL,dbUserName,dbPassword);
}
catch(SQLException e){
System.out.println("Could not connect to DB");
}
return conn;
}
public void putConnection(Connection conn){
if(conn!=null)
{
try {
conn.close();
}
catch (SQLException e)
{
System.out.println("Could not close the connection");

}
}
}


public static void main(String args[]){
try {
System.out.println(new ManageConnectionClass().getConnection());
} catch (Exception e) {
System.out.println("Exception"+e);
}

}
}
and i'm invoking this using the main method......... I wasted a whole day for this .... please help me out...
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Which line throws ClassNotFoundException?
 
Kiran kk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Kamal wrote:
Which line throws ClassNotFoundException?


at this line

Class.forName("oracle.jdbc.OracleDriver");
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add the Oracle JDBC driver jar to your class path. When you use this in your servlet the driver jar must be placed in the WEB-INF/lib directory of your war file.
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to put the right driver jar on your classpath.
 
Kiran kk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sabre wrote:You need to add the Oracle JDBC driver jar to your class path. When you use this in your servlet the driver jar must be placed in the WEB-INF/lib directory of your war file.


I'm using eclipse galileo.... still do i need to add? I added the classes12.jar file in my database connections....even the pinging also failed....
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiran kk wrote:

I'm using eclipse galileo.... still do i need to add? I added the classes12.jar file in my database connections...

I no longer use Eclipse so I don't know the proper approach to this.

.even the pinging also failed....


I don't understand. How were you 'pinging'?
 
Kiran kk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm using eclipse galileo.. In that i created a new database connection selecting Oracle.... In the new Driver definition(oracle thin driver 10) i made modifications like removing the classes14 jar,adding the classes12 jar, setting the SID,username ,password... i made these modifications in the properties also... When i click the test connection button it says the ping failed
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you have a problem connecting. Check if all your data is correct... Username, password, driver string, hostname, and port.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dieter Quickfend wrote:Then you have a problem connecting. Check if all your data is correct... Username, password, driver string, hostname, and port.


This may be true BUT the original ClassNotFound exception still points to a classpath problem and I suspect this should be addressed first.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Two things to check
1) Do you see the Class for which ClassNotFound is thrown in the JAR that you added in Eclipse
2) Right click the java project and check the Java Build Path. It should have correct location of the JDBC driver's Jar file.

Regards,
Amit
 
Kiran kk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dieter Quickfend wrote:Then you have a problem connecting. Check if all your data is correct... Username, password, driver string, hostname, and port.


Username:system
password:default
driver string:jdbcracle:thin:@localhost:1521:ORCL
hostname:ORCL

the username and password are working as i checked that with my oracle sql puls..
 
Kiran kk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit punekar wrote:Hi,
Two things to check
1) Do you see the Class for which ClassNotFound is thrown in the JAR that you added in Eclipse
2) Right click the java project and check the Java Build Path. It should have correct location of the JDBC driver's Jar file.

Regards,
Amit


Thank you Amit For your Reply The ClassNotFound problem is resolved but still i got SQL exception saying that the network adapter could not establish the connection
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using your real IP address instead of localhost.
 
Kiran kk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dieter Quickfend wrote:try using your real IP address instead of localhost.


Thank you very much... It worked!!!The ping succeeded
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Kiran kk
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dieter Quickfend wrote:You're welcome.


This is my Servlet class


This is the model class



This is the class for getting connection to the Database




This is the code to add a movie to the pyramid table.. When i executed it including main method the insertion is happened but when i removed the main method it's showing an error "Class not foundjava.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
11:10:11,022 INFO [STDOUT] java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
11:10:11,023 INFO [STDOUT] Could not connect to DBjava.sql.SQLException: No suitable driver found for jdbcracle:thin:@192.168.1.10:1521:ORCL
11:10:11,024 INFO [STDOUT] Exceptionjava.lang.NullPointerException"


How can i overcome this error? help me


 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add the Oracle JDBC driver to your classpath. One way to do this in Eclipse is to add the JDBC JAR file to the project's build path.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic