This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes doubt having servlet connect to oracle XE database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "doubt having servlet connect to oracle XE database" Watch "doubt having servlet connect to oracle XE database" New topic
Author

doubt having servlet connect to oracle XE database

mike barakat
Greenhorn

Joined: Jan 28, 2008
Posts: 3
hello there,
i got a working application (an online bookstore) which interacts with MS access database through java servlets.
now i wish to change the database to oracle XE,but i am having many doubts and can't tell what to do to fix it.
first doubt is,do i need to put a system dsn in control panel like i did to the MS access database?
note that i am using oracle.jdbc.driver.OracleDriver.
second,is my code correct at all?
i will paste here the code of the java Database class which connects to the oracle database,and the code for a java servlet which calls the java class to connect to Database.
code of java class: (Database.java)

code for the java servlet: (ShoppingCart.java)

i checked logs,here is the error:
java.lang.NullPointerException
Database.get(Database.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet,java:803)

if you need further info i will write,thanks for your time.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30795
    
157

Mike,
Welcome to JavaRanch!

In the connect method, you print a stack trace if there is a problem, but then swallow the exception. This allows code to continue and makes it likely you will get a null pointer later on when the connection is null.

Do the logs have the stack trace? Also, which is like 36?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
mike barakat
Greenhorn

Joined: Jan 28, 2008
Posts: 3
hi there,
thank you and sorry for the late reply ...had few exams..
can you please clarify what do you mean here ?
but then swallow the exception

i don't understand how could this code get a null pointer later.
Do the logs have the stack trace
how can i check?
can you please clarrify more, i will be thankful and thanks in advance for you patience
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
Instead of



try



No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
mike barakat
Greenhorn

Joined: Jan 28, 2008
Posts: 3
hey,
i tried this code, it gave a compile error:
registerDriver(java.sql.Driver) in java.sql.DriverManager cannot be applied to (java.lang.String)
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
It is perfectly correct to use Class.forName to explicitly load the driver.

What you really need to do is to deal with the point which Jeanne made about swallowing exceptions. This is poor practice, you should at least print the stacktrace.

You are also returning null from the get method. This is the immediate cause of the problem. It is better to either rethrow SQLException after printing the stacktrace or to not handle the exception at all.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30795
    
157

Originally posted by mike barakat:
i don't understand how could this code get a null pointer later.

It here is an error getting the connection, you log a message and then connection is set to null. Then when you call the get method, you have "statement = null.createStatement();"



how can i check?

The exact way depends on the server you are using (Tomcat, JBoss, etc). They are either in a console file or a log file under the server install directory.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt having servlet connect to oracle XE database