第十一周作业
1.建库,建表2个
用户表(id,用户名,密码)
邮件表(id,发件人,收件人,标题,内容,发送时间,状态)
2.建model层
entity,dao包
3.登陆,注册,登陆后显示全部邮件
package utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class utils { //注册驱动 static { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //获取数据库连接 //获取Connection对象 private static Connection conn; public static Connection getConnection() throws SQLException { String url = "jdbc:mysql://127.0.0.1/dbtext"; String user = "root"; String password = "123456"; if(conn == null || conn.isClosed()) { conn = DriverManager.getConnection(url, user, password); } return conn; } //关闭资源 public static void Close(Connection conn,Statement stmt,ResultSet rs) throws SQLException { if(conn!=null && !conn.isClosed()) { conn.close(); } if(stmt!=null && !stmt.isClosed()) { stmt.close(); } if(rs!=null && !rs.isClosed()) { rs.close(); } } }
package entiry; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import Service.StudentService; import Service.StudentServiceImpl; public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); StudentService service = new StudentServiceImpl(); HttpSession session = request.getSession(); PrintWriter out = response.getWriter(); StringBuilder json = new StringBuilder(); Student student = service.selectStu(username,password); if(student==null) { json.append("{\"success\":false}");//json={"success":false} out.print(json); }else {json.append("{\"success\":true}");//json={"success":false} out.print(json); session.setAttribute("student", student); System.out.println(student); response.sendRedirect(request.getContextPath()+"/Main.jsp"); } } }
package entiry; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import Service.StudentService; import Service.StudentServiceImpl; public class Some extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String idStr = request.getParameter("id"); Integer id = Integer.valueOf(idStr); HttpSession session = request.getSession(); StudentService service = new StudentServiceImpl(); Student student = service.selectStuById(id); if(student!=null) { response.sendRedirect(request.getContextPath()+"/Main.jsp"); session.setAttribute("student", student); System.out.println("学生信息为=="+student); } } }
package entiry; import java.io.Serializable; public class Student implements Serializable { private String username; private String password; private Integer age; private Integer id; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String username, String password, Integer age, Integer id) { super(); this.username = username; this.password = password; this.age = age; this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } 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; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String toString() { return "Student [username=" + username + ", password=" + password + ", age=" + age + ", id=" + id + "]"; } }