4.14作业
数据库表的数据截图
注册界面
注册成功界面
登录界面
登录成功界面
登录失败界面
stu代码
package bean; public class Stu { private int sid; private String uname; private String upwd; // 访问器 public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } // 构造器 public Stu(int sid, String uname, String upwd) { super(); this.sid = sid; this.uname = uname; this.upwd = upwd; } public Stu() { super(); } }
StuDao代码
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import bean.Stu; import java.sql.*; public class StuDao { private static final Connection con = null; private static final PreparedStatement ps = null; ResultSet res = null; // 学生数据访问类 // 添加学生 public int addStu(Stu s) { int i = 0; try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立连接 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mysql", "root", "root"); // 写SQL语句 String sql = "insert into stu values(?,?,?)"; // 执行 PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, s.getSid()); ps.setString(2, s.getUname()); ps.setString(3, s.getUpwd()); i = ps.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { close(con, ps, null); } return i; } public int adcStu(Stu t) { int a = 0; try { // 加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立连接 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mysql", "root", "root"); // 写SQL语句 String sql = "select * from stu where uname=? and upwd=?"; // 执行 PreparedStatement p = con.prepareStatement(sql); p.setString(1, t.getUname()); p.setString(2, t.getUpwd()); // a = p.executeUpdate(); res = p.executeQuery(); if (res.next()) { a = 1; return a; } else { return a; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { close(con, ps, null); } return a; } public void close(Connection conn, PreparedStatement ps, ResultSet rs) { try { if (conn != null) { conn.close(); } if (ps != null) { ps.close(); } if (rs != null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 删除学生 // 登录 } // 查询全部 // 修改学生信息
index.isj代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <body> <form name="form1" method="post" action="control.jsp"> <table> <tr> <td>用户名:</td> <td><input type="text" name="uname" id="userName"> </td> </tr> <tr> <td>输入登录密码:</td> <td><input type="password" name="upwd" id="pwd"> </td> </tr> <tr> <td colspan="2"><input type="submit" value="注册"> <input type="button" value="已有账号点击登录" onclick="window.location = 'tz.jsp';"/> </td> </tr> </table> </form> </body> </html>
control.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="dao.StuDao"%> <%@page import="bean.Stu"%> <html> <body> <% Stu s = new Stu(); String uid = request.getParameter("uid"); s.setUname(uid); String uname = request.getParameter("uname"); s.setUname(uname); String upwd = request.getParameter("upwd"); s.setUpwd(upwd); StuDao sd = new StuDao(); if (sd.addStu(s) > 0) { //跳转注册成功页面 response.sendRedirect("zccg.jsp"); } else { //错误页面 response.sendRedirect("index.jsp"); } %> </body> </html>
dlcg.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<body>
恭喜您登录成功!
<input type="button" value="点击返回注册" onclick="window.location = 'index.jsp';"/>
<input type="button" value="点击返回登录" onclick="window.location = 'tz.jsp';"/>
</body>
</html>
dlsb.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<body>
登陆失败!请重新登录
<input type="button" value="点击返回注册" onclick="window.location = 'index.jsp';"/>
<input type="button" value="点击返回登录" onclick="window.location = 'tz.jsp';"/>
</body>
</html>
go.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="dao.StuDao"%> <%@page import="bean.Stu"%> <html> <body> <% Stu t = new Stu(); String name = request.getParameter("uname"); name.trim(); t.setUname(name); String pwd = request.getParameter("upwd"); pwd.trim(); t.setUpwd(pwd); StuDao sdt = new StuDao(); if (sdt.adcStu(t)> 0) { response.sendRedirect("dlcg.jsp"); } else { response.sendRedirect("dlsb.jsp"); } %> </body> </html>
tz.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <body> <body> <form name="form1" method="post" action="go.jsp"> <table> <tr> <td>请输入用户名:</td> <td><input type="text" name="uname" id="userName"> </td> </tr> <tr> <td>输入登录密码:</td> <td><input type="password" name="upwd" id="pwd"> </td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"> <input type="button" value="点击返回注册" onclick="window.location = 'index.jsp';"/> </td> </tr> </table> </form> </body> </html>
zccg.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<body>
恭喜您注册成功!
<input type="button" value="点击返回注册" onclick="window.location = 'index.jsp';"/>
<input type="button" value="点击返回登录" onclick="window.location = 'tz.jsp';"/>
</body>
</html>