错误
servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
MD5 md = new MD5();
user n = new user();// 创建user对象
userDao nDao = new userDao();// 创建userDao实例
try{
String yhn = (String)request.getParameter("yhName");
String yha = (String)request.getParameter("yhAccount");
String pw = (String)request.getParameter("password");
String pw1 =md.toMD5(pw);//获取表单传过来的数据
n.setYhName(yhn);
n.setYhAccount(yha);
n.setPassword(pw1);//将数据存入cb对象中
try{
nDao.addUser(n);//调用contactBiaoDao中的addContactBiao方法将对象插入数据库
}catch(SQLException e){
e.printStackTrace();}
System.out.println("插入成功");
} catch(Exception e1){
e1.printStackTrace();
}
dao:
public void addUser(user u) throws SQLException {
Connection conn = DBUtil.getConnection();
if(conn!=null){
try{
String sql = "Insert into user " + "(yhName,yhAccount,password) "
+ "values(?,?,?) ";
PreparedStatement ptmt = conn.prepareStatement(sql);
ptmt.setString(1, u.getYhName());
ptmt.setString(2, u.getYhAccount());
ptmt.setString(3, u.getPassword());
ptmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
}
}
数据库:
