从JSP更新MySQl数据库

时间:2015-08-13 09:27:45

标签: javascript java html jsp

我编写了一个表单来更新用户的登录详细信息。但更新并不成功。

我是JSP的新手,所以请点击“保存”按钮,帮助我将用户数据更新到MySQL数据库。

我的代码如下所示;

<%
String CONN_STRING = "jdbc:mysql://localhost/pmsdb";
String USERNAME = "dbuser";
String PASSWORD = "dbpassword";

Connection conn=null;
PreparedStatement pst=null;
ResultSet rs =null;

if(request.getParameter("submit")!=null){
    String cuser=request.getParameter("currentusername"); 
    String user = request.getParameter("username");
    String pwd = request.getParameter("password");
    String cpwd = request.getParameter("confirmpasword");
    Class.forName("com.mysql.jdbc.Driver");

     conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
     String sql = "UPDATE supplierinfo SET username='"+user+"', password='"+pwd+"' WHERE username='"+cuser+"' ";

     if(cuser!=null && user!=null && pwd!=null && cpwd!=null  ){
     pst=conn.prepareStatement(sql);
     pst.execute(); 
     }else{
     %>
     <script language="JavaScript">
         alert("Fill all fields and try again")
     </script>
     <%
     }
}
    %>
  <div class="row">
  <div class="col-md-6 col-md-offset-3 settingbox">
    <div class="panel panel-login">
      <div class="panel-heading">
        <div class="row">
          <div class="col-xs-6">
            <a href="#" class="active" id="login-form-link">Login Details</a>
          </div>
          <div class="col-xs-6">
            <a href="#" id="register-form-link">General Details</a>
          </div>
        </div>
        <hr>
      </div>
      <div class="panel-body">
        <div class="row">
          <div class="col-lg-12">
            <form id="login-form" action="" method="post" role="form" style="display: block;">
              <div class="form-group">
                <input type="text" name="currentusername" id="currentusername" tabindex="1" class="form-control" placeholder="Current Username" value="">
              </div>
              <div class="form-group">
                <input type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="New Username" value="">
              </div>
              <div class="form-group">
                <input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="New Password">
              </div>
              <div class="form-group">
                <input type="password" name="confirmpassword" id="password" tabindex="2" class="form-control" placeholder="Confirm New Password">
              </div>
              <div class="form-group">
                <div class="row">
                  <div class="col-sm-6 col-sm-offset-3">
                    <input type="submit" name="submit" id="submit" tabindex="4" class="form-control btn btn-login" value="Save">
                  </div>
                </div>
              </div>

2 个答案:

答案 0 :(得分:1)

问题出在这一行:

String cpwd = request.getParameter("confirmpasword");

请求附加:

<input type="password" name="confirmpassword" id="password"...

Java控制器尝试获取null参数。 (您在s参数)

中缺少getParameter个字符

考虑使用简单的方法验证表单字段是否为空和空状态,如:

public boolean isSet(String s){
    if(s==null || "".equals(s)) return false; //String not set
    return true;
}

答案 1 :(得分:1)

在阅读form中的数据时,您使用了confirmpasword代替confirmpassword

更改以下内容

 String cpwd = request.getParameter("confirmpasword");

String cpwd = request.getParameter("confirmpassword");

还有一件事是提交表单字段时,null将发送textbox个文件。

将您的if条件更改为以下内容,它会检查空值以及empty sting

if((cuser!=null &&cuser.length()>0) 
 && (user!=null &&user.length()>0)  
 && (pwd!=null && pwd.length()>0)
 && cpwd!=null && cpwd.length()>0) ) {
 // statements
}