@SuppressWarnings("serial")
public class Registration extends HttpServlet {
@SuppressWarnings("serial")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String mysqlUrl = "jdbc:mysql://localhost:3306/attendance";
String mysqlUserName = "root";
String mysqlPassword = "cqut";
// 连接到数据库
Connection conn;
conn = getConnection(mysqlUrl, mysqlUserName, mysqlPassword);
String roleName = correctMessycode(request,request.getParameter("roleID"));
String sql0 = "SELECT roleID from role where name='" + roleName + "'";
String roleID = "";
try {
// 数据操作
PreparedStatement preparedStatement0 = conn.prepareStatement(sql0);
ResultSet _rs = preparedStatement0.executeQuery();
while (_rs.next()) {
roleID = _rs.getString("roleID");
}
} catch (Exception e) {
System.out.println(e.toString());
}
String userID = EntityIDFactory.createId();
String loginName = request.getParameter("loginName");
String phoneNumber = request.getParameter("phoneNumber");
String qqnumber = request.getParameter("qqNumber");
String email = request.getParameter("email");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String surePassword = request.getParameter("surePassword");
String sql = "insert into user (userID,loginName,phoneNumber,QQ,email,userName,password,roleID)"
+ "values ('"
+ userID
+ "','"
+ loginName
+ "','"
+ phoneNumber
+ "','"
+ qqnumber
+ "','"
+ email
+ "','"
+ userName + "','" + password + "','" + roleID + "')";
System.out.println("sql:" + sql);
try {
// 数据操作
PreparedStatement preparedStatement = conn.prepareStatement(sql);
int i = preparedStatement.executeUpdate();
System.out.print(i);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
// 关闭数据库连接
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 访问后台之后返回字符串“true”
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("true");
out.flush();
out.close();
}
private static final String DRIVERNAME = "com.mysql.jdbc.Driver";
// 连接数据库方法
public static Connection getConnection(String url, String userName,
String password) {
Connection conn = null;
try {
Class.forName(DRIVERNAME);
conn = DriverManager.getConnection(url, userName, password);
System.out.print(conn.toString());
} catch (ClassNotFoundException e) {
// 检查驱动不可用,执行此语句
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//纠正乱码情况
public String correctMessycode(HttpServletRequest request,String param) throws UnsupportedEncodingException{
/*
* 在地址栏输入默认是gbk,而get传参数是要先编码为iso8859-1再传的,
* 所以要先用iso-8859-1来解码,再用utf-8编码为一个新字符串
* */
String str = "";
boolean isIE = request.getHeader( "USER-AGENT" ).toLowerCase().indexOf( "msie" ) > 0 ? true : false ;
if(isIE){
str = new String(request.getParameter("qq").getBytes("iso-8859-1"),"gbk"); // IE
}else{
str = new String(request.getParameter("qq").getBytes("iso-8859-1"),"utf-8");// FF, Chrome
}
return str;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
servlet交互案例分析及终极实现 使用jdbc连接数据库实现
最新推荐文章于 2022-03-14 10:48:01 发布