和插入语句相似,表单传值,在另一个页面接收数据并连接数据库进行更新;
语句如下:
<% request.setCharacterEncoding("UTF-8"); String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=Users"; Connection conn=null; String userName="sa"; String pwd="xxxx"; //Class.forName(driverName); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection(dbURL,userName,pwd); String sql="update Users2 set password=?,question=?,answer=?,gender=? where username=?;"; PreparedStatement stmp=conn.prepareStatement(sql); String username=request.getParameter("User"); String password=request.getParameter("pwd"); String question=request.getParameter("secret"); String answer=request.getParameter("pwd2"); String gender=request.getParameter("sexy"); stmp.setString(5,username); stmp.setString(1,password); stmp.setString(2,question); stmp.setString(3,answer); stmp.setString(4,gender); int n=stmp.executeUpdate(); if(n==1){%>数据修改成功!<br><%} else{%>数据修改失败!<br><%} if(stmp!=null){ stmp.close(); } if(conn!=null){ conn.close(); } %>