3 Replies Latest reply on Jul 17, 2008 10:23 AM by 634908

    Unable to connect JavaDB

    807589
      I created a database in derby (with ij tool). I wanted to connect
      this database through JSP page from Tomcat 6 server.

      I started the Network Server from Command Window(Server Shell)

      Server started (used 1527 port)

      I connected the created database by typing:

      ij> connect 'jdbc:derby://localhost:1527/assetsdb;create=true';

      Can I access the database from JSP page this way?

      Sign.jsp
      <html>
      <head>
      <title>Sign Up</title>
      </head>
      <body>
      <%@ page language="java" import="java.sql.*" %>
      <%
      Connection conn;
      conn=null;
      ResultSet rs;
      rs=null;
      String empID=request.getParameter("empID");
      String pwd1=request.getParameter("pwd1");
      String pwd2=request.getParameter("pwd2");
       
      int flag=0;
       
      String driver="org.apache.derby.jdbc.ClientDriver";
      String dbName="assetsdb";
      String
      connURL="jdbc:derby://localhost:1527/"+dbName+";create=true";
      String selQuery="SELECT * FROM Employees where ID=?";
      String inQuery="INSERT INTO Viewers values(?,?)";
      try
      {
      Class.forName(driver);
      conn=DriverManager.getConnection(connURL);
      PreparedStatement stat=conn.prepareStatement
      (selQuery);
      stat.setString(1,empID);
      rs=stat.executeQuery();
      if(!rs.next())
      {
      flag=1;
      %>
      <script type="text/javascript">
      alert("Employee ID Not Found.");
      location.href="NewViewer.htm";
      </script>
      <%
      conn.close();
      }
      }
      catch(Exception e)
      {
      out.println("Mistake:"+e);
      }
      if(flag==0)
      {
      try
      {
      Class.forName(driver);
      conn=DriverManager.getConnection(connURL);
      PreparedStatement stat1=conn.prepareStatement
      (inQuery);
      stat1.setString(1,empID);
      stat1.setString(2,pwd1);
      stat1.executeUpdate();
      flag=0;
      response.sendRedirect("login.htm");
      }
      catch(Exception e)
      {
      out.println("Mistake inserting values:"+e);
      }
      finally
      {
      rs.close();
      conn.close();
      }
      }
      %>
      </body>
      </html>
      Here is the 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: An exception occurred processing JSP page /JSPs/Sign.jsp at line 65
       
      62:           }
      63:           finally
      64:           {
      65:                rs.close();
      66:                conn.close();
      67:           }
      68:      }
       
       
      Stacktrace:
           org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
           org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
           org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
           org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
           javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
       
       
      root cause 
       
      java.lang.NullPointerException
           org.apache.jsp.JSPs.Sign_jsp._jspService(Sign_jsp.java:120)
           org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
           javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
           org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
           org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
           org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
           javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
       
       
      note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.
       
       
      --------------------------------------------------------------------------------
       
      Apache Tomcat/6.0.16
      --------------------------------------------------------------------------------
      Where am I wrong in connecting the database? I placed the
      Sign.jsp page in Tomcat's ROOT/JSPs/Sign.jsp and the database in
      E:\IT Assets\assetsdb

      Please help.