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

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

General Database error

Hello..... I'm developing a sign up page for the new users in JSP. As I enter a new entry with a new loginid, it successfully gets added to the database, but when i try to check the result on entering the old id, it returns some kind of "General Error", means it does not move to "CreateUser.jsp?error=bad" for showing the error, "Username already exists". Plz do let me know the solution immediately.... I'e worked for hours on it but culdn't find the solution yet

// CreateUserAct.jsp

<%
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

  Connection con = DriverManager.getConnection("jdbc:odbc:hello","","");

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

  String dbID = request.getParameter("uid");
  while(rs1.next())
  {
    if( dbID.equals(request.getParameter("uid")) )
    {
      response.sendRedirect("CreateUser.jsp?error=bad&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();
%>

// CreateUser.jsp

  String uid = request.getParameter("uid");
  if( uid != null ){
      out.println("<input name=uid size=15 value=" + uid + ">");
  }
0
adnan_rais
Asked:
adnan_rais
  • 8
  • 8
1 Solution
 
adnan_raisAuthor Commented:
bobbit31, kotan if u do know the solution plz help me out then
0
 
adnan_raisAuthor Commented:
The JBuilder returns the error as


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:136)
      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:124)
      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)

0
 
JNicCommented:
Please post the error-message you receive in details
0
Technology Partners: 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!

 
JNicCommented:
ok! You posted while I asked for it ;-)
0
 
adnan_raisAuthor Commented:
u've been a gr8 help to me JNic.... plz do let me know it's correct solution immediately
0
 
JNicCommented:
I am not perfectly sure, but I think it is because the jsp doesn't know how to handle the SQLException.

Try to embed your code in a TRY-block, and do a SQLException-catch after. Like this:

// CreateUserAct.jsp

<%
try{
 
 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

  Connection con = DriverManager.getConnection("jdbc:odbc:hello","","");

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

  String dbID = request.getParameter("uid");
  while(rs1.next())
  {
    if( dbID.equals(request.getParameter("uid")) )
    {
      response.sendRedirect("CreateUser.jsp?error=bad&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();
%>

// CreateUser.jsp

  String uid = request.getParameter("uid");
  if( uid != null ){
      out.println("<input name=uid size=15 value=" + uid + ">");
  }
}
catch(SQLException e){e.printStackTrace();}


This is a problem when you write jsps instead of java-classes; in regular java I am sure your editor would have objected because you did not handle the SQLException.
0
 
adnan_raisAuthor Commented:
Thanx JNic ur code worked but just 1 more simple question to ask......


Suppose the page url is

http://localhost/CreateUser.jsp?error=bad&addr=uhjggfytfdtdresdresres+jknjjhgjghfftrdfd

As Itry to retrieve the value of variable 'addr' from the url using request.getParameter(), it returns the characters before the first spcace, i.e. , uhjggfytfdtdresdresres in the 'address' textfield. Why is it so? I need the complete data to be displayed in the text field. Just 1 more answer plzz.....
0
 
JNicCommented:
Hmmmm, cant see any error in that url.
String address = request.getParameter("addr"); should work.
Try posting your code.
0
 
JNicCommented:
0
 
adnan_raisAuthor Commented:
Hi JNic.... the matter isn't solved yet... 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_raisAuthor Commented:
i know it's too much to ask for just 20 points but belive me it's the need
0
 
JNicCommented:
Hi, you wrote:

 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") + "");
    }
  }

Note that: if(dbID.equals(request.getParameter("uid")))  always will return TRUE!

Maybe you meant: if(rs.getString("uid").equals(dbID))  ??


About problem 3:

I think I will need more of your code to answer this. Where do the error-messages come from? (ie "username allready exists" aso). I dont see them in your code...
Let me see some more.... ;-)
0
 
adnan_raisAuthor Commented:
They come from CreateUser.jsp . The code is as follows:

<%
  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
 
JNicCommented:
I am not sure about this, but try to put a return statement after your redirects. - Maybe the rest of the jsp is exectuted before the redirect works. A return should stop the jsp-exectution immediately.


Fx:
{
   response.sendRedirect("CreateUser.jsp?error=bad&uid=" + request.getParameter("uid") + "");
   return;
}

Do it both places where you have response.sendRedirect, and let me know if it helps.
0
 
adnan_raisAuthor Commented:
no it didn't
0
 
JNicCommented:
OK -  I changed some parts of your code. Try it.


<%
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(rs.getString("loginid").equals(dbID)))  //CHANGED
    {
      response.sendRedirect("CreateUser.jsp?error=bad&uid=" + request.getParameter("uid") + "");
      return;  //CHANGED
    }
  }

  PreparedStatement ps4 = con.prepareStatement("select * from account where account_no = '" + request.getParameter("acc_no") + "'"); // CHANGED - single quation-marks added around 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") + "");
return; // CHANGED
    }
  }

  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
 
paragagarwalCommented:
The problem is that you are not checking your login id with the database ids.

The code that u have written:-
String dbID = request.getParameter("uid");
  while(rs1.next())
  {
//this statement will always be true. here u should check like this.
if( dbID.equals(rs1.getString("uid") )  
Then it will give u proper result.
 
if( dbID.equals(request.getParameter("uid")) )
    {
      response.sendRedirect("CreateUser.jsp?error=bad&uid=" + request.getParameter("uid") + "");
    }
  }

 
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now