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

    Default Attempting to connect to a database in 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 DatabaseServlet.java
    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
       {
          super.init(config);
          try {
             Class.forName("oracle.jdbc.OracleDriver");
             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.io.*, java.sql.*"%>
    <%
      
       Statement stmt = con.createStatement();
       String sql = "select name from lib_member_master where member_id = 101";
       ResultSet rs = stmt.executeQuery(sql);
       while (rs.next()) {
          String result = rs.getString(1);
    %>
    <html>
    <head><title>Welcome</title></head>
    <body>
    Welcome <%=result%>
    </body>
    <%
       }
       rs.close();
       stmt.close();
    %>
    </html>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Attempting to connect to a database in a JSP

    Don't double post.
    I'm locking this one in favour of the one in the JDBC forum.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Connect with DATABASE
    By UJJAL DHAR in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 04:18 PM
  2. How to connect database using javaScript..
    By vinoopraj in forum Advanced Java
    Replies: 1
    Last Post: 03-20-2009, 11:50 AM
  3. connect drop down to database
    By Shivraj in forum NetBeans
    Replies: 0
    Last Post: 03-16-2009, 09:54 AM
  4. errors when trying to connect to database
    By matpj in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 12:06 AM
  5. Problem to connect Database
    By Swamipsn in forum NetBeans
    Replies: 1
    Last Post: 08-07-2007, 07:13 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
  •