[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 631
  • Last Modified:

Problem while handling try, catch blocks

Hi ....... according to the following program, the page must check either the userID and account_no given by the user does exist in the database( or not ).

1 - If I enter new userID and new Account_no, the record safely gets inserted in both the tables(i.e., 'customer' and 'account')

2 - If I enter an old userID and a new account_no, the page returns an error saying .... "Username already exists"

3 - If I enter a new userID and an old Account_no, the
page returns an error saying .... "Account # already exists" but alongwith it inserts the record in 'customer' table

     No. 3 points to the major problem.... the record must not be inserted in the 'customer' table even if the user gives a new ID. I hope u understand what I mean to say..... What should be the change in the following code.... Plz do reply back immediately as I'll b obliged to u

<%
try{
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection con = DriverManager.getConnection("jdbc:odbc:hello","","");

  PreparedStatement ps = con.prepareStatement("select * from customer where loginid = '" + request.getParameter("uid") + "'");
  ResultSet rs = ps.executeQuery();

  String dbID = request.getParameter("uid");
  int i;
  String ac_no = request.getParameter("acc_no");
  String ac1;
  while(rs.next())
  {
    if(dbID.equals(request.getParameter("uid")))
    {
      response.sendRedirect("CreateUser.jsp?error=bad&uid=" + request.getParameter("uid") + "");
    }
  }

  PreparedStatement ps4 = con.prepareStatement("select * from account where account_no = " + request.getParameter("acc_no") + "");
  ResultSet rs4 = ps4.executeQuery();

  while(rs4.next())
  {
    int i = rs4.getInt("account_no");
    String ac1 = new Integer(i).toString();
    if(ac1.equals(request.getParameter("acc_no")))
    {
      response.sendRedirect("CreateUser.jsp?ac=bad&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "");
    }
  }

  Statement statement = con.createStatement();
  String strSQL = "insert into customer(loginid,password) values('" + request.getParameter("uid") + "','" + request.getParameter("pwd") + "')";
  statement.executeUpdate(strSQL);
  statement.close();

  Statement statement2 = con.createStatement();
  String strSQL2 = "insert into account(account_no,loginid) values(" + request.getParameter("acc_no") + ",'" + request.getParameter("uid") + "')";
  statement2.executeUpdate(strSQL2);
  statement2.close();
}
catch(SQLException e){
  e.printStackTrace();
}
%>

0
adnan_rais
Asked:
adnan_rais
  • 16
  • 12
  • 11
1 Solution
 
adnan_raisAuthor Commented:
Plz reply as it's urgent
0
 
jimmackCommented:
The problem you have is that you are relying on the exception to be thrown when you try to insert a duplicate record.  In this particular case, you have two separate inserts.  Unfortunately, the first one succeeds, but the second one fails.

You should avoid throwing an exception in a case like this (where you can identify if there is a problem or not).  Exceptions are usually reserved for situations that you cannot easily test or control (eg. SQLExceptions are designed to handle things like incorrect SQL or problems accessing the database).

In your code, when testing (with select) whether the id or account number exist, you should set a boolean flag eg.

  boolean okToInsert = true;

  while(rs.next())
  {
    if(dbID.equals(request.getParameter("uid")))
    {
      okToInsert = false;
      response.sendRedirect("CreateUser.jsp?error=bad&uid=" + request.getParameter("uid") + "");
    }
  }

  PreparedStatement ps4 = con.prepareStatement("select * from account where account_no = " + request.getParameter("acc_no") + "");
  ResultSet rs4 = ps4.executeQuery();

  while(rs4.next())
  {
    int i = rs4.getInt("account_no");
    String ac1 = new Integer(i).toString();
    if(ac1.equals(request.getParameter("acc_no")))
    {
      okToInsert = false;
      response.sendRedirect("CreateUser.jsp?ac=bad&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "");
    }
  }

  if (okToInsert)
  {
    Statement statement = con.createStatement();
    String strSQL = "insert into customer(loginid,password) values('" + request.getParameter("uid") + "','" + request.getParameter("pwd") + "')";
    .
    .
0
 
JNicCommented:
jimmack is right!

This way you also avoid the problems mentioned in the end of:

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20791821.html

- The jsp will never execute the update, if okToInsert is false.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
adnan_raisAuthor Commented:
ok jammick ur code worked a lot but still displays an error in userID matching case. Here is the error code

type Exception report

message Internal Server Error

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

exception

javax.servlet.ServletException: General error
      at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:463)
      at org.apache.jsp.CreateUserAct$jsp._jspService(CreateUserAct$jsp.java:157)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
      at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012)
      at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107)
      at java.lang.Thread.run(Thread.java:484)


root cause

java.sql.SQLException: General error
      at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6060)
      at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6188)
      at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:2494)
      at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:334)
      at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:284)
      at org.apache.jsp.CreateUserAct$jsp._jspService(CreateUserAct$jsp.java:140)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
      at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012)
      at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107)
      at java.lang.Thread.run(Thread.java:484)

What to do next?
0
 
jimmackCommented:
The problem has moved now ;-)  It's now in the file CreateUserAct.jsp.

This seems to imply that within that file you are trying to access the database again, but perhaps you are missing some data (because no account has been created?).

Is there any chance you could post that file?
0
 
JNicCommented:
Have you forgotten try/catch blocks in CreateUserAct.jsp ? Remember, you must handle an eventual SQLException when you work with ResultSets.
0
 
jimmackCommented:
JNic, even if this is the case, it's more likely something that should be tested, rather than thrown ;-)
0
 
adnan_raisAuthor Commented:
<%
  String err = request.getParameter("error");
  String err1 = request.getParameter("ac");
  if( err != null )
  {
    out.println("<tr><td align=middle colspan=2><b><font color=red size=3>Username already exists</font></b></td></tr>");
  }else if( err1 != null )
  {
    out.println("<tr><td align=middle colspan=2><b><font color=red size=3>Account # already exists for some other user</font></b></td></tr>");
  }
%>
0
 
adnan_raisAuthor Commented:
any idea jimmack... i've posted u a little code  from CreateUser.jsp

i think it would b enough to u
0
 
jimmackCommented:
I can't see anything wrong with that ;-)

The problem is an SQLException though.  Do you do any SQL handling in the this JSP?

You could check in the "work" directory under your servlet container installation directory.  In there, you should find a file called something like CreateUserAct_jsp.java.

The problem is occurring (originally) at line 140.

>>      at org.apache.jsp.CreateUserAct$jsp._jspService(CreateUserAct$jsp.java:140)

I may have made an error in my last posting.  Do you have JSPs called CreateUser.jsp and one called CreateUserAct.jsp?  If so, the problem is in the second one ;-)
0
 
adnan_raisAuthor Commented:
yes I've created the pages named CreateUser.jsp and CreateUserAct.jsp..... How to handle the error in that file at line 140
0
 
adnan_raisAuthor Commented:
no the page doesn't contain any Exception handling after pasting ur code
0
 
adnan_raisAuthor Commented:
How to check the work directory in JBuilder.... The file is not named as CreateUserAct.java but it is CreateUserBean.java n there's no such coding upto line 140
0
 
adnan_raisAuthor Commented:
Here is the code of CreateUserAct.java file

package org.apache.jsp;

import java.sql.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;


public class CreateUserAct$jsp extends HttpJspBase {

    // begin [file="/CreateUserAct.jsp";from=(7,0);to=(7,87)]
    // end

    static {
    }
    public CreateUserAct$jsp( ) {
    }

    private static boolean _jspx_inited = false;

    public final void _jspx_init() throws org.apache.jasper.runtime.JspException {
    }

    public void _jspService(HttpServletRequest request, HttpServletResponse  response)
        throws java.io.IOException, ServletException {

        JspFactory _jspxFactory = null;
        PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        String  _value = null;
        try {

            if (_jspx_inited == false) {
                synchronized (this) {
                    if (_jspx_inited == false) {
                        _jspx_init();
                        _jspx_inited = true;
                    }
                }
            }
            _jspxFactory = JspFactory.getDefaultFactory();
            response.setContentType("text/html;charset=ISO-8859-1");
            pageContext = _jspxFactory.getPageContext(this, request, response,
                                    "", true, 8192, true);

            application = pageContext.getServletContext();
            config = pageContext.getServletConfig();
            session = pageContext.getSession();
            out = pageContext.getOut();

            // HTML // begin [file="/CreateUserAct.jsp";from=(0,0);to=(6,0)]
                out.write("<html>\r\n<head>\r\n<title>\r\nCreate User Action Page\r\n</title>\r\n</head>\r\n");

            // end
            // HTML // begin [file="/CreateUserAct.jsp";from=(6,58);to=(7,0)]
                out.write("\r\n");

            // end
            // begin [file="/CreateUserAct.jsp";from=(7,0);to=(7,87)]
                obs3.CreateUserActBean CreateUserActBeanId = null;
                boolean _jspx_specialCreateUserActBeanId  = false;
                 synchronized (session) {
                    CreateUserActBeanId= (obs3.CreateUserActBean)
                    pageContext.getAttribute("CreateUserActBeanId",PageContext.SESSION_SCOPE);
                    if ( CreateUserActBeanId == null ) {
                        _jspx_specialCreateUserActBeanId = true;
                        try {
                            CreateUserActBeanId = (obs3.CreateUserActBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "obs3.CreateUserActBean");
                        } catch (ClassNotFoundException exc) {
                             throw new InstantiationException(exc.getMessage());
                        } catch (Exception exc) {
                             throw new ServletException (" Cannot create bean of class "+"obs3.CreateUserActBean", exc);
                        }
                        pageContext.setAttribute("CreateUserActBeanId", CreateUserActBeanId, PageContext.SESSION_SCOPE);
                    }
                 }
                if(_jspx_specialCreateUserActBeanId == true) {
            // end
            // begin [file="/CreateUserAct.jsp";from=(7,0);to=(7,87)]
                }
            // end
            // HTML // begin [file="/CreateUserAct.jsp";from=(7,87);to=(8,0)]
                out.write("\r\n");

            // end
            // begin [file="/CreateUserAct.jsp";from=(8,0);to=(8,59)]
                JspRuntimeLibrary.introspect(pageContext.findAttribute("CreateUserActBeanId"), request);
            // end
            // HTML // begin [file="/CreateUserAct.jsp";from=(8,59);to=(13,0)]
                out.write("\r\n<body bgcolor=\"lightyellow\">\r\n<center>\r\n<br><br>\r\n<br><br><br><br>\r\n");

            // end
            // begin [file="/CreateUserAct.jsp";from=(13,2);to=(61,0)]


                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                  Connection con = DriverManager.getConnection("jdbc:odbc:hello","","");

                  PreparedStatement ps = con.prepareStatement("select * from customer where loginid = '" + request.getParameter("uid") + "' and account_no = " + request.getParameter("acc_no") + "");
                  ResultSet rs = ps.executeQuery();

                  String dbID;
                  int i;
                  String ac_no = request.getParameter("acc_no");
                  String ac1;
                  boolean insertTo = true;
                  while(rs.next())
                  {
                    dbID = rs.getString("loginid");
                    if( dbID.equals(request.getParameter("uid")) )
                    {
                      insertTo = false;
                      response.sendRedirect("CreateUser.jsp?error=bad&title=" + request.getParameter("title") + "&fname=" + request.getParameter("fname") + "&lname=" + request.getParameter("lname") + "&father=" + request.getParameter("father") + "&addr=" + request.getParameter("addr") + "&phone=" + request.getParameter("phone") + "&bank=" + request.getParameter("bank") + "&AccType=" + request.getParameter("AccType") + "&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "&day=" + request.getParameter("day") + "&month=" + request.getParameter("month") + "&year=" + request.getParameter("year") + "&gender=" + request.getParameter("gender") + "&bal=" + request.getParameter("bal") + "");
                    }
                  }
                  PreparedStatement ps4 = con.prepareStatement("select * from account where account_no = " + request.getParameter("acc_no") + "");
                  ResultSet rs4 = ps4.executeQuery();

                  while(rs4.next())
                  {
                    i = rs4.getInt("account_no");
                    ac1 = new Integer(i).toString();
                    if(ac1.equals(request.getParameter("acc_no")))
                    {
                      insertTo = false;
                      response.sendRedirect("CreateUser.jsp?ac=bad&title=" + request.getParameter("title") + "&fname=" + request.getParameter("fname") + "&lname=" + request.getParameter("lname") + "&father=" + request.getParameter("father") + "&addr=" + request.getParameter("addr") + "&phone=" + request.getParameter("phone") + "&bank=" + request.getParameter("bank") + "&AccType=" + request.getParameter("AccType") + "&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "&day=" + request.getParameter("day") + "&month=" + request.getParameter("month") + "&year=" + request.getParameter("year") + "&gender=" + request.getParameter("gender") + "&bal=" + request.getParameter("bal") + "");
                    }
                  }

                  if(insertTo)
                  {
                    Statement statement = con.createStatement();
                    String strSQL = "insert into customer(loginid,branch_code,fname,lname,father_name,phone_no,address,Dob,password,Gender) values('" + request.getParameter("uid") + "'," + request.getParameter("bank") + ",'" + request.getParameter("fname") + "','" + request.getParameter("lname") + "','" + request.getParameter("father") + "'," + request.getParameter("phone") + ",'" + request.getParameter("addr") + "','" + request.getParameter("month") + "/" + request.getParameter("day") + "/" + request.getParameter("year") + "','" + request.getParameter("pwd") + "','" + request.getParameter("gender") + "')";
                    statement.executeUpdate(strSQL);
                    statement.close();

                    Statement statement2 = con.createStatement();
                    String strSQL2 = "insert into account(account_no,branch_code,acc_type,loginid,balance) values(" + request.getParameter("acc_no") + "," + request.getParameter("bank") + ",'" + request.getParameter("AccType") + "','" + request.getParameter("uid") + "'," + request.getParameter("bal") + ")";
                    statement2.executeUpdate(strSQL2);
                    statement2.close();
                  }
            // end
            // HTML // begin [file="/CreateUserAct.jsp";from=(61,2);to=(69,0)]
                out.write("\r\n<font color=\"red\" face=\"verdana\" size=3><b>Your have successfully created your account</b></font>\r\n<br><br><br><br>\r\n<a href=\"CreateUser.jsp\">Continue to Login Page</a>\r\n\r\n</center>\r\n</body>\r\n</html>\r\n");

            // end

        } catch (Throwable t) {
            if (out != null && out.getBufferSize() != 0)
                out.clearBuffer();
            if (pageContext != null) pageContext.handlePageException(t);
        } finally {
            if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
        }
    }
}


The line no. 140 points to this statement

                    statement.executeUpdate(strSQL);
What to do next? Do guide me it's urgent
0
 
jimmackCommented:
Ah.  Sorry.  I should probably have explained that better :-(

The JSP gets translated into servlet source code (as though you had written it as a servlet yourself) and then compiles it.  The file it creates is the same name as your .jsp, but with a little renaming, in this case CreateUserAct_jsp.java.

Since this is the file that actually gets compiled, the server can only report line number problems from that and not from the JSP.

Sorry.

On my system (running Tomcat in standalone mode), these files appear in:

/var/tomcat4/work/Standalone/localhost/<webapp name>/..._jsp.java

If you can find that file, it's line 140 that's causing the problem.  You should be able to relate that back to the JSP to see what the SQL statement is that is giving the problem.

Hope that helps a bit ;-)
0
 
jimmackCommented:
Bit of a cross-posting there ;-)

The problem is probably within the strSQL that is being executed.  Are all the inverted commas and apostrophes correct?  Is it because some of the request parameters are null?
0
 
adnan_raisAuthor Commented:
U do let me know about it plz..... All the data is almost correct....
0
 
jimmackCommented:
Does request.getParameter("phone") contain any characters that might cause problems in SQL?

The problem is definitely with this "insert" command, but I'm not sure what "General Error" means in this context?  It may be because you are trying to add a duplicate record.  This could be for the following reason:

On lines 115 and 116, you are comparing "loginid" and "uid".  The "insert" statement has a column name of loginid, but you are actually providing the uid.  Is that what you want to do?
0
 
adnan_raisAuthor Commented:
no the request.getParameter("phone") doesn't contain any character. The program is inserting all the records correctly in both tables and none value is duplicated ..... Ok now I'm posting u both the files
0
 
adnan_raisAuthor Commented:
//  CreateUser.jsp



<html>
<head>
<title>
Create User Page
</title>
<script language="JavaScript">

   function validate(f){
     if(f.title.value=="none"){
       alert("Title must be selected");
       f.title.focus();
       return false;
     }else
     if(f.fname.value==""){
       alert("Must enter your First name");
       f.fname.focus();
       return false;
     }else
       if( !ChkLtr(f.fname.value) )
          {
                  f.fname.value = "";
                  f.fname.focus();
                  return false;
          }else

     if(f.lname.value==""){
       alert("Last name must be entered");
       f.lname.focus();
       return false;
     }else
       if( !ChkLtr(f.lname.value) )
        {
                f.lname.value = "";
                f.lname.focus();
                return false;
       }else
     if(f.father.value==""){
       alert("Father's name is missing");
       f.father.focus();
       return false;
     }else
     if(f.day.value=="none"){
       alert("Select your day of birth");
       f.day.focus();
       return false;
     }else
     if(f.month.value=="none"){
       alert("Select your month of birth");
       f.month.focus();
       return false;
     }else
     if(f.year.value=="none"){
       alert("Select your year of birth");
       f.year.focus();
       return false;
     }else
       if(f.gender.value=="none"){
           alert("What is your gender?");
           f.gender.focus();
           return false;
       }else
     if(f.addr.value==""){
       alert("Address is missing");
       f.addr.focus();
       return false;
     }else
     if(f.phone.value==""){
       alert("Phone number is missing");
       f.phone.focus();
       return false;
     }else
       if( !ChkNbr(f.phone.value) )
        {
                f.phone.value = "";
                f.phone.focus();
                return false;
        }else
     if(f.bank.value=="none"){
        alert("For which bank you would like to create account");
        f.bank.focus();
        return false;
     }else
     if(f.AccType.value=="none"){
        alert("Account type must be selected");
        f.AccType.focus();
        return false;
     }else
       if(f.acc_no.value==""){
               alert("Account # must be provided");
               f.acc_no.focus();
               return false;
            }else
              if( !ChkNbr(f.acc_no.value) )
               {
                       f.acc_no.value = "";
                       f.acc_no.focus();
                       return false;
               }else
          if(f.bal.value==""){
        alert("User Balance must be provided");
        f.bal.focus();
        return false;
     }else
       if( !ChkNbr(f.bal.value) )
        {
                f.bal.value = "";
                f.bal.focus();
                return false;
        }else
     if(f.uid.value==""){
       alert("User ID is missing");
       f.uid.focus();
       return false;
     }else
     if(f.pwd.value==""){
       alert("Password is missing");
       f.pwd.focus();
       return false;
     }else
     if(f.c_pwd.value==""){
       alert("Password must be confirmed once again");
       f.c_pwd.focus();
       return false;
     }else
     if(f.c_pwd.value != f.pwd.value){
       alert("Password mismatch");
       f.pwd.value = "";
       f.c_pwd.value = "";
       f.pwd.focus();
       return false;
     }
   return true;
   }
   function ChkLtr(str)
   {
           var txt = /[^a-zA-Z]/g;
           if( txt.test(str) )
           {
                   alert("Only Alphabetic Characters are allowed");
                   return false;
           }
           return true;
   }

   function ChkNbr(str)
   {
           var txt = /[^0-9]/g;
           if( txt.test(str) )
           {
                   alert("Only Numeric values are allowed");
                   return false;
           }
           return true;
   }
</script>
</head>
<%@ page language="Java" import="java.sql.*,java.util.*,java.lang.*" %>
<jsp:useBean id="CreateUserBeanId" scope="session" class="obs3.CreateUserBean" />
<jsp:setProperty name="CreateUserBeanId" property="*" />
<%
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con = DriverManager.getConnection("jdbc:odbc:hello","","");
%>
<body bgcolor="lightyellow">
<center>
<h1>
Create User Page
</h1>
<hr>
<form name="user" method="get" action="CreateUserAct.jsp">
<br>
<table border=0 bordercolor="black" bgcolor="whitesmoke" cellspacing=2 cellpadding=3>
<%
  String err = request.getParameter("error");
  String err1 = request.getParameter("ac");
  if( err != null )
  {
    out.println("<tr><td align=middle colspan=2><b><font color=red size=3>Username already exists</font></b></td></tr>");
  }else if( err1 != null )
  {
    out.println("<tr><td align=middle colspan=2><b><font color=red size=3>Account # already exists for some other user</font></b></td></tr>");
  }
%>
<tr>
<td align="right"><b>Title:</b></td>
<td>
   <select name="title">
<%
  String title = request.getParameter("title");
  if( title != null){
  if( title.equals("Mr") )
  {
    out.println("<option value=none></option>");
    out.println("<option selected value=Mr>Mr</option>");
    out.println("<option value=Ms>Ms</option>");
    out.println("<option value=Mrs>Mrs</option>");
  }else if( title.equals("Ms") ){
    out.println("<option value=none></option>");
    out.println("<option value=Mr>Mr</option>");
    out.println("<option selected value=Ms>Ms</option>");
    out.println("<option value=Mrs>Mrs</option>");
  }else if( title.equals("Mrs") ){
    out.println("<option value=none></option>");
    out.println("<option value=Mr>Mr</option>");
    out.println("<option value=Ms>Ms</option>");
    out.println("<option selected value=Mrs>Mrs</option>");
  }
  }else{
    out.println("<option value=none></option>");
    out.println("<option value=Mr>Mr</option>");
    out.println("<option value=Ms>Ms</option>");
    out.println("<option value=Mrs>Mrs</option>");
  }
%>
   </select>
</td>
</tr>

<tr>
<td align="right"><b>First Name:</b></td>
<td>
<%
  String fname = request.getParameter("fname");
  if( fname != null )
  {
    out.println("<input name=fname size=20 value=" + fname + ">");
  }else{
%>
<input name="fname" size=20>
<%
  }
%>
</td>
</tr>

<tr>
<td align="right"><b>Last Name:</b></td>
<td>
<%
  String lname = request.getParameter("lname");
  if( lname != null )
  {
    out.println("<input name=lname size=20 value=" + lname + ">");
  }else{
%>
<input name="lname" size=20>
<%
  }
%>
</td>
</tr>

<tr>
<td align="right"><b>Father's Name:</b></td>
<td>
<%
  String father = request.getParameter("father");
  if( father != null )
  {
    out.println("<input name=father value=" + father + " size=20>");
  }else{
%>
<input name="father" size=20>
<%
  }
%>
</td>
</tr>

<tr>
<td align="right"><b>Date of birth:</b></td>
<td>
   <select name="day">
      <option value="none">Day</option>
<%
    String day = request.getParameter("day");
    String mday;
    for(int i=1;i<=31;i++)
    {
      mday = new Integer(i).toString();
      if( day != null && mday.equals(request.getParameter("day")) )
      {
        out.println("<option selected value=" + mday + ">" + mday + "</option>");
      }else{
        out.println("<option value=" + i + ">" + i + "</option>");
      }
    }
%>
   </select>
   <select name="month">
      <option value="none">Month</option>
<%
String m[] = new String[13];
m[0] = "none";
m[1] = "January";
m[2] = "February";
m[3] = "March";
m[4] = "April";
m[5] = "May";
m[6] = "June";
m[7] = "July";
m[8] = "August";
m[9] = "September";
m[10] = "October";
m[11] = "November";
m[12] = "December";

String m1 = request.getParameter("month");
String m2;
for(int j=1;j<=m.length-1;j++)
{
  m2 = new Integer(j).toString();
  if( m1 != null && m2.equals(request.getParameter("month")) ){
    out.println("<option selected value=" + m2 + ">" + m[j] + "</option>");
  }else{
    out.println("<option value=" + j + ">" + m[j] + "</option>");
  }
}
%>
   </select>
   <select name="year">
      <option value="none">Year</option>
<%
   Calendar cal = Calendar.getInstance();
   int year = cal.get(Calendar.YEAR);

   String y = request.getParameter("year");
   String y1;
   for(int k=1900;k<=year-2;k++)
   {
     y1 = new Integer(k).toString();
     if( y!=null && y1.equals(y) )
     {
       out.println("<option selected value=" + y1 + ">" + y1 + "</option>");
     }else{
       out.println("<option value=" + k + ">" + k + "</option>");
     }
   }
%>
   </select>
</td>
</tr>

<tr>
<td align="right"><b>Gender:</b></td>
<td>
   <select name="gender">
<%
   String g = request.getParameter("gender");
   if(g != null){
     if(g.equals("male")){
       out.println("<option value=none></option>");
       out.println("<option selected value=male>Male</option>");
       out.println("<option value=female>Female</option>");
     }else if(g.equals("female")){
       out.println("<option value=none></option>");
       out.println("<option value=male>Male</option>");
       out.println("<option selected value=female>Female</option>");
     }
   }else{
     out.println("<option value=none></option>");
     out.println("<option value=male>Male</option>");
     out.println("<option value=female>Female</option>");
   }
%>
   </select>
</td>
</tr>

<tr>
<td align="right"><b>Address:</b></td>
<td>
<%
  String addr = request.getParameter("addr");
  if(addr != null)
  {
    out.println("<input name=addr size=35 value=" + addr + ">");
  }else{
%>
<input name="addr" size=35>
<%
  }
%>
</td>
</tr>

<tr>
<td align="right"><b>Phone:</b></td>
<td>
<%
  String ph = request.getParameter("phone");
  if(ph != null)
  {
    out.println("<input name=phone value=" + ph + " size=20>");
  }else{
%>
<input name="phone" size=20>
<%
  }
%>
</td>
</tr>

<tr>
<td align="right"><b>Bank:</b></td>
<td>
   <select name="bank">
      <option value="none"></option>
<%
  int b;
  ResultSet rs,rs2;
  PreparedStatement ps = con.prepareStatement("select * from branch");
  rs = ps.executeQuery();

  while(rs.next())
  {
    b = rs.getInt("branch_code");
    if( (request.getParameter("bank"))!=null && b == Integer.parseInt(request.getParameter("bank")) ) {
      out.println("<option selected value=" + b + ">" + rs.getString("branch_name") + "</option>");
    }else{
      out.println("<option value=" + b + ">" + rs.getString("branch_name") + "</option>");
    }
  }
%>
   </select>
</td>
</tr>

<tr>
<td align="right"><b>Account type:</b></td>
<td>
   <select name="AccType">
       <option value="none"></option>
<%
  String ac;
  PreparedStatement ps1 = con.prepareStatement("select * from account_type");
  rs2 = ps1.executeQuery();

  while(rs2.next())
  {
    ac = rs2.getString("acc_type");
    if( (request.getParameter("AccType"))!=null && ac.equals(request.getParameter("AccType")) ){
      out.println("<option selected>" + ac + "</option>");
    }else{
      out.println("<option>" + ac + "</option>");
    }
  }
%>
   </select>
</td>
</tr>

<tr>
<td align="right"><b>Account No:</b></td>
<td>
<%
  String acc_no = request.getParameter("acc_no");
  if( acc_no!=null ){
    out.println("<input name=acc_no value=" + request.getParameter("acc_no") + " size=15>");
  }else{
%>
<input name="acc_no" size=15>
<%
  }
%>
</td>
</tr>

<tr>
<td align="right"><b>Balance:</b></td>
<td>
<%
  String bal = request.getParameter("bal");
  if( bal!=null ){
    out.println("<input name=bal size=15 value=" + bal + "></td>");
  }else{
%>
<input name="bal" size=15>
<%
  }
%>
</td>
</tr>

<tr>
<td align="right"><b>Username:</b></td>
<td>
<%
  String uid = request.getParameter("uid");
  if( uid != null ){
      out.println("<input name=uid size=15 value=" + uid + ">");
  }else{
%>
<input name="uid" size=15>
<%
  }
%>
</td>
</tr>

<tr>
<td align="right"><b>Password:</b></td>
<td><input type="password" name="pwd" size=15></td>
</tr>

<tr>
<td align="right"><b>Re-type Password:</b></td>
<td><input type="password" name="c_pwd" size=15></td>
</tr>

<tr>
<td colspan=2 align="middle">
<input type="submit" value="Create Account" onClick="return validate(this.form)">
<input type="reset" value="Reset">
</td>
</tr>

</table>
</form>
</center>
</body>
</html>
0
 
adnan_raisAuthor Commented:
//   CreateUserAct.jsp


<html>
<head>
<title>
Create User Action Page
</title>
</head>
<%@ page language="Java" import="java.sql.*,java.lang.*"%>
<jsp:useBean id="CreateUserActBeanId" scope="session" class="obs3.CreateUserActBean" />
<jsp:setProperty name="CreateUserActBeanId" property="*" />
<body bgcolor="lightyellow">
<center>
<br><br>
<br><br><br><br>
<%

  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection con = DriverManager.getConnection("jdbc:odbc:hello","","");

  PreparedStatement ps = con.prepareStatement("select * from customer where loginid = '" + request.getParameter("uid") + "' and account_no = " + request.getParameter("acc_no") + "");
  ResultSet rs = ps.executeQuery();

  String dbID;
  int i;
  String ac_no = request.getParameter("acc_no");
  String ac1;
  boolean insertTo = true;
  while(rs.next())
  {
    dbID = rs.getString("loginid");
    if( dbID.equals(request.getParameter("uid")) )
    {
      insertTo = false;
      response.sendRedirect("CreateUser.jsp?error=bad&title=" + request.getParameter("title") + "&fname=" + request.getParameter("fname") + "&lname=" + request.getParameter("lname") + "&father=" + request.getParameter("father") + "&addr=" + request.getParameter("addr") + "&phone=" + request.getParameter("phone") + "&bank=" + request.getParameter("bank") + "&AccType=" + request.getParameter("AccType") + "&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "&day=" + request.getParameter("day") + "&month=" + request.getParameter("month") + "&year=" + request.getParameter("year") + "&gender=" + request.getParameter("gender") + "&bal=" + request.getParameter("bal") + "");
    }
  }
  PreparedStatement ps4 = con.prepareStatement("select * from account where account_no = " + request.getParameter("acc_no") + "");
  ResultSet rs4 = ps4.executeQuery();

  while(rs4.next())
  {
    i = rs4.getInt("account_no");
    ac1 = new Integer(i).toString();
    if(ac1.equals(request.getParameter("acc_no")))
    {
      insertTo = false;
      response.sendRedirect("CreateUser.jsp?ac=bad&title=" + request.getParameter("title") + "&fname=" + request.getParameter("fname") + "&lname=" + request.getParameter("lname") + "&father=" + request.getParameter("father") + "&addr=" + request.getParameter("addr") + "&phone=" + request.getParameter("phone") + "&bank=" + request.getParameter("bank") + "&AccType=" + request.getParameter("AccType") + "&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "&day=" + request.getParameter("day") + "&month=" + request.getParameter("month") + "&year=" + request.getParameter("year") + "&gender=" + request.getParameter("gender") + "&bal=" + request.getParameter("bal") + "");
    }
  }

  if(insertTo)
  {
    Statement statement = con.createStatement();
    String strSQL = "insert into customer(loginid,branch_code,fname,lname,father_name,phone_no,address,Dob,password,Gender) values('" + request.getParameter("uid") + "'," + request.getParameter("bank") + ",'" + request.getParameter("fname") + "','" + request.getParameter("lname") + "','" + request.getParameter("father") + "'," + request.getParameter("phone") + ",'" + request.getParameter("addr") + "','" + request.getParameter("month") + "/" + request.getParameter("day") + "/" + request.getParameter("year") + "','" + request.getParameter("pwd") + "','" + request.getParameter("gender") + "')";
    statement.executeUpdate(strSQL);
    statement.close();

    Statement statement2 = con.createStatement();
    String strSQL2 = "insert into account(account_no,branch_code,acc_type,loginid,balance) values(" + request.getParameter("acc_no") + "," + request.getParameter("bank") + ",'" + request.getParameter("AccType") + "','" + request.getParameter("uid") + "'," + request.getParameter("bal") + ")";
    statement2.executeUpdate(strSQL2);
    statement2.close();
  }
%>
<font color="red" face="verdana" size=3><b>Your have successfully created your account</b></font>
<br><br><br><br>
<a href="CreateUser.jsp">Continue to Login Page</a>

</center>
</body>
</html>
0
 
adnan_raisAuthor Commented:
Do use both the files n let me know how the userID matching error could be resolved
0
 
adnan_raisAuthor Commented:
The database is built in MS Access
0
 
JNicCommented:
Hi, what a novel this is turning into!  :-)

I am pretty sure, that this error is because you did not handle SQLException in your createUserAct.jsp!

java.sql.SQLException: General error
     at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6060)

This error is typical in this case.

Jimmack, I dont think it really matters if there actually happens an SQLException or not, - it is neccesary to handle this exception in your code whenever you work with ResultSets.  
A problem when you write JSP's is that you are allowed to write db-access code that does not handle SQLException.
:-( This would never happen in a normal java-editor, since you would be required to do so.

adnan_rais, try to post your complete createUserAct.jsp - I am almost sure there is no SQLException-handling.
0
 
JNicCommented:
Ok, you posted it meanwhile :-)
0
 
jimmackCommented:
I think I've got it ;-)

>> PreparedStatement ps = con.prepareStatement("select * from customer where loginid = '" + request.getParameter("uid") + "' and account_no = " + request.getParameter("acc_no") + "");

With this statement, you are retrieving all users where both the uid and acc_no match.  So if you have the following in the database:

uid            acc_no
1234         5678
2345         6789

And the user enters:

ID: 1234,  Acc: 6789

Your result set will be empty, leading you to believe that you can add the record.

This should be solved if you change " and account_no" to " or account_no"

I *think*.
0
 
JNicCommented:
Try this:
//   CreateUserAct.jsp


<html>
<head>
<title>
Create User Action Page
</title>
</head>
<%@ page language="Java" import="java.sql.*,java.lang.*"%>
<jsp:useBean id="CreateUserActBeanId" scope="session" class="obs3.CreateUserActBean" />
<jsp:setProperty name="CreateUserActBeanId" property="*" />
<body bgcolor="lightyellow">
<center>
<br><br>
<br><br><br><br>
<%

  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection con = DriverManager.getConnection("jdbc:odbc:hello","","");

  PreparedStatement ps = con.prepareStatement("select * from customer where loginid = '" + request.getParameter("uid") + "' and account_no = " + request.getParameter("acc_no") + "");
  ResultSet rs = ps.executeQuery();

  String dbID;
  int i;
  String ac_no = request.getParameter("acc_no");
  String ac1;
  boolean insertTo = true;

try{

  while(rs.next())
  {
    dbID = rs.getString("loginid");
    if( dbID.equals(request.getParameter("uid")) )
    {
      insertTo = false;
      response.sendRedirect("CreateUser.jsp?error=bad&title=" + request.getParameter("title") + "&fname=" + request.getParameter("fname") + "&lname=" + request.getParameter("lname") + "&father=" + request.getParameter("father") + "&addr=" + request.getParameter("addr") + "&phone=" + request.getParameter("phone") + "&bank=" + request.getParameter("bank") + "&AccType=" + request.getParameter("AccType") + "&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "&day=" + request.getParameter("day") + "&month=" + request.getParameter("month") + "&year=" + request.getParameter("year") + "&gender=" + request.getParameter("gender") + "&bal=" + request.getParameter("bal") + "");
    }
  }
  PreparedStatement ps4 = con.prepareStatement("select * from account where account_no = " + request.getParameter("acc_no") + "");
  ResultSet rs4 = ps4.executeQuery();

  while(rs4.next())
  {
    i = rs4.getInt("account_no");
    ac1 = new Integer(i).toString();
    if(ac1.equals(request.getParameter("acc_no")))
    {
      insertTo = false;
      response.sendRedirect("CreateUser.jsp?ac=bad&title=" + request.getParameter("title") + "&fname=" + request.getParameter("fname") + "&lname=" + request.getParameter("lname") + "&father=" + request.getParameter("father") + "&addr=" + request.getParameter("addr") + "&phone=" + request.getParameter("phone") + "&bank=" + request.getParameter("bank") + "&AccType=" + request.getParameter("AccType") + "&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "&day=" + request.getParameter("day") + "&month=" + request.getParameter("month") + "&year=" + request.getParameter("year") + "&gender=" + request.getParameter("gender") + "&bal=" + request.getParameter("bal") + "");
    }
  }

  if(insertTo)
  {
    Statement statement = con.createStatement();
    String strSQL = "insert into customer(loginid,branch_code,fname,lname,father_name,phone_no,address,Dob,password,Gender) values('" + request.getParameter("uid") + "'," + request.getParameter("bank") + ",'" + request.getParameter("fname") + "','" + request.getParameter("lname") + "','" + request.getParameter("father") + "'," + request.getParameter("phone") + ",'" + request.getParameter("addr") + "','" + request.getParameter("month") + "/" + request.getParameter("day") + "/" + request.getParameter("year") + "','" + request.getParameter("pwd") + "','" + request.getParameter("gender") + "')";
    statement.executeUpdate(strSQL);
    statement.close();

    Statement statement2 = con.createStatement();
    String strSQL2 = "insert into account(account_no,branch_code,acc_type,loginid,balance) values(" + request.getParameter("acc_no") + "," + request.getParameter("bank") + ",'" + request.getParameter("AccType") + "','" + request.getParameter("uid") + "'," + request.getParameter("bal") + ")";
    statement2.executeUpdate(strSQL2);
    statement2.close();
  }

}
catch (SQLException e){e.printStackTrace();}
0
 
JNicCommented:
Wow, we are on different tracks here!! *LOL*
(I am also not sure) :)
0
 
jimmackCommented:
JNic.  Regarding catching the SQLException.

The reason that you can "get away with it" (or rather "without" it) ;-) in a JSP is because when the servlet container translates the JSP, it catches a generic Throwable.
0
 
jimmackCommented:
All this for 20 points ;-)
0
 
JNicCommented:
Sounds reasonable jimmack, but:

 Comment from adnan_rais
Date: 11/09/2003 06:53PM CET
 Author Comment  

Plz reply as it's urgent

 
Comment from jimmack
Date: 11/09/2003 07:03PM CET
 Comment  

The problem you have is that you are relying on the exception to be thrown when you try to insert a duplicate record.  In this particular case, you have two separate inserts.  Unfortunately, the first one succeeds, but the second one fails.

You should avoid throwing an exception in a case like this (where you can identify if there is a problem or not).  Exceptions are usually reserved for situations that you cannot easily test or control (eg. SQLExceptions are designed to handle things like incorrect SQL or problems accessing the database).

In your code, when testing (with select) whether the id or account number exist, you should set a boolean flag eg.

  boolean okToInsert = true;

  while(rs.next())
  {
    if(dbID.equals(request.getParameter("uid")))
    {
      okToInsert = false;
      response.sendRedirect("CreateUser.jsp?error=bad&uid=" + request.getParameter("uid") + "");
    }
  }

  PreparedStatement ps4 = con.prepareStatement("select * from account where account_no = " + request.getParameter("acc_no") + "");
  ResultSet rs4 = ps4.executeQuery();

  while(rs4.next())
  {
    int i = rs4.getInt("account_no");
    String ac1 = new Integer(i).toString();
    if(ac1.equals(request.getParameter("acc_no")))
    {
      okToInsert = false;
      response.sendRedirect("CreateUser.jsp?ac=bad&acc_no=" + request.getParameter("acc_no") + "&uid=" + request.getParameter("uid") + "");
    }
  }

  if (okToInsert)
  {
    Statement statement = con.createStatement();
    String strSQL = "insert into customer(loginid,password) values('" + request.getParameter("uid") + "','" + request.getParameter("pwd") + "')";
    .
    .


 
Comment from JNic
Date: 11/09/2003 08:57PM CET
 Your Comment  

jimmack is right!

This way you also avoid the problems mentioned in the end of:

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20791821.html

- The jsp will never execute the update, if okToInsert is false.

 
Comment from adnan_rais
Date: 11/09/2003 09:28PM CET
 Author Comment  

ok jammick ur code worked a lot but still displays an error in userID matching case. Here is the error code

type Exception report

message Internal Server Error

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

exception

javax.servlet.ServletException: General error
     at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:463)
     at org.apache.jsp.CreateUserAct$jsp._jspService(CreateUserAct$jsp.java:157)
     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012)
     at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107)
     at java.lang.Thread.run(Thread.java:484)


root cause

java.sql.SQLException: General error
     at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6060)
     at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6188)
     at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:2494)
     at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:334)
     at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:284)
     at org.apache.jsp.CreateUserAct$jsp._jspService(CreateUserAct$jsp.java:140)
     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)
     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)
     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2343)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
     at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
     at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
     at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1012)
     at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1107)
     at java.lang.Thread.run(Thread.java:484)

What to do next?

 
Comment from jimmack
Date: 11/09/2003 09:35PM CET
 Comment  

The problem has moved now ;-)  It's now in the file CreateUserAct.jsp.

This seems to imply that within that file you are trying to access the database again, but perhaps you are missing some data (because no account has been created?).

Is there any chance you could post that file?

 
Comment from JNic
Date: 11/09/2003 09:47PM CET
 Your Comment  

Have you forgotten try/catch blocks in CreateUserAct.jsp ? Remember, you must handle an eventual SQLException when you work with ResultSets.

 
Comment from jimmack
Date: 11/09/2003 09:48PM CET
 Comment  

JNic, even if this is the case, it's more likely something that should be tested, rather than thrown ;-)

 
Comment from adnan_rais
Date: 11/09/2003 10:14PM CET
 Author Comment  

<%
  String err = request.getParameter("error");
  String err1 = request.getParameter("ac");
  if( err != null )
  {
    out.println("<tr><td align=middle colspan=2><b><font color=red size=3>Username already exists</font></b></td></tr>");
  }else if( err1 != null )
  {
    out.println("<tr><td align=middle colspan=2><b><font color=red size=3>Account # already exists for some other user</font></b></td></tr>");
  }
%>


 
Comment from adnan_rais
Date: 11/09/2003 10:21PM CET
 Author Comment  

any idea jimmack... i've posted u a little code  from CreateUser.jsp

i think it would b enough to u


 
Comment from jimmack
Date: 11/09/2003 10:29PM CET
 Comment  

I can't see anything wrong with that ;-)

The problem is an SQLException though.  Do you do any SQL handling in the this JSP?

You could check in the "work" directory under your servlet container installation directory.  In there, you should find a file called something like CreateUserAct_jsp.java.

The problem is occurring (originally) at line 140.

>>      at org.apache.jsp.CreateUserAct$jsp._jspService(CreateUserAct$jsp.java:140)

I may have made an error in my last posting.  Do you have JSPs called CreateUser.jsp and one called CreateUserAct.jsp?  If so, the problem is in the second one ;-)

 
Comment from adnan_rais
Date: 11/09/2003 10:34PM CET
 Author Comment  

yes I've created the pages named CreateUser.jsp and CreateUserAct.jsp..... How to handle the error in that file at line 140


 
Comment from adnan_rais
Date: 11/09/2003 10:36PM CET
 Author Comment  

no the page doesn't contain any Exception handling after pasting ur code

 
Comment from adnan_rais
Date: 11/09/2003 10:58PM CET
 Author Comment  

How to check the work directory in JBuilder.... The file is not named as CreateUserAct.java but it is CreateUserBean.java n there's no such coding upto line 140

 
Comment from adnan_rais
Date: 11/09/2003 11:10PM CET
 Author Comment  

Here is the code of CreateUserAct.java file

package org.apache.jsp;

import java.sql.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;


public class CreateUserAct$jsp extends HttpJspBase {

    // begin [file="/CreateUserAct.jsp";from=(7,0);to=(7,87)]
    // end

    static {
    }
    public CreateUserAct$jsp( ) {
    }

    private static boolean _jspx_inited = false;

    public final void _jspx_init() throws org.apache.jasper.runtime.JspException {
    }

    public void _jspService(HttpServletRequest request, HttpServletResponse  response)
        throws java.io.IOException, ServletException {

 
 I see no SQLException?? :-o
0
 
JNicCommented:
ARH SORRY!!! :-(

Forget that huge post!!

Here is what I meant to say:

Sounds reasonable jimmack, but:

Here is the code of CreateUserAct.java file

package org.apache.jsp;

import java.sql.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;


public class CreateUserAct$jsp extends HttpJspBase {

    // begin [file="/CreateUserAct.jsp";from=(7,0);to=(7,87)]
    // end

    static {
    }
    public CreateUserAct$jsp( ) {
    }

    private static boolean _jspx_inited = false;

    public final void _jspx_init() throws org.apache.jasper.runtime.JspException {
    }

    public void _jspService(HttpServletRequest request, HttpServletResponse  response)
        throws java.io.IOException, ServletException {

 
 I see no SQLException?? :-o
0
 
jimmackCommented:
JNic.  I don't think it was really necessary to post all that in an already excessive thread :-(

For your reference (and from your previous post)

>> root cause
>>
>> java.sql.SQLException: General error
0
 
JNicCommented:
Yeah it shows that we do not want to earn points!!  ;-)
0
 
JNicCommented:
The big posting was an error, - I 'm sorry :-(

I meant, that I did not see CreateUserAct$jsp throwing any SQLException.
0
 
jimmackCommented:
Sorry adnan_rais,

Did you catch my last relevant post:

 I think I've got it ;-)

>> PreparedStatement ps = con.prepareStatement("select * from customer where loginid = '" + request.getParameter("uid") + "' and account_no = " + request.getParameter("acc_no") + "");

With this statement, you are retrieving all users where both the uid and acc_no match.  So if you have the following in the database:

uid            acc_no
1234         5678
2345         6789

And the user enters:

ID: 1234,  Acc: 6789

Your result set will be empty, leading you to believe that you can add the record.

This should be solved if you change " and account_no" to " or account_no"

I *think*.
0
 
adnan_raisAuthor Commented:
I'm sorry but it's all my mistake not to report u at the very beginning. The account_no doesn't belong to 'customer' table. It is a separate field defined for 'account' table. That's why I was writing the queries separately. What should b the code then?
0
 
adnan_raisAuthor Commented:
Thanx for all ur help jimmack

I got my mistake

For matching the userID's in the table, I earlier used

>> PreparedStatement ps = con.prepareStatement("select * from customer where loginid = '" + request.getParameter("uid") + "' and account_no = " + request.getParameter("acc_no") + "");
 
But as account_no doesn't belong to 'customer' table, therefore, I replaced the query as

>> PreparedStatement ps = con.prepareStatement("select * from customer where loginid = '" + request.getParameter("uid") + "' );

Thanx for all ur help, n JNic too
0
 
JNicCommented:
Give the points to jimmack, - yes yes all 20 of them ;-)

Good luck on your project adnan_rais!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 16
  • 12
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now