Java JDBC - Oracle Connection Test Program

Attempting to compile a small java program that connects with Oracle thin, and then with an OCI connection.  Problem with call to, Class.forName:
    Class.forName("oracle.jdbc.driver. OracleDriver");
     DriverManager.getConnection( ........).;
 Oracle Database Server is 11.20.1.0.  What class do I need to include with my classpath?  I thought i probably needed to include "ojdbc5.jar"?  
LVL 1
sdrussAsked:
Who is Participating?
 
calboronsterConnect With a Mentor Commented:
Am I not supposed to get the credits for my solution ?
Was this a mistake?
0
 
objectsCommented:
yes you need to include the driver jar
0
 
for_yanCommented:
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
for_yanConnect With a Mentor Commented:
These are for 11:
http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
yes ojdc5.jar is for Java 1.5

ojdbc6.jar for java 1.6
0
 
objectsCommented:
for_yan,

Been meaning to have a chat and welcome you to EE :)
Feel free to contact me (see my profile) when you get a chance
0
 
sdrussAuthor Commented:
for_yan;

Thanks for your response.  Tried compiling with both ojdbc5.jar and ojdbc6.jar, with no luck.  I include the respective jar file in my javac compile classpath:

javac -classpath .../.../../ojdbc5.jar myJDBC.java
javac -classpath .../../../ojdbc6.jar myJDBC.java

Still no luck.  I am using Oracle Server 11.2.0.1.0, and Java 1.6.0_18

Thanks sdruss
0
 
sdrussAuthor Commented:
for_yan,


Getting java exception, "Class Not Found Exception", when calling Class.forName in my Java JDBC connection test.
0
 
objectsCommented:
> I include the respective jar file in my javac compile classpath:

doesn't need to be in your compile classpath
should be in your classpath when you run it
0
 
for_yanCommented:
Sure, you should check if those are available in your classpath at the runtime, when you run java -classpath ...
Just make sure that all those dots in your classpath are correct.
If you are in Unix check
 ls .../.../../ojdbc5.jar
from the same prompt
It seems to me that with such paths it is easy to make a mistake
Otherwise thin drivers are starightforward, used them this way many times
 
0
 
sdrussAuthor Commented:
for_yan,

My environment is Windows XP.  Directory path where ojdbc6.jar is correct - verified.  Could it be the CLASS_PATH system environment variable potentially screwing things up?  In my work environment don't have sysadmin privilege, so I thought specifying:
 javac -classpath c:\Progra.m Files\Java\..\..\11.2\jdbc\lib\ojdbc6.jar MyJDBC.java
 would take precedence?  Really not sure what to try next continue to get error with call Class.forName.  Is Class.forName actually in ojdbc6.jar?  Also, need to get OCI driver going - any advise?

Thanks sdruss.

0
 
for_yanCommented:
If your real path is like that - did you use double quotes as Program Files has embedded space?
0
 
for_yanCommented:
One other thing, please mind that Java is using CLASSPATH environment variable (not CLASS_PATH).

Also you should not be worrying about sysadmin, precedence, etc.
Sysadmin may control your general environment, true, however after you open
Dos window in most corporate setups inside this window you are free to set or modify your own variables which
will apply to subsequent life of this window.
In particular if after you open dos window you type, e.g. :
set CLASSPATH="C:\temp"
then your new CLASSPATH will override your previous CLASSPATH and subsequent java command will use this CLASSPATH

Yes, I guess comandline -classpath switch should override environment settings; but if I were you given
this problem at least for a start I would ensure that both point to identical paths; after it works once you
may remove environemnt and see that it still works, but for now keep them both the same.



 
 

0
 
for_yanCommented:

With OCI since Oracle realeased their InstantClient technology - it became
really easy.

Go to this site for download for your system (I guess you have windows 32 bit):
http://www.oracle.com/technetwork/topics/winsoft-085727.html

At the bottom of this page you'll see three easy installation steps.
It is indeed that easy.
0
 
sdrussAuthor Commented:
for_yan,

I am running Windows XP-64 bit.  Already have Oracle Client software installed.  Is this the primary difference in the call:

DriverManager.getConnection ("jdbc:oracle:thin:@" + host + ":" + port  + ":" + SID,  user, password);

                           versus

DriverManager.getConnection ("jdbc:oracle:oci:@" + host + ":" + port  + ":" + SID,  user, password);

Also, is the class, "Class.forName" in the ojdbc6.jar file?

Thanks!

Thanks sdruss

0
 
for_yanCommented:
Yes, I blieve it is the only differencr in the connection string.
Yes I beleiev the class you call on clas for name should be oresent in that driver jar
So thin classpath still does not work?
0
 
sdrussAuthor Commented:
for_yan,

Sad but true.  Correct it is not working.  

This is my exact compilation command:

         javac -classpath c:\app\oracle\sqldeveloper\jdbc\lib\ojdbc5.jar MyJDBC.java

And this is the error I get:

    43:  unreported exception java.lang.ClassNotFound; exception; must be caught or declared to be thrown Class.ForName("oracle.jdbc.driver.OracleDriver:)'
                                                                           ^
I could not verify that Class.forName exist in ojdbc5.jar file.  Did table of contents on jar file (i.e. jar tvf ojdbc6.jar) and did not see this class.  Should I have been able the class?  sdruss
0
 
calboronsterCommented:
Oh come on guys..really?

sdruss: are you really with java background? Class.forName is a core java package function. You really tried to search it in OJDBC5 library?

The solution is that there is nothing wrong with the libraries, just that the method that houses Class.forName need exception handling. (try-catch/throws). You should start using good IDEs like Eclipse to get around some silly mistakes we all do time to time.

I am so disappointed with the quality of this thread.

1. Please check the English in the thread it makes readers doubt the solutions (Mobile post, is it?).
2. The OJDBC library provides a lot of backward compatibility, hence simple applications run quite well even with classes12.jar although the JDK/JRE might be 1.6. (not recommended though)
0
 
for_yanCommented:
Sure, calboronster is right: this error message just says the you haven't enclosed ClassForName(..) statement within the
try/catch loop. Actually all your operations involving access to Oracle from ClassForName statement and creating connection up to
selecting from database, manipulating with ResultSet and
closing of the connection should be enclosed within the try/catch loop more or less like that:

try {


Class.forName(...);

Connection conn = ....

Statement stmt=conn.createStatement();

ResultSet rs = stmt.executeQuery("...");

while(rs.next()){
....
}

conn.close();

}  catch(Exception ex) {
System.out.println("Error " + ex.toString());
ex.printStackTrace();

}

Within the ojdbc6.jar library you would expect to see oracle.jdbc.driver.OracleDriver  class, not the Class.forName() method of the core java Class class;
 this latter class (clled Class) is one of the classes  of the java.lang.  package which comes with JDK (rather than with the Oracle driver).

0
 
sdrussAuthor Commented:
 
Wow, my bad.  Zero Java background - this is my first time out.  Tried to mimic an example line for line.  At any rate I did have the try-catch block surrounding call.

I found something else ... used the OracleDataSource class, and everything compiled and ran the first time.
0
 
calboronsterCommented:
Hi SDRUSS,

As I mentioned earlier, get some IDE ( like Eclipse). Saves a lot of time for most of us on quite silly issues.

And what? You used OracleDataSource??? You just made your code database dependent. But i am going to conveniently ignore this and continue.

Will really appreciate if you provide grade and points to the solution.

PS: And sorry for my outburst earlier it just feels good to be grammar nazi sometimes, isn't it.
0
 
sdrussAuthor Commented:
calboronster - No good example.  Just harshness and very critical.

for_yan -thanks for your patience and example
0
 
calboronsterCommented:
Hi sdruss,

I accept your complaint, but that was supposed to be tongue-in-cheek harshness, not to offend.
Genuinely sorry about that. (But get that Eclipse, I warn you...)
(for_yan : get that spellchecker enabled on you mobile ;) )

Thanks again for the grade, points and comments; keep the community ticking.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.