|
Java学习者论壇
遇到一个问题,是数据库的更新操作,不报错但也不更新希望高人指点一二~
這个是JSP页面代码:
<body>
<form action="../Servlet/UsersUpdateServlet" method="post" name="upuser">
账户名称:<input type="text" name="name" value="${name}"/>
账戶密碼:<input type="password" name="password" value="${password}"/>
性別{sex}
密码問题:<input type="text" name="questions" value="${questions}"/>
密码答案:<input type="text" name="answer" value="${answer}"/>
身份證號:<input type="text" name="idCard" value="${idCard}" readonly="readonly" />
注册日期:${regTime}
<input type="submit" value="修改"/>
</form>
</body>
这个是servlet代码:
package com.project.user.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.project.dao.DaoFactory;
import com.project.user.beans.Users;
import com.sun.org.apache.commons.beanutils.BeanUtils;
public class UsersUpdateServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String name = (String) session.getAttribute("name");
String password = (String) session.getAttribute("password");
String questions = (String) session.getAttribute("questions");
String answer = (String) session.getAttribute("answer");
String idCard = (String) session.getAttribute("idCard");
Users users = new Users();
users.setName(name);
users.setPassword(password);
users.setQuestions(questions);
users.setAnswer(answer);
users.setIdCard(idCard);
try {
DaoFactory.getIUsersDaoInstance().update(users);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这个是更新方法代码:
public void update(Users users) throws Exception {
Connection con = null;
PreparedStatement ps = null;
String sql = "update Pro_user set Pro_name=?, Pro_password=?, Pro_questions=?, Pro_answer=? where Pro_idCard=?";
try{
con = DatabaseUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, users.getName());
ps.setString(2, users.getPassword());
ps.setString(3, users.getQuestions());
ps.setString(4, users.getAnswer());
ps.setString(5, users.getIdCard());
ps.executeUpdate();
ps.close();
}catch(Exception e){
e.printStackTrace();
}finally{
DatabaseUtil.closeConnection(con);
}
}
这个是PO类代码:
package com.project.user.beans;
import java.util.Date;
import java.util.List;
public class Users {
private String name;
private String password;
private String sex;
private String questions;
private String answer;
private String idCard;
private Date regTime;
private List list;
private int id;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the sex
*/
public String getSex() {
return sex;
}
/**
* @param sex the sex to set
*/
public void setSex(String sex) {
this.sex = sex;
}
/**
* @return the questions
*/
public String getQuestions() {
return questions;
}
/**
* @param questions the questions to set
*/
public void setQuestions(String questions) {
this.questions = questions;
}
/**
* @return the answer
*/
public String getAnswer() {
return answer;
}
/**
* @param answer the answer to set
*/
public void setAnswer(String answer) {
this.answer = answer;
}
/**
* @return the idCard
*/
public String getIdCard() {
return idCard;
}
/**
* @param idCard the idCard to set
*/
public void setIdCard(String idCard) {
this.idCard = idCard;
}
/**
* @return the regTime
*/
public Date getRegTime() {
return regTime;
}
/**
* @param regTime the regTime to set
*/
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
/**
* @return the list
*/
public List getList() {
return list;
}
/**
* @param list the list to set
*/
public void setList(List list) {
this.list = list;
}
}
谢谢~~
欢迎来到Java学习者论坛,转载请注明地址:http://www.javaxxz.com. |
|