JSP连接数据库实现注册登录
1.数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)
2.设计一个注册页面,实现用户注册功能
3.设计一个登陆页面,实现用户名密码登陆
4.两个页面可以互相超链接
//代码
//StuDao.java
public class StuDao extends BaseDao { public boolean login(String name, String pwd) { boolean f = false; Connection conn = getConnection(); String sql = "select * from stu where stuName=? and stuPwd=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); rs = ps.executeQuery(); if (rs.next()) f = true; } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(conn, ps, rs); } return f; } public void reg(String stuName, String stuPwd,int stuAge) { Connection conn = getConnection(); PreparedStatement ps = null; try { String sql = "insert into stu(stuName,stuPwd,stuAge) values(?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, stuName); ps.setString(2, stuPwd); ps.setInt(3, stuAge); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { closeAll(conn, ps, null); } } }
//BaseDao.java
public class BaseDao { protected Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "123456"); } catch (Exception e) { e.printStackTrace(); } return conn; } protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs) { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
//userRegister.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注册</title> </head> <body> <script type="text/javascript"> function check() { if (login.account.value==""){ alert("请输入账号!"); return; }else if(login.psw.value=="") { alert("请输入密码!"); return; }else if(login.psw1.value=="") { alert("请再次输入密码!"); return; }else if(login.stuAge.value=="") { alert("请输入年龄!"); return; } var pwd = document.getElementById("pwd").value; var pwd1 = document.getElementById("pwd1").value; if (pwd==pwd1){ alert("注册成功!"); login.submit(); }else { alert("两次输入密码不一样!"); return; } } </script> <form action="checkReg.jsp" method="post" name="login"> 请输入账号:<input type="text" name="account"><br> 请输入密码:<input type="password" name="psw" id="pwd"><br> 请再次输入密码:<input type="password" name="psw1" id="pwd1"><br> 请输入年龄:<input type="text" name="stuAge"><br> <input type="button" value="注册" onclick="check()"> </form> <form action="login.jsp" method="post"> <input type="submit" value="已有账号?前往登录"> </form> </body> </html>
//checkReg.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String account=request.getParameter("account"); String psw=request.getParameter("psw"); String psw1=request.getParameter("psw1"); String age=request.getParameter("stuAge"); if (psw.equals(psw1)) { int stuAge=Integer.parseInt(age); StuDao stuDao = new StuDao(); stuDao.reg(account,psw,stuAge); request.getRequestDispatcher("login.jsp").forward(request,response); } else { request.getRequestDispatcher("userRegister.jsp").forward(request,response); } %> </body> </html>
//login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录</title> </head> <body> <script type="text/javascript"> function check() { if (login.account.value==""){ alert("请输入账号!"); return; }else if(login.psw.value=="") { alert("请输入密码!"); return; } login.submit(); } </script> <form action="check.jsp" method="post" name="login"> 账号:<input type="text" name="account"><br> 密码:<input type="password" name="psw"><br> <input type="checkbox" value="注册会员">注册会员 <input type="checkbox" value="不注册会员">不注册会员<br> <input type="button" value="登录" onclick="check()"> </form><br> <form action="userRegister.jsp" method="post"> <input type="submit" value="立即注册账户"> </form> </body> </html>
//check.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>check</title> </head> <body> <% String account=request.getParameter("account"); String psw=request.getParameter("psw"); StuDao stuDao = new StuDao(); if (stuDao.login(account,psw)){ session.setAttribute("account",account); request.getRequestDispatcher("logSuccess.jsp").forward(request,response); }else { request.getRequestDispatcher("logFail.jsp").forward(request,response); } %> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器