58,454
社区成员
发帖
与我相关
我的任务
分享
Connection conn = null;// 连接类
PreparedStatement pstmt = null;// 预处理类 Statement类
PreparedStatement pst = null;
try {
conn = DriverManager.getConnection(url, JDBCname, JDBCpassword);
String countSql = "select count(*) count from user where gname=?";
pst = conn.prepareStatement(sql);
pst.setString(1, gname);
ResultSet countSet = stmt.executeQuery(countSql);
int count = 0;
if (countSet.next()) {
rowCount = countSet.getInt("count");
}
if (count > 0) {
response.sendRedirect("login.jsp?errNo=1");
} else {
String sql = "insert into user (gname,userid,userpassword,email,age,hobby,gender,city, characteristic) values (?,?,?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);// 使用预处理连接 注意顺序!
pstmt.setString(1, gname);// 表示设置sql语句的参数 对应占位符 1代表的一个问号
pstmt.setString(2, userid);
pstmt.setString(3, userpassword);
pstmt.setString(4, email);
pstmt.setInt(5, age);
pstmt.setString(6, hobby);
pstmt.setString(7, gender);
pstmt.setString(8, city);
pstmt.setString(9, characteristic);
int i = pstmt.executeUpdate();// 预处理提交
if (i > 0) {
out.print("注册成功!3秒后回到主页面");
response.setHeader("refresh", "3;url=face.jsp");
}// 设置头信息跳转
}
} catch (Exception e) {
out.println("添加失败,错误:" + e.getMessage());// 方法一,输出错误的信息
e.printStackTrace();// 方法二,输出 错误的信息
out.println("添加失败!3秒后回到show页面");
response.setHeader("refresh", "3;url=face.jsp");
} finally {
if (pst != null) {
pst.close();// 关闭资源
}
if (pstmt != null) {
pstmt.close();// 关闭资源
}
if (conn != null)
conn.close();// 关闭资源 注意需要按顺序关闭 顺序与你创建对象时的顺序相反 最后创建对象的最先关闭
}
}