📄 regdatabean.java
字号:
package com.dataquery.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import com.dataquery.util.Status;
import com.dataquery.util.TranString;
public class RegDataBean implements ProcessData {
public void execute(HttpServletRequest request, Status status) throws Exception {
String name = request.getParameter("name");
String password = request.getParameter("password");
String rePassword = request.getParameter("rePassword");
String email = request.getParameter("email");
//校验数据
if ((name==null) || (name.length()==0)) {
Exception e = new Exception("请输入姓名的值.");
status.addException(e);
}
if ((password==null) || (password.length()==0)) {
Exception e = new Exception("请输入密码的值.");
status.addException(e);
}
if ((rePassword==null) || (rePassword.length()==0)) {
Exception e = new Exception("请输入确认密码的值.");
status.addException(e);
}
if ((password.length()!=0) && (rePassword.length()!=0) && (!password.equals(rePassword))){
Exception e = new Exception("输入密码的值不相等.");
status.addException(e);
}
if (!status.isSuccessful()) {
throw new Exception("数据校验有误...");
}
//数据业务处理
name = TranString.encoding(name); //编码转换
String id = new Long(System.currentTimeMillis()).toString(); //根据系统时间产生毫秒数来表示ID
DBConnection db = new DBConnection();
try {
Connection conn = db.getConnection();
String sql = "insert into regmessage (id,name,password,email,regdate) values (?,?,?,?,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,id);
stmt.setString(2,name);
stmt.setString(3,password);
stmt.setString(4,email);
stmt.setString(5,new Date().toString());
stmt.executeUpdate();
String flag = "操作成功!";
request.setAttribute("Result",flag); //将成功标记作为request属性存储
} catch (SQLException e) {
e.printStackTrace();
Exception ex = new Exception("操作失败,请检查日志......");
status.addException(ex);
throw ex; //表示该方法是发生异常,而非正常结束
} finally {
db.closeConnection();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -