hello there, i got a working application (an online bookstore) which interacts with MS access database through javaservlets. 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.
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?
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.