request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring","root","123456");
String sqlQuery = "select count(*) from user where username = ?";
PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
psQuery.setString(1,username);
ResultSet rs = psQuery.executeQuery();
int count = 0;
if(rs.next()){
count = rs.getInt(1);
}
if(count > 0){
response.sendRedirect("registerFail.jsp");
psQuery.close();
conn.close();
return;
}
String sql = "insert into user values(null,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ps.executeUpdate();
ps.close();
conn.close();
response.sendRedirect("registerSuccess.jsp");
简单的用户注册,jdbc连接,以防忘记