第十周作业
package zhuce2; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DbUtil { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getCon() { Connection con = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } }
package zhuce2; public class User { private Integer id; private String uname; private String password; private Integer age; public User() { } public User(String uname, String password, Integer age) { super(); this.uname = uname; this.password = password; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
package zhuce2; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UserDao { public void regUser(User user) { // String sql = "insert into stu(uname,password,age) value( " + user.getUname() + "," + user.getPassword() + "," // + user.getAge() + ")"; String sql = "insert into stu(uname,password,age) value(?,?,?)"; // 建立数据库链接 Connection con = DbUtil.getCon(); try { // 预编译 PreparedStatement pred = con.prepareStatement(sql); // 替换占位符 pred.setString(1, user.getUname()); pred.setString(2, user.getPassword()); pred.setInt(3, user.getAge()); // 执行sql语句 pred.executeUpdate(); pred.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public User getUserByName(String uname) { User user = null; String sql = "select * from stu where uname = ?"; Connection con = DbUtil.getCon(); try { // 预编译 PreparedStatement pred = con.prepareStatement(sql); // 替换占位符 pred.setString(1, uname); // 执行sql语句 ResultSet res = pred.executeQuery(); if (res.next()) { user = new User(res.getString("uname"), res.getString("password"), res.getInt("age")); } pred.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return user; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="dologin.jsp" name="formName"> 用户名:<input type="text" name="uname"> <br> 密码:<input type="password" name="password"><br> <input type="button" value="登录" onclick="toDoLogin()"> <input type="button" value="注册" onclick="toReg()"> </form> <script> function toReg() { window.location.href = 'reg.jsp'; } function toDoLogin() { if (formName.uname.value == '' || formName.password.value == '') { alert('账号密码不能为空'); } else { formName.submit(); } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="doReg.jsp" method="post"> 用户名:<input type="text" name="uname"><br> 密码:<input type="password" name="password"><br> 确认密码:<input type="password"><br> 年龄:<input type="text" name="age"><br> <input type="submit" value="注册"> </form> </body> </html>
<%@page import="zhuce2.UserDao"%> <%@page import="zhuce2.User"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); String uname = request.getParameter("uname"); String password = request.getParameter("password"); Integer age = Integer.parseInt(request.getParameter("age")); User user = new User(uname, password, age); UserDao userDao = new UserDao(); userDao.regUser(user); response.sendRedirect("index.jsp"); %> </body> </html>
<%@page import="zhuce2.User"%> <%@page import="zhuce2.UserDao"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String uname = request.getParameter("uname"); String password = request.getParameter("password"); UserDao userDao = new UserDao(); User user = userDao.getUserByName(uname); if (password.equals(user.getPassword())) { response.sendRedirect("main.jsp"); } else { %> <script> alert('账号或密码错误'); </script> <% response.sendRedirect("index.jsp"); } %> </body> </html>