• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt having servlet connect to oracle XE database

 
mike barakat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
mike barakat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of



try

 
mike barakat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic