基于DAO模式和MVC模式完成一个用户管理程序,包括用户注册、用户登录、浏览用户列表和删除用户等四个功能。
要求:1)JSP页面中不允许出现Java代码,使用标签实现显示逻辑
2)HTTP请求发送给Servlet,由Servlet负责调用DAO类,并转发给JSP页面
3)浏览用户列表页面要支持分页功能
DBCon.java类:与数据库admin表连接,有插入,删除等动作;
1 package db_admin; 2 3 import java.sql.*; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 public class DBCon { 10 private String dbDriver = "com.mysql.jdbc.Driver"; 11 private String dbUrl = "jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8"; 12 private String dbUser = "root"; 13 private String dbPwd = "123456"; 14 private Connection conn = null; 15 16 public Connection getConnection() { 17 try { 18 Class.forName(dbDriver); 19 conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd); 20 } catch (ClassNotFoundException e) { 21 e.printStackTrace(); 22 } catch (SQLException e) { 23 e.printStackTrace(); 24 } 25 return conn; 26 } 27 28 public ResultSet doQurey(String sql) { 29 ResultSet rs = null; 30 conn = this.getConnection(); 31 try { 32 Statement stmt = conn.createStatement(); 33 rs = stmt.executeQuery(sql); 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 return rs; 38 } 39 40 public int doUpdate(String sql) { 41 int res = 0; 42 conn = this.getConnection(); 43 try { 44 Statement stmt = conn.createStatement(); 45 res = stmt.executeUpdate(sql); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 return res; 50 } 51 52 public String checkUser(String username, String password) { 53 boolean has_username = false; 54 boolean password_correct = false; 55 ResultSet rs = null; 56 try { 57 rs = doQurey("select * from admin"); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 62 try { 63 while (rs.next()) { 64 String temp_username = rs.getString("username"); 65 String temp_password = rs.getString("password"); 66 if (username.equals(temp_username)) { 67 has_username = true; 68 if (password.equals(temp_password)) { 69 password_correct = true; 70 71 return "hasUserNameAndPasswordCorrect"; 72 73 } 74 75 return "hasUserNameButPasswordInCorrect"; 76 77 } 78 79 } 80 81 } catch (SQLException e) { 82 83 e.printStackTrace(); 84 85 } 86 87 return "hasNoUserName"; 88 89 } 90 91 public void close() throws SQLException { 92 try { 93 conn.close(); 94 } catch (Exception e) { 95 e.printStackTrace(); 96 } 97 } 98 }
View Code
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:基于设计模式的用户管理 - Python技术站