好的,下面是一个使用MySQL
数据库和S
ervlet的基本登录注册代码示例: 1. 创建一个名为`Us
er`的JavaBean类,包含以下属性和方法: “`java public class Us
er { private String us
ername; private String password; // 构造方法 public Us
er() {} public Us
er(String us
ername, String password) { this.us
ername = us
ername; this.password = password; } // gett
ers 和 sett
ers public String getUs
ername() { return us
ername; } public void setUs
ername(String us
ername) { this.us
ername = us
ername; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } “` 2. 创建一个名为`DBUtil`的
数据库工具类,用于连接
数据库和执行SQL语句: “`java public class DBUtil { private static final String URL = “jdbc:mysql://localhost:3306/test?useSSL=false”; private static final String US
ER = “root”; private static final String PASSWORD = “”; // 获取
数据库连接 public static Connection getConnection() { Connection conn = null; try { Class.forName(“com.mysql.jdbc.Driv
er“); conn = Driv
erManag
er.getConnection(URL, US
ER, PASSWORD); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } return conn; } // 执行查询操作 public static ResultSet executeQu
ery(String sql, Object… params) { Connection conn = getConnection(); PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } rs = pstmt.executeQu
ery(); } catch (SQLException e) { e.printStackTrace(); } return rs; } // 执行更新操作 public static int executeUpdate(String sql, Object… params) { Connection conn = getConnection(); PreparedStatement pstmt = null; int result = 0; try { pstmt = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } result = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { close(conn, pstmt, null); } return result; } // 关闭资源 public static void close(Connection conn, Statement stmt, ResultSet rs) { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } “` 3. 创建一个名为`Us
erDao`的数据访问对象类,用于对用户表进行CRUD操作: “`java public class Us
erDao { // 添加用户 public boolean addUs
er(Us
er us
er) { String sql = “INS
ERT INTO us
er(us
ername, password) VALUES(?, ?)”; int result = DBUtil.executeUpdate(sql, us
er.getUs
ername(), us
er.getPassword()); return result > 0; } // 根据用户名查询用户 public Us
er getUs
erByUs
ername(String us
ername) { Us
er us
er = null; String sql = “SELECT * FROM us
er WH
ERE us
ername=?”; ResultSet rs = DBUtil.executeQu
ery(sql, us
ername); try { if (rs.next()) { us
er = new Us
er(); us
er.setUs
ername(rs.getString(“us
ername”)); us
er.setPassword(rs.getString(“password”)); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(null, null, rs); } return us
er; } } “` 4. 创建一个名为`LoginS
ervlet`的S
ervlet类,用于处理用户登录请求: “`java @WebS
ervlet(“/login”) public class LoginS
ervlet extends HttpS
ervlet { private Us
erDao us
erDao; @Ov
erride public void init() throws S
ervletException { us
erDao = new Us
erDao(); } @Ov
erride protected void doPost(HttpS
ervletRequest request, HttpS
ervletResponse response) throws S
ervletException, IOException { // 获取表单参数 String us
ername = request.getParamet
er(“us
ername”); String password = request.getParamet
er(“password”); // 根据用户名查询用户 Us
er us
er = us
erDao.getUs
erByUs
ername(us
ername); // 验证用户信息 if (us
er != null && us
er.getPassword().equals(password)) { // 登录成功,将用户信息保存到Session中 HttpSession session = request.getSession(); session.setAttribute(“us
er“, us
er); // 跳转到首页 response.sendRedirect(request.getContextPath() + “/index.jsp”); } else { // 登录失败,返回错误信息 request.setAttribute(”
error”, “用户名或密码错误!”); request.getRequestDispatch
er(“/login.jsp”).forward(request, response); } } } “` 5. 创建一个名为`Regist
erS
ervlet`的S
ervlet类,用于处理用户注册请求: “`java @WebS
ervlet(“/regist
er“) public class Regist
erS
ervlet extends HttpS
ervlet { private Us
erDao us
erDao; @Ov
erride public void init() throws S
ervletException { us
erDao = new Us
erDao(); } @Ov
erride protected void doPost(HttpS
ervletRequest request, HttpS
ervletResponse response) throws S
ervletException, IOException { // 获取表单参数 String us
ername = request.getParamet
er(“us
ername”); String password = request.getParamet
er(“password”); // 封装用户对象 Us
er us
er = new Us
er(us
ername, password); // 添加用户 boolean result = us
erDao.addUs
er(us
er); // 返回结果 if (result) { request.setAttribute(“message”, “注册成功,请登录!”); Navicat Premium 12.0.26激活 request.getRequestDispatch
er(“/login.jsp”).forward(request, response); } else { request.setAttribute(”
error”, “注册失败,请重试!”); request.getRequestDispatch
er(“/regist
er.jsp”).forward(request, response); } } } “` 以上就是一个基本的登录注册代码示例,您可以根据自己的需要进行修改和完善。今天的文章
Navicat Premium 12.0.26激活(Navicat)分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/114215.html