无法使用数据库中的最后一个值自动更新注册ID

时间:2014-08-24 13:10:46

标签: java javascript database jsp servlets

我想在注册表单中使用更新的注册ID,即在向数据库中的最后一个ID添加一个ID之后使用ID。我已将登录ID作为标签,我猜不能在标签上使用request.getParameter("lid")。此外,只有ID不会存储在数据库中,它是空白的,并且每个其他条目都被存储。

代码:

//register.jsp

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

<html>
  <head> 
    <script type="text/javascript">

      function ckh()
      {    
        var fn=document.rform.fname.value;
        var ln=document.rform.lname.value;
        var lad=document.rform.laddr.value;
        var pad=document.rform.paddr.value;
        var mo=document.rform.mno.value;
        var em=document.rform.email.value;
        var pd=document.rform.pwd.value;
        var a=isNaN(mo);
        var nam1=isNaN(fn);
        var nam2=isNaN(ln);
        var atpos=em.indexOf("@");
        var dtpos=em.lastIndexOf(".");

        var msg="";

        if(fn.length==0)
        {
          msg=msg+"First Name can not be blank! ";
        }

        if(ln.length==0)
        {
          msg=msg+"Last Name can not be blank! ";
        }

        if(!nam1 || !nam2)
        {
          msg=msg+"Don't be oversmart! Enter valid name! "
        }

        if(lad.length==0)
        {
          msg=msg+"Address can not be blank! "; 
        }

        if(pad.length==0)
        {
          msg=msg+"City Name can not be blank! ";
        }

        if(mo.length==0)
        {
          msg=msg+"Please enter mobile number! ";
        }

        if(em.length==0)
        {
          msg=msg+"Enter Email ID! ";
        }

        if(atpos<3 || dtpos<(atpos+5) || dtpos+2>em.length)
        {
          msg+="Invalid Email! Hint:xyz@gmail.com! ";
        }

        if(pd.length==0)
        {
          msg=msg+"Password can not be blank! ";
        }

        if(a)
        {
          msg=msg+"Please enter valid phone number! ";
        }


        if(msg.length==0)
        {
          rform.submit();
        }
        else
        {
          alert(msg);
          return false;

        }
     }

   </script>

   <title>Shop Online</title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <link href="style/style.css" rel="stylesheet" type="text/css" />
 </head>

<body background="images/background3.jpg">
  <center>

    <br>
    <br>

    <div class="wrapper">
      <div class="logo"> Shop<strong>Online</strong></div>
      <div class="menu">
        <ul class="solidblockmenu">
          <li><a href="index.html">Home</a></li>
          <li><a href="login.jsp">LOGIN</a></li>
          <li><a href="register.jsp">REGISTER</a></li>
          <li><a href="http://www.google.com">PRODUCTS</a></li>
          <li><a href="">CONTACT US</a></li>
        </ul>
        <div class="clear"></div>
      </div>
    </div>
    <br>
    <br>

    <% Class.forName("oracle.jdbc.OracleDriver");
       Connection con=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","system","manhattan");
       PreparedStatement ps=con.prepareStatement("select *from uinfo");
       ResultSet rs=ps.executeQuery();
       int n=0;
       while(rs.next())
       {
         n=rs.getInt("lid");

       }
       n++;

       //out.println(n);


       con.close();

      %>

     <form name="rform" action="register" method="post" onsubmit="return ckh()">
        <table border="0" cellspacing="11"  cellpadding="11"                                align="center"                          bgcolor="skyblue">
            <thead>
                <tr>
                    <th colspan="4" align="center">
                        REGISTRATION FORM
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        First Name
                    </td>
                    <td>
                        <input type="text" name="fname">
                    </td>
                    <td>
                        Last Name
                    </td>
                    <td>
                        <input type="text" name="lname">
                    </td>
                </tr>
                <tr>
                    <td>
                        Address
                    </td>
                    <td>
                        <input type="text" name="laddr">
                    </td>
                    <td>
                        City
                    </td>
                    <td>
                        <input type="text" name="paddr">
                    </td>
                </tr>
                <tr>
                    <td>
                        Mobile No
                    </td>
                    <td>
                        <input type="text" name="mno">
                    </td>
                    <td>
                        Email
                    </td>
                    <td>
                        <input type="text" name="email">
                    </td>
                </tr>
                <tr>
                    <td>
                        Login id
                    </td>
                    <td>
                        <label name="lid"><%=n%></label>
                    </td>
                    <td>
                        Password
                    </td>
                    <td>
                        <input type="password" name="pwd">
                    </td>
                </tr>
                <tr>
                    <td colspan="4" align="center">
                        <input type="submit" value="New User">
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
</center>
</body>
</html>



  //register servlet (RegiServlet.java)- Mapping already done in web.xml

public class RegiServlet extends HttpServlet 
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String fname=request.getParameter("fname");
    String lname=request.getParameter("lname");
    String laddr=request.getParameter("laddr");
    String paddr=request.getParameter("paddr");
    String mno=request.getParameter("mno");
    String email=request.getParameter("email");
    String lid=request.getParameter("lid");
    String pwd=request.getParameter("pwd");

    try {

        DaoUtil du=new DaoUtil();
    int i=du.ins(fname, lname, laddr, paddr, mno, email, lid, pwd);
    if(i==1)
    {
     String fnam= fname;
     HttpSession ses=request.getSession();
        ses.setAttribute("xyz",fnam);
        response.sendRedirect("thanks.jsp");
         }

    else
    {
        response.sendRedirect("sorry.jsp");
    }
} catch (Exception e) {
}
}
}


//DaoUtil.java

public class DaoUtil
{
private Connection con;
public void connect()
{

    try {
        Class.forName("oracle.jdbc.OracleDriver");

                                                                                                         con=DriverManager.getConnection
         ("jdbc:oracle:thin:@127.0.0.1:1521:xe","system","manhattan");
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
 }
public int ins(String fname,String lname,String laddr,String paddr,String ph,String   email,String lid,
        String pwd)
 {
    int result=0;
    PreparedStatement ps;
    try {
        connect();
        ps=con.prepareStatement("insert into uinfo values(?,?,?,?,?,?,?,?)");
        ps.setString(1, fname);
        ps.setString(2, lname);
        ps.setString(3, laddr);
        ps.setString(4, paddr);
        ps.setString(5, ph);
        ps.setString(6, email);
        ps.setString(7, lid);
        ps.setString(8, pwd);
        result=ps.executeUpdate();
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
    return result;
 }
 public ResultSet check(String lid,String pwd)
 {
    ResultSet rs=null;
    try {
        connect();
        PreparedStatement ps=con.prepareStatement
                ("select fname from uinfo where lid=? and pwd=?");
        ps.setString(1, lid);
        ps.setString(2,pwd);
        rs=ps.executeQuery();
    } catch (Exception e) 
    {
    }
    return rs;
 }
 public ResultSet itemdtl()
 {
    ResultSet rs1=null;
    try {
        connect();
        PreparedStatement ps=con.prepareStatement
                ("select itemid,itemdesc,price from item_price");
           rs1=ps.executeQuery();
    } catch (Exception e) 
    {
        System.out.println("ERROR IN itemdt1");
    }
    return rs1;
}
}

1 个答案:

答案 0 :(得分:0)

你的猜测是正确的。可能无法使用request.getParameter("lid")收集标签值。 相反,您可以添加一个隐藏的输入来传递值。

替换

<td>
   <label name="lid"><%=n%></label>
</td>

<td>
   <label name="lid_lbl"><%=n%></label>
   <input type="hidden" name="lid" value="<%=n%>" >
</td>