this code has following error
--it ensures that the username is unique but still insert it into database
please help me to debug this error.

Recommended Answers

All 3 Replies

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.sql.*;
    import java.util.*;
    public class student extends HttpServlet
    {
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    {
    res.setContentType("text/html");
    PrintWriter out=res.getWriter();


    String x=req.getParameter("r3");
    String y=req.getParameter("r4");
    String p=req.getParameter("r1");
    String q=req.getParameter("r5");
    String r=req.getParameter("r2");
    String w=req.getParameter("r6");
    String a=req.getParameter("r7");
    String b=req.getParameter("r8");
    String c=req.getParameter("r9");
    String d=req.getParameter("r");
    String f=req.getParameter("s");

    String g=req.getParameter("mail");






    System.out.println(x);
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:portaldbsh","","");
    Statement st=con.createStatement();

    ArrayList<String> lis=new ArrayList<String>();
    ResultSet rs=st.executeQuery("select * from Table6");
    while(rs.next()!=false)
    {
    lis.add(rs.getString(1));
    }
    System.out.println(lis);
     Iterator<String> itr = lis.iterator();
    while(itr.hasNext())
    {
     String well= itr.next();


    if(well.compareTo(x)==0)
    {
    res.sendRedirect("/demosh/invalid.jsp");
    }
    }

this code has following error
--it ensures that the username is unique but still insert it into database...
Please help me to debug this error

    st.executeUpdate("insert into Table6 values('"+x+"','"+y+"','"+p+"','"+q+"','"+r+"','"+w+"','"+b+"','"+a+"','"+c+"','"+g+"','"+d+"','"+f+"')");


    out.println("<html><body bgcolor='lightblue'><h1><center>You are succesfully registered<br>Thank You</center></h1> <br><a href='student.jsp'>Login Now</a></body></html>");
    }


    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }

instead of compareTo,use equals() method to match two strings.

Just try it.

I feel it is a logical error .
You are doing a redirect inside a while loop;

I feel instead of this declare a boolean variable assign its value=true even before you iterate over the list.
Then inside the if condition block (when the result of if condition is true) make the value of boolean variable as "false" and break the while loop immediately from if block.
Then immediately outside the while block check for the value of boolean variable , if its value is false then call the redirect. If the value of boolean variable is true only then do the insert operation.

boolean checkId=true;
while(itr.hasNext())
   {
     String well= itr.next();
    if(well.compareTo(x)==0)
    {
       checkId=false;
       break;
    }
 }
  if(!checkId)  {
     res.sendRedirect("/demosh/invalid.jsp");
  }else{
       st.executeUpdate("insert into Table6 values('"+x+"','"+y+"','"+p+"','"+q+"','"+r+"','"+w+"','"+b+"','"+a+"','"+c+"','"+g+"','"+d+"','"+f+"')");
    out.println("<html><body bgcolor='lightblue'><h1><center>You are succesfully registered<br>Thank You</center></h1> <br><a href='student.jsp'>Login Now</a></body></html>");

  }

This will guarantee that insert will not happen when boolean variable is false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.