File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes IDEs, Version Control and other tools and the fly likes ClassNotFoundException while trying to connect to Oracle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "ClassNotFoundException while trying to connect to Oracle" Watch "ClassNotFoundException while trying to connect to Oracle" New topic
Author

ClassNotFoundException while trying to connect to Oracle

Kiran kk
Greenhorn

Joined: Jun 17, 2011
Posts: 9
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

Joined: Mar 02, 2005
Posts: 443
    
    1


Which line throws ClassNotFoundException?
Kiran kk
Greenhorn

Joined: Jun 17, 2011
Posts: 9
Raj Kamal wrote:
Which line throws ClassNotFoundException?


at this line

Class.forName("oracle.jdbc.OracleDriver");
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

You probably need to put the right driver jar on your classpath.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Kiran kk
Greenhorn

Joined: Jun 17, 2011
Posts: 9
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

Joined: Sep 07, 2004
Posts: 781

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

Joined: Jun 17, 2011
Posts: 9
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

Joined: Aug 06, 2010
Posts: 543
    
    4

Then you have a problem connecting. Check if all your data is correct... Username, password, driver string, hostname, and port.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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

Joined: May 14, 2004
Posts: 516
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

Joined: Jun 17, 2011
Posts: 9
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

Joined: Jun 17, 2011
Posts: 9
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

Joined: Aug 06, 2010
Posts: 543
    
    4

try using your real IP address instead of localhost.
Kiran kk
Greenhorn

Joined: Jun 17, 2011
Posts: 9
Dieter Quickfend wrote:try using your real IP address instead of localhost.


Thank you very much... It worked!!!The ping succeeded
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

You're welcome.
Kiran kk
Greenhorn

Joined: Jun 17, 2011
Posts: 9
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

Joined: May 14, 2008
Posts: 5845
    
    7

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.


JBoss In Action
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassNotFoundException while trying to connect to Oracle