Solved

Java JDBC - Oracle Connection Test Program

Posted on 2011-03-11
23
1,400 Views
Last Modified: 2013-11-23
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"?  
0
Comment
Question by:sdruss
  • 8
  • 7
  • 4
  • +1
23 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35112795
yes you need to include the driver jar
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35112816
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 200 total points
ID: 35112823
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
 
LVL 92

Expert Comment

by:objects
ID: 35113702
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
 
LVL 1

Author Comment

by:sdruss
ID: 35113770
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
 
LVL 1

Author Comment

by:sdruss
ID: 35113778
for_yan,


Getting java exception, "Class Not Found Exception", when calling Class.forName in my Java JDBC connection test.
0
 
LVL 92

Expert Comment

by:objects
ID: 35113782
> 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
 
LVL 47

Expert Comment

by:for_yan
ID: 35113876
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
 
LVL 1

Author Comment

by:sdruss
ID: 35114598
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35114821
If your real path is like that - did you use double quotes as Program Files has embedded space?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35114862
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 47

Expert Comment

by:for_yan
ID: 35114887

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
 
LVL 1

Author Comment

by:sdruss
ID: 35116070
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35116094
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
 
LVL 1

Author Comment

by:sdruss
ID: 35117382
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
 
LVL 2

Expert Comment

by:calboronster
ID: 35125495
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35125654
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
 
LVL 1

Author Comment

by:sdruss
ID: 35134392
 
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
 
LVL 2

Expert Comment

by:calboronster
ID: 35136163
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
 
LVL 2

Accepted Solution

by:
calboronster earned 300 total points
ID: 35136898
Am I not supposed to get the credits for my solution ?
Was this a mistake?
0
 
LVL 1

Author Closing Comment

by:sdruss
ID: 35177186
calboronster - No good example.  Just harshness and very critical.

for_yan -thanks for your patience and example
0
 
LVL 2

Expert Comment

by:calboronster
ID: 35178289
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now