Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes FOR HELP: Failed to connect to oracle in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "FOR HELP: Failed to connect to oracle in servlet" Watch "FOR HELP: Failed to connect to oracle in servlet" New topic
Author

FOR HELP: Failed to connect to oracle in servlet

Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Hi everyone,
I got pretty frustrated while working on servlet and JDBC recently and hope some experts can help me out.
I am trying to connect to a oracle database using servlet, which is hosted by Tomcat, UNIX server.
I am no right to play with the server so I copy the whole directory of oracle thin driver to my servlet class directory from others.
I am not sure whether that is the right driver, but I have no problem to connect to the oracle in the stand along application (in main method) in UNIX. However, after I tried to put the same codes in the doGet() of my servlet and call it from the browser, I got error as "No suitable driver".
I tried to debug my codes, it appears the following codes are OK
try
{
driver = Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (java.lang.ClassNotFoundException e)
{
return "Driver not found";
}
Seems soemthing wrong with DriverManager.getConnection.
In addition, I got so frustrated that I wrote a server in the the same host and let the servlet in the same directory to open a socket to contact the server. Something make me mad is that I got message of Connection Refused in servlet though I have no problem in the stand alone program again.
I do not understand why driver cannot be found in servlet while it is working perfect in my application.
Do I need to ask the admin to modify some settings except for put the driver in my own servlet directory?
Or whatever else can I do?
Thanks for your help.
Desperate Chris
Bjarki Holm
Author
Ranch Hand

Joined: May 25, 2001
Posts: 65
Chris,
you say that you "[...] copy the whole directory of oracle thin driver to my servlet class directory from others [...]". What exactly do you mean? What you should be doing, is having the <code>classes12.zip</code> archive in your runtime class path when you start Tomcat. Also, if you add that archive to the <code>$TOMCAT_HOME/lib</code> directory, Tomcat will add the file to the runtime class path when it starts up, which is probably the best solution. Your problem stems from the fact that the Oracle JDBC drivers (classes12.zip) are not in the runtime class path of Tomcat.
------------------
Bjarki Holm
Author of Professional Java Data
[This message has been edited by Bjarki Holm (edited June 10, 2001).]


Bjarki Holm
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by Bjarki Holm:
$TOMCAT_HOME/lib</code> directory, Tomcat will add the file to the runtime class path ...

The Tomcat Documentation specifically states "jar files" could the extension prevent it from picking this up? If it still gives you this error with the zip in the lib directory, change the name to classes12.jar.

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi,
i had a similiar problem. it worked after i put the driver (either packed or unpacked. if unpacked -> make sure you copy the whole package path) into class directory of my webapp (webapp/<webappname>/web-inf/classes/<driver here>). but it should also work if you put it in the lib directory.

karl
Bjarki Holm
Author
Ranch Hand

Joined: May 25, 2001
Posts: 65
Actually, on Windows, it only picks up the JAR files (*.jar). However, on UNIX/Linux (Chris is using UNIX), it picks up everything from TOMCAT_HOME/lib and adds it to the class path. You can check out TOMCAT_HOME/bin/tomcat.sh to make sure.
------------------
Bjarki Holm
Author of Professional Java Data
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
OK, that's saying he has access to $TOMCAT_HOME/lib If he does not, the only place he can deploy this is in the WEB-INF/lib and it will not pick it up if it isn't name .jar (Tested on HP UNIX and Redhat 7)
[This message has been edited by Carl Trusiak (edited June 12, 2001).]
Bjarki Holm
Author
Ranch Hand

Joined: May 25, 2001
Posts: 65
You're right Carl
------------------
Bjarki Holm
Author of Professional Java Data
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Hi,

I think I am so lucky to get my problem when so many experts are meeting here. I just notice the give away note on the board .
However, my problem is still not solved. The current situation is:
I got a mail from the admin who deploy Orion on Solaris 2.7. I am not familiar with Orion but it appears to be an enterprise package providing many J2EE features.
He is not familiar with JDBC but he provides a workaround:
"Anything you put in your WEB-INF\classes or, for jars or zips, WEB-INF\lib, will be picked up by the app server." in addition, he told me that the the server will restart every 15 min (???).
So what I did is go to Oracle website, download the thin driver for Solari, which is classes12.zip and nls_charset12.zip, I remember it is version 8.1.6. or something similar. Then, I
1) rename the oracle directory in my classes dir as something else
2) put the zip files in the WEB-INF/lib
3) compile a simple JDBC program in classes dir as
javac JDBCVersion.java
The error message is
JDBCVersion.java:11: cannot resolve symbol
symbol : class OracleDriver
location: package driver
(new oracle.jdbc.driver.OracleDriver());
This program is not a servlet at all and it works when I have oracle driver directory in my classes dir.
So as Carl suggested, I simply rename classes12.zip as jar. I still have the problem. I finally use socket programming to delegate database connection to a server program and then contact it from the servlet. This is a workaround, BUT not as a servlet supposed to do. the deadline is approachig and I cannot afford more time on it .
Still confused, what hell is going on? BTW, I include the classes12.zip and classes111.zip for NT in the lib of my JBuilder4 Ent Edition at home (NT 4), my servlet program works just fine and I can connect from home to oracle at remote server. I think the problem is still in the server side, but do you think what I can do or what I can suggest the admin do with it?
And should I do anything on that web.xml, I have little ideas about it at all but just simply copy it from someone else and put it in my dir, and my servlet wrok well without the part of JDBC.
Thank you for all your help. I appreciate it!
Chris
Bjarki Holm
Author
Ranch Hand

Joined: May 25, 2001
Posts: 65
Chris,
I understand your frustration. The problem IS that the classes12.zip is not in your class path, and I suggest that you contact the server admin for the Tomcat, and have him put the file in the lib directory of Tomcat, so that there is not doubt that you get in your runtime path.
To check out the actual class path during runtime, try to put the following in some JSP under your app:
<code>
out.println("Class path is:
" + System.getProperty("java.class.path"));
</code>
See what this gives you, and whether the Oracle classes are anywhere in your class path. I suppose they're not.

------------------
Bjarki Holm
Author of Professional Java Data
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Bjarki,
Thank you for your help. But you can not imagine how frustrated I am . I wish I could blame the admin, but I got this after I put your code in my servlet program.
First
Class path is: orion.jar:ejb.jar:activation.jar:jdbc.jar:jndi.jar:jta.jar:mail.jar:xerces.jar:/spool/local/orion/lib:/spool/local/orion/lib/hsql.jar:/spool/local/orion/lib/jakarta-oro-2.0.1.jar:: /spool/local/servlets/chris/WEB-INF/lib/classes12.zip:/spool/local/servlets/chris/WEB-INF/lib/nls_charset12.zip:/spool/local/servlets/chris/WEB-INF/classes
Conclusion: Seems classpath is OK
Second
I still got compiling errors so I remove my main program totally which call my connection method and I compile the file successfully in my unix account (why???)
Third
I put the connection method in the doGet(), compile it and run it from the browser. I got
Could not connect becasue Io exception: The Network Adapter could not establish the connection
The servlet has received a GET. This is the d reply.
Fourth
To make things more confusing
same program runs well in my JBuilder and get connected to the oracle server that I cannot reach at school's orion server. Here is my result when I run it at home:
connected
The servlet has received a GET. This is the d reply.

From my codes, my conclusion is that class is loaded but DriverManager.getConnection has some problems.
Here are the codes for my doGet() and connection method:

doGet(){
...
out.println(testConnection());
...
}
private static String testConnection() {
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (java.lang.ClassNotFoundException e){
return "Driver not found";
}
try{
conn = DriverManager.getConnection(
"jdbc racle:thin:chris/chris@...edu:1521:wg805");
return "connected";
} catch(Exception e){
return "Could not connect becasue "+e.getMessage();
}
}
Like a puzzle for me. Any suggestions? Is that a problem beyond JDBC and servlet?
Thanks a lot
Chris
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
In addition, I tried to rename classes12.zip to classes12.jar and I got the same problems.
I got the driver from http://technet.oracle.com/software/tech/java/sqlj_jdbc/software_index.htm
The only difference is that I use classes12.zip and classes111.zip at home, which I got from another NT machine. The classes12.zip at the school server is from the oracle site.
I believe I selected the one for Solaris, though I heard I can use any classes12.zip for the connection.
Thanks
Chris
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Get answer for one question. Because I encapsulate connection codes in the try block, I now can include it in the main method and get it compiled. So, path is not a big problem now, maybe.
Bjarki Holm
Author
Ranch Hand

Joined: May 25, 2001
Posts: 65
Chris,
ok, the key problem now is that you don't get a connection to the database. The error message you get,
<code>
Io exception: The Network Adapter could not establish the connection
</code>
means that you are unable to access the database you specified, either because:
1. the database is not listening
2. the database properties are invalid
You are using the Thin driver: are you sure that the TNS listener is up and running at the database host?
If so, is the URL you supplied correct? Can you access the database from the middle tier (try to ping the URL, possibly using TNSPING if there is an Oracle client at the server)? I notices a little space between "jdbc" and " racle" in the URL, is this just a typo here or also in your app?
Check these things out, especially the one with the listener, and let me know if you get any closer ;)

------------------
Bjarki Holm
Author of Professional Java Data
[This message has been edited by Bjarki Holm (edited June 12, 2001).]
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Thanks. be honest, I forgot what TNS stands for, . However, I now think the problem is on Orion's configuration, not the coding part. My logic is:
1. With the same Oracle server, which is Solaris with java 1.1 or something, and with the same servlet program, I can connect the oracle from my home pc in Jbuilder4 (with tomcat included), but I cannot do the same thing at the school's Orion server, even though the classpath now seems not a problem anymore.
2. I still cannot run a stand alone JDBC program in UNIX in that server, IF I rename the extracted driver directory to something else (oracle). But if I keep it as it is, I can connect oracle in a stand alone program in my unix account, but not in servlet through browser. I will get the same error message. Kind of strange.
BTW, the codes you see on the board is a little strange, I think the board convert ":" and "o" to a mark.
I remember I saw a post before and someone claimed there was a bug on DriverManager, which uses == instead of equals(), but that is a 1999 or 98 post at Sun's forum. I doubt that is the problem.
Due to the coming deadline, I decide and actually have to use socket to delegate the database access to a server program and send the result to the servlet finally . I feel it is hard for me to figure out the problems soon, which might be related to the server configuration, without the proper permission to access them. And I understand it is even harder for you think about it with my limited info. Thanks a lot and I appreciate it very much!
Chris
Bjarki Holm
Author
Ranch Hand

Joined: May 25, 2001
Posts: 65
Chris,
well, I really think you have to get some Oracle specialist, with an access to that server, to assist you on this. You let us know if you have any more information.
Good luck; hope you make that deadline!
------------------
Bjarki Holm
Author of Professional Java Data
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Thanks Bjarki. I never missed any deadline and I will make it this time.
It is hard to find a place where there are so many experts get together as JavaRanch. I asked the people at school who know oracle, but he is not familiar with orion. For the people who installed orion, he is not familiar with oracle. I wish I had the access to the servers and I believe I would figure it out eventually with your help, but...
Anyway, for a student, learning is more important. I think I already benefit from the discussions with you and other friends during my frustration. I am very glad to have this problem now.
Have a nice day
Chris
BTW, sure, if I could find the reason later, I will let you know.
Bjarki Holm
Author
Ranch Hand

Joined: May 25, 2001
Posts: 65
You do that Chris, and good luck with your assignment!
------------------
Bjarki Holm
Author of Professional Java Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FOR HELP: Failed to connect to oracle in servlet