File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes New to JDBC - problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "New to JDBC - problems" Watch "New to JDBC - problems" New topic
Author

New to JDBC - problems

sharad bhai
Greenhorn

Joined: Jun 25, 2004
Posts: 5
import java.sql.*;

public class trial2{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.OracleDriver()");
Connection conn=DriverManager.getConnection("jdbc racle:thin:@URL,"USERNAME","PASSWORD");
}
catch(java.sql.SQLException e)
{
e.printStackTrace();
}
System.out.println("Connecting to Database");
}
}

I am getting the following error

unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName("oracle.jdbc.OracleDriver()");


I am absolutely new to JDBC and this is my first code. Absolutely no idea why this happens
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Hi Sharad,

Well what you have mentioned is not really a JDBC related error. it is related to the Core Java.

Class.forName Method throws a ClassNotFoundException, as mentioned during compile time it shows an error that you have mentioned. so all you probably need to do is catch and rethrow the exception or simply declare that you method also throws this exception.

Your code could compile without any errors, but the exception could persist at runtime, because the class name that you have mentioned ends with a "()".
sharad bhai
Greenhorn

Joined: Jun 25, 2004
Posts: 5
Thanks. What I did was took your advice and modifed my code to catch a general Exception e. Then did a e.getMessage(). There is an error showing up saying oracle/jdbc/OracleDriver(). Any idea what this means??

I have just downloaded the classes12.zip file and added it to my d: drive in a folder I created called d:\orcldrv.

I have also seen that in many forums they talk about CLASSPATH. I looked up my environment varialbes and could not find a CLASSPATH listed there. Any ideas...
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

hi Sharad,

First of all Class path is not by defalut present, and you can create the classpath environment variable and probably retstart the application ( this would also depend on the OS you are using).

if you are using an IDE these problems are taken care of atleast at
development time, you can try Eclipse if you are not already using one. ut since you said you are in the beginning stages, you can try that later.

but if you are using a typical "notepad", command window, then you could also set the classpath only for the time as long as the command window is running. by typing SET CLASSPATH = "ABSOLUTE CLASSPATH TO YOUR CLASSES12.ZIP". and a ; at the end if you want to add other class files.


hope this satisfies your understanding of the classpaths.

after you have resolved your classpath issues, then in your code just remove the () in the Class.forName, and see.

that is use Class.forName("oracle.jdbc.OracleDriver");

well if you want to avoid this error at runtime, you could also try to load the driver through the following code

DriverManager.registerDriver (new oracle.jdbc.OracleDriver());

though not advisable, this should work.

hope this info is sufficient
sharad bhai
Greenhorn

Joined: Jun 25, 2004
Posts: 5
First of all I had classes12.zip in my class path instead of classes12.jar. Got past the first line. Mdified the program as under

import java.sql.*;
import oracle.jdbc.*;

public class trial2{
public static void main(String args[])
{
Connection conn=null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("JDBC Driver Loaded");
String username = "system";
String password = "sys";
conn=DriverManager.getConnection("jdbc racle:thin:@<IP address>:5560:COFFEEBREAK",username,password);
System.out.println("Connecting to Database");
}
catch (SQLException e){
System.err.println("SQL state: " + e.getSQLState());
System.err.println("SQL error: " + e.getErrorCode());
e.printStackTrace();
}
catch(Exception e)
{
System.out.println("Error ---> " + e.getMessage());
}
finally{
try
{
if (conn != null)
conn.close();
}
catch (Exception ignoreThis)
{
}
}
}
}

I get the following error message


JDBC Driver Loaded
SQL state: null
SQL error: 17002
java.sql.SQLException: Io exception: Bad packet type
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:124)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:161)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:273)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:318)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:
343)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:147)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtensio
n.java:31)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at trial2.main(trial2.java:14)
Press any key to continue...
sharad bhai
Greenhorn

Joined: Jun 25, 2004
Posts: 5
Could someone please reply to this. I would really appreciate this. I really need this connection to work. Thanks.
Pooja Agarwal
Ranch Hand

Joined: May 19, 2004
Posts: 37
You need to change this line.



Give the IP address of the system on which the oracle is running.
if its on ur system give localhost as the IP adderss


and make sure the db name is COFFEEBREAK, and user name and pwd are correct.
Madeswaran Pachiyappan
Greenhorn

Joined: Jun 29, 2004
Posts: 7
Hi,
Two things U want to check.

1) Ensure that classes12.zip or jar file is set in classpath.
Go to command Prompt and type "set classpath" and enter.Now see that Ur classes12.zip or jar file is in classpath.
2) Use
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
instead of Class.forName
Enjoy.....


Madeswaran P,<br />Chennai,<br />TamilNadu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New to JDBC - problems
 
Similar Threads
ORA600 error with jdk1.4, ojdbc14.jar and oracle10.2.0.0
Exception while connecting to an Oracle database
What does the line Class.forName("oracle.jdbc.OracleDriver"); actually do?
Problems Verifying Oracle Drivers
Invalid operation for read only resultset: updateFloat