aspose file tools*
The moose likes JDBC and the fly likes Database connection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database connection problem" Watch "Database connection problem" New topic
Author

Database connection problem

Krasimir Ivanov
Greenhorn

Joined: Feb 04, 2011
Posts: 7
Hello, guys i need help. I work on a website project and i decided to make the the database connection with web.xml and servlet database connectivity. I defined all database variables in web.xml and on load of servlet i open connection in servlet’s init method through JavaBean.

This is the structure of the website project.
website
---------html
---------jsp
---------img
---------WEB-INF
-------------------web.xml
-------------------classes
---------------------------database
-------------------------------------DBInit.class
-------------------------------------DBConnection.class

web.xml


DBInit.class


DBConection.class


This the code that i'm using in every jsp page to connect with the database



When i start a jsp file, i get the folowing error:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:532)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.NullPointerException
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
database.DBConnection.getDBConnection(DBConnection.java:72)
org.apache.jsp.history_jsp._jspService(history_jsp.java:67)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.6 logs.
Apache Tomcat/7.0.6


And this is in the log file of the tomcat server:

2011-2-4 18:37:16 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/website] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DBConnection.getDBConnection(DBConnection.java:72)
at org.apache.jsp.history_jsp._jspService(history_jsp.java:67)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:284)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1684)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

Where is the problem?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

You are getting a null pointer exception at line 72 of DBConnection. Look at that line to figure out why a reference could be null, and work back from there.

Though I would say that the first step is to refactor the code out of the JSP. JSPs should not contain any Java code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Krasimir Ivanov
Greenhorn

Joined: Feb 04, 2011
Posts: 7
The line 72 is: Class.forName(driver).newInstance();
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Makes sense. You don't set the variable sdriver anywhere.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Krasimir Ivanov
Greenhorn

Joined: Feb 04, 2011
Posts: 7
I set it in DBInit.class. It's from "Statement statement = connection.createStatement();".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database connection problem
 
Similar Threads
Best way to use a Connection
Problen running Struts application!!
Problem in inserting record to Oracle Database.
overriding init() to make a database connection:nullpointer Exception
Hibernate and JSP