Results 1 to 2 of 2
  1. #1
    milleniamisc is offline Member
    Join Date
    Oct 2012
    Rep Power

    Unhappy Help connecting to a database from a JSP

    I am trying to connect to a database using a very simple JSP. I made a Database connection servlet which is extended in my JSP. And I have the sql statement to be executed written in my JSP. I keep getting a java.lang.NullPointerException and I do not know why. Help!!

    This is my
    Java Code:
    package app01b;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public abstract class DatabaseServlet
       extends HttpServlet
    	private static final long serialVersionUID = 1L;
    protected Connection con;
       public void init(ServletConfig config)
          throws ServletException
          try {
             con = DriverManager.getConnection(
                     "jdbc:oracle:thin:@localhost:1521:ganesh", "hr", "hr");
             catch (Exception e) {
                 throw new UnavailableException(e.getMessage());
    This is my welcome.jsp
    Java Code:
    <%@ page extends="app01b.DatabaseServlet"%>
    <%@page import="*, java.sql.*"%>
       Statement stmt = con.createStatement();
       String sql = "select name from lib_member_master where member_id = 101";
       ResultSet rs = stmt.executeQuery(sql);
       while ( {
          String result = rs.getString(1);
    Welcome <%=result%>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Help connecting to a database from a JSP

    Don't write it that way.
    Have your request go to a servlet.
    Have that servlet call a DAO to get the data it wants.
    The DAO will handle opening and closing connections (preferably with a connections pool) and will return a List<SomeModelClass>, where SomeModelClass is a class that represents the data returned. The DAO will convert each row in the resultset into one of these and add it to the List.

    The servlet will add this list to the request, and then forward to the JSP (if everything is OK).
    All the JSP has to do is display the Model.

    Put simply, the servlet does the work, the JSP simply displays what the servlet supplies to it.

    Oh, and you should pretty much never give a servlet state. That is, it should not have any attributes. That connection you have there is not threadsafe at all.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. connecting to a database
    By mxb7642 in forum JDBC
    Replies: 5
    Last Post: 09-08-2009, 09:39 AM
  2. connecting to a database
    By kswiss in forum NetBeans
    Replies: 4
    Last Post: 06-25-2009, 07:22 PM
  3. Connecting JSP with database
    By ramachandran in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 05-13-2009, 07:15 AM
  4. Connecting to a database
    By peiceonly in forum New To Java
    Replies: 2
    Last Post: 04-06-2008, 03:28 PM
  5. Error while connecting to a database
    By Fukushuusha in forum JDBC
    Replies: 2
    Last Post: 12-16-2007, 10:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts