转载声明:文章来源https://blog.csdn.net/yang_study_first/article/details/86383092
一、JavaBean层的设计
数据表参数的获取
主要是生成数据表字段的get,set方法,对数据进行设置和获取。
数据库连接和操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /**数据库连接*/ public class DBConn { public static Connection conn; // Connection对象(链接) // 连接数据库 public static Connection getConn() { try { // 加载注册SQLSever的JDBC驱动 Class.forName( "com.mysql.jdbc.Driver" ); // 编写链接字符串,创建并且获取链接 } catch (Exception e) { e.printStackTrace(); } return conn; } public static void CloseConn() { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { Connection conn=DBConn.getConn(); if (conn!= null ) { System.out.println( "数据库连接正常" ); } else { System.out.println( "数据库连接失败" ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | /**数据库操作*/ public class LoginDao { Connection conn = DBConn.getConn(); // 数据库连接对象 PreparedStatement pstmt; public Login checkLogin(String name, String password) { // 验证用户名密码 try { pstmt = conn.prepareStatement( "select*from logins where name=?and password=?" ); pstmt.setString(1, name); // 设置SQL语句参数 pstmt.setString(2, password); // 设置SQL语句参数 ResultSet rs = pstmt.executeQuery(); // 执行查询,返回结果集 if (rs.next()) { // 通过JavaBean保存值 Login login = new Login(); login.setId(rs.getInt(1)); login.setName(rs.getString(2)); login.setPassword(rs.getString(3)); login.setRole(rs.getInt(4)); return login; // 返回JavaBean对象 } return null ; // 验证失败返回null } catch (Exception e) { e.printStackTrace(); return null ; } } //留言查询 public ArrayList<MessBor> findMbInfo() { try { ArrayList<MessBor> al = new ArrayList<MessBor>(); pstmt = conn.prepareStatement( "select *from messages" ); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { MessBor mb = new MessBor(); mb.setId(rs.getInt(1)); mb.setName(rs.getString(2)); mb.setTime(rs.getDate(3)); mb.setTitle(rs.getString(4)); mb.setMessage(rs.getString(5)); al.add(mb); } return al; } catch (Exception e) { e.printStackTrace(); return null ; } } public String getName(int id) { String name = null ; try { pstmt = conn.prepareStatement( "select namefrom logins where id=?" ); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { name = rs.getString(1); } return name; } catch (Exception e) { e.printStackTrace(); return null ; } } //添加留言 public boolean addInfo(MessBor mb) { try { pstmt = conn.prepareStatement( "insert intomessages values(?,?,?,?,?)" ); pstmt.setInt(1, mb.getId()); pstmt.setString(2, mb.getName()); pstmt.setDate(3, mb.getTime()); pstmt.setString(4, mb.getTitle()); pstmt.setString(5, mb.getMessage()); pstmt.executeUpdate(); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } //用户注册 public boolean insertUser(int id, String name, String password) { try { pstmt = conn.prepareStatement( "insert into logins(id,name,password,role) values(?,?,?,?)" ); pstmt.setInt(1, id); pstmt.setString(2, name); pstmt.setString(3, password); pstmt.setInt(4, 0); pstmt.executeUpdate(); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } } |
二、jsp页面设计
jsp页面主要包括登陆和注册页面,留言主页面,留言界面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!-- 登陆界面 --> <body> <div style= "margin-left: 35%; margin-top: 100px; font-family: Microsoft YaHei" > <h1>登录界面</h1> <form action= "LoginServlet" method= "post" > <table> <tr> <td>账号:</td> <td><input name= "name" type= "text" ></td> </tr> <tr> <td>密码:</td> <td ><input name= "password" type= "password" ></td> </tr> </table> <input type= "submit" value= "登录" style= "font-size: 16px" > <a href= "register.jsp" >注册</a> </form> </div> </body> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!-- 注册界面 --> <body> <div> <h1>注册界面</h1> <form action= "RegisterServlet" method= "post" > <table> <tr> <td>ID:</td> <td><input name= "id" type= "text" size= "20" ></td> </tr> <tr> <td>登录名:</td> <td><input name= "name" type= "text" size= "20" ></td> </tr> <tr> <td>密码:</td> <td><input name= "password" type= "password" size= "21" ></td> </tr> </table> <input type= "submit" value= "注册" > <input type= "reset" value= "重置" > </form> <br> <a href= "login.jsp" >登录</a> </div> </body> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!-- 留言主界面 --> <body> <div style= "margin-left: 35%; margin-top: 100px; font-family: Microsoft YaHei" > <h1 style= "margin-left: 5%" >这里是留言板主界面</h1> <form action= "leavemessage.jsp" method= "post" > <table border= "1" > <caption>所有留言信息</caption> <tr> <th>留言人姓名</th> <th>留言时间</th> <th>留言标题</th> <th>留言内容</th> </tr> <% ArrayList<MessBor> al = new ArrayList<MessBor>(); al = (ArrayList) session.getAttribute( "al" ); if (al != null ) { Iterator iter = al.iterator(); while (iter.hasNext()) { MessBor mb = (MessBor) iter.next(); %> <tr> <td><%= new LoginDao().getName(mb.getId())%></td> <td><%=mb.getTime().toString()%></td> <td><%=mb.getTitle()%></td> <td><%=mb.getMessage()%></td> </tr> <% } } %> </table> </form> <a style= "margin-left: 22%" href= "leavemessage.jsp" >留言</a> </div> </body> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!-- 留言界面 --> <body> <div style= "text-align: center; margin-top: 140px" > <h1>请留言</h1> <form action= "LeaveMessage" method= "post" > <table style= "margin-left: 37%" border= "1" > <caption>填写留言信息</caption> <tr> <td>留言标题</td> <td><input type= "text" name= "title" /></td> </tr> <tr> <td>留言内容</td> <td><textarea name= "message" rows= "5" cols= "35" ></textarea></td> </tr> </table> <input type= "submit" value= "提交" /> <input type= "reset" value= "重置" /> </form> <a href= "main.jsp" >返回留言板界面</a> </div> </body> |
三、servlet设计
servlet主要实现登录,注册,留言等功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /**登录操作*/ @WebServlet( "/LoginServlet" ) public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super (); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置请求编码 request.setCharacterEncoding( "utf-8" ); // 设置响应编码 response.setContentType( "utf-8" ); LoginDao loginDao = new LoginDao(); HttpSession session = request.getSession(); // 先获得user对象,如果是第一次访问该Servlet,用户对象肯定为空, //但如果是第二次甚至是第三次,就不应该再判断该用户的信息 Login l = (Login) session.getAttribute( "login" ); if (l == null ) l = loginDao.checkLogin(request.getParameter( "name" ), request.getParameter( "password" )); if (l != null ) { // 如果登陆成功 session.setAttribute( "login" , l); // 将获取的对象保存在session中 ArrayList al = loginDao.findMbInfo(); // 获取留言板的内容,返回一个数组 session.setAttribute( "al" , al); // 把数组保存起来 response.sendRedirect( "main.jsp" ); // 验证成功跳转到 main.jsp } else { // 验证失败跳转到 error.jsp response.sendRedirect( "error.jsp" ); } if (session!= null ) { response.sendRedirect( "login.jsp" ); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /**注册操作*/ @WebServlet( "/RegisterServlet" ) public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; public RegisterServlet() { super (); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置请求编码 request.setCharacterEncoding( "utf-8" ); //获取字段 int id = Integer.valueOf(request.getParameter( "id" )); String name = request.getParameter( "name" ); String password = request.getParameter( "password" ); if ( new LoginDao().insertUser(id, name, password)) { //注册成功,跳转到登录界面 response.sendRedirect( "login.jsp" ); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /**留言操作*/ @WebServlet( "/LeaveMessage" ) public class LeaveMessage extends HttpServlet { private static final long serialVersionUID = 1L; public LeaveMessage() { super (); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置请求编码 request.setCharacterEncoding( "utf-8" ); // 设置响应编码 response.setContentType( "utf-8" ); // 获取title内容 String title=request.getParameter( "title" ); // 获取message内容 String message=request.getParameter( "message" ); // 从session中取出当前用户对象 Login leaveMessageBoard=(Login) request.getSession().getAttribute( "login" ); // 建立留言表对应JavaBean对象,把数据封装进去 MessBor mb= new MessBor(); mb.setId(leaveMessageBoard.getId()); // 参数为获取的当前时间 mb.setName(leaveMessageBoard.getName()); mb.setTime( new Date(System.currentTimeMillis())); mb.setTitle(title); mb.setMessage(message); // 调DB类中的方法判断是否插入成功 if ( new LoginDao().addInfo(mb)){ response.sendRedirect( "success.jsp" ) ; } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
四、数据库设计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /*注册表和留言表*/ CREATE TABLE `logins` ( `id` int(11) NOT NULL, `name` varchar(20) NOT NULL, `password` varchar(20) NOT NULL, `role` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ------------------------------------------------------------------- CREATE TABLE `messages` ( `id` int(11) NOT NULL, `name` varchar(20) NOT NULL, `time` varchar(20) NOT NULL, `title` varchar(20) NOT NULL, `message` varchar(60) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
小结:
本项目采用MVC设计模式(所谓MVC:模型(model)-视图(view)-控制器(controller),它是用一种业务逻辑、数据与界面显示分离的方法来组织代码,将众多的业务逻辑聚集到一个部件里面,在需要改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,达到减少编码的时间。是一种软件设计典范。),主要学习数据库与jsp和servlet的综合使用,也是本项目的难点。本项目已上传到GitHub,供大家下载使用。
学习学习学习
可以把所有资料打包,发给我嘛?