JSP第十三周练习

1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现。

 1 package com.wl.email.domain;
 2 
 3 public class User {
 4 
 5     private Long id;
 6     private String username; 
 7     private String password;
 8     
 9     public User(Long id, String username, String password) {
10         super();
11         this.id = id;
12         this.username = username;
13         this.password = password;
14     }
15     public User() {
16         super();
17         // TODO Auto-generated constructor stub
18     }
19     public Long getId() {
20         return id;
21     }
22     public void setId(Long id) {
23         this.id = id;
24     }
25     public String getUsername() {
26         return username;
27     }
28     public void setUsername(String username) {
29         this.username = username;
30     }
31     public String getPassword() {
32         return password;
33     }
34     public void setPassword(String password) {
35         this.password = password;
36     }
37     @Override
38     public String toString() {
39         return "Email [id=" + id + ", username=" + username + ", password="
40                 + password + "]";
41     } 
42     
43 }
domain.User

 

 1 package com.wl.email.domain;
 2 
 3 public class Email {
 4 
 5     private Long id;
 6     private String address;
 7     private String sender;
 8     private String tittle;
 9     private String content;
10     private String sendDate ;
11     private int state;
12     
13     public Email(Long id, String address, String sender, String tittle,
14             String content, String sendDate, int state) {
15         super();
16         this.id = id;
17         this.address = address;
18         this.sender = sender;
19         this.tittle = tittle;
20         this.content = content;
21         this.sendDate = sendDate;
22         this.state = state;
23     }
24     public Email() {
25         super();
26         // TODO Auto-generated constructor stub
27     }
28     public Long getId() {
29         return id;
30     }
31     public void setId(Long id) {
32         this.id = id;
33     }
34     public String getAddress() {
35         return address;
36     }
37     public void setAddress(String address) {
38         this.address = address;
39     }
40     public String getSender() {
41         return sender;
42     }
43     public void setSender(String sender) {
44         this.sender = sender;
45     }
46     public String getTittle() {
47         return tittle;
48     }
49     public void setTittle(String tittle) {
50         this.tittle = tittle;
51     }
52     public String getContent() {
53         return content;
54     }
55     public void setContent(String content) {
56         this.content = content;
57     }
58     public String getSendDate() {
59         return sendDate;
60     }
61     public void setSendDate(String sendDate) {
62         this.sendDate = sendDate;
63     }
64     public int getState() {
65         return state;
66     }
67     public void setState(int state) {
68         this.state = state;
69     }
70     @Override
71     public String toString() {
72         return "Email [id=" + id + ", address=" + address + ", sender="
73                 + sender + ", tittle=" + tittle + ", content=" + content
74                 + ", sendDate=" + sendDate + ", state=" + state + "]";
75     }
76     
77 
78 }
domain.Email

 

 1 package com.wl.email.utils;
 2 
 3 import java.sql.DriverManager;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 import com.mysql.jdbc.Connection;
 9 
10 public class JDBCUtils {
11     private static Connection conn;
12     static {
13         try {
14             Class.forName("com.mysql.jdbc.Driver");
15             String url = "jdbc:mysql:///wl_tale?SSL=false";
16             String username = "root";
17             String password = "root";
18             conn = (Connection) DriverManager.getConnection(url, username,
19                     password);
20         } catch (Exception e) {
21             e.printStackTrace();
22         }
23 
24     }
25 
26     public static Connection getConn() {
27         return conn;
28     }
29 
30     public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) {
31         try {
32             if (conn != null)
33                 System.out.println();
34 //                conn.close();
35                 
36             if (ps != null)
37                 ps.close();
38             if (rs != null)
39                 rs.close();
40 
41         } catch (SQLException e) {
42             e.printStackTrace();
43         }
44     }
45 
46 }
utils.JDBCUtil

 

 1 package com.wl.email.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.wl.email.domain.Email;
 6 
 7 public interface IEmailDAO {
 8     void add(Email email);
 9     void delById(Long id);
10     void update(Long id);
11     Email selOneById(Long id);
12     List<Email> selAll();
13 
14 }
dao.IEmailDAO

 

 1 package com.wl.email.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.wl.email.domain.User;
 6 
 7 public interface IUserDAO {
 8     void add(User user);
 9     void delById(Long id);
10     void update(User user);
11     User selOneByUser(User user);
12     List<User> selAll();
13 
14 }
dao.IUserDAO

 

  1 package com.wl.email.dao.impl;
  2 
  3 import java.sql.ResultSet;
  4 import java.sql.SQLException;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 
  8 import com.mysql.jdbc.Connection;
  9 import com.mysql.jdbc.PreparedStatement;
 10 import com.wl.email.dao.IEmailDAO;
 11 import com.wl.email.dao.IUserDAO;
 12 import com.wl.email.domain.Email;
 13 import com.wl.email.domain.User;
 14 import com.wl.email.utils.JDBCUtils;
 15 
 16 public class IEmailDAOImpl implements IEmailDAO{
 17 
 18     @Override
 19     public void add(Email email) {
 20         Connection conn = null;
 21         PreparedStatement ps = null;
 22         try {
 23             conn = JDBCUtils.getConn();
 24             String sql = "INSERT INTO email(address,sender,tittle,content,sendDate,state) " 
 25                     + "VALUES(?,?,?,?,?,0)";
 26             ps = (PreparedStatement) conn.prepareStatement(sql);
 27             ps.setString(1, email.getAddress());
 28             ps.setString(2, email.getSender());
 29             ps.setString(3, email.getTittle());
 30             ps.setString(4, email.getContent());
 31             ps.setString(5, email.getSendDate());
 32             int row = ps.executeUpdate();
 33                 System.out.println((row > 0)?"添加成功":"添加失败");
 34         } catch (SQLException e) {
 35             e.printStackTrace();
 36         }finally{
 37             JDBCUtils.closeAll(conn, ps, null);
 38         }
 39         
 40     }
 41 
 42     @Override
 43     public void delById(Long id) {
 44         Connection conn = null;
 45         PreparedStatement ps = null;
 46         try {
 47             conn = JDBCUtils.getConn();
 48             String sql = "DELETE FROM email WHERE id=?";
 49             ps = (PreparedStatement) conn.prepareStatement(sql);
 50             ps.setLong(1, id);
 51             int row = ps.executeUpdate();
 52             System.out.println((row > 0)?(id+"删除成功"):(id+"删除失败"));
 53         } catch (SQLException e) {
 54             e.printStackTrace();
 55         }finally{
 56             JDBCUtils.closeAll(conn, ps, null);
 57         }        
 58     }
 59 
 60     @Override
 61     public void update(Long id) {
 62         Connection conn = null;
 63         PreparedStatement ps = null;
 64         try {
 65             conn = JDBCUtils.getConn();
 66             String sql = "UPDATE email SET state = 1 WHERE id=?";
 67             ps = (PreparedStatement) conn.prepareStatement(sql);
 68             ps.setLong(1, id);
 69             int row = ps.executeUpdate();
 70             System.out.println((row > 0)?(id+"已读成功"):(id+"已读失败"));
 71         } catch (SQLException e) {
 72             e.printStackTrace();
 73         }finally{
 74             JDBCUtils.closeAll(conn, ps, null);
 75         }
 76         
 77     }
 78 
 79     @Override
 80     public Email selOneById(Long id) {
 81         Connection conn = null;
 82         PreparedStatement ps = null;
 83         ResultSet rs = null;
 84         Email email = new Email();
 85         try {
 86             conn = JDBCUtils.getConn();
 87             String sql = "SELECT id,address,sender,tittle,content,sendDate,state " 
 88                         +"FROM email WHERE id=?";
 89             ps = (PreparedStatement) conn.prepareStatement(sql);
 90             ps.setLong(1, id);
 91             rs = ps.executeQuery();
 92             while (rs.next()) {
 93                 // 8.获取ResuleSet对象的元素
 94                 //long id = rs.getLong("id");
 95                 String address = rs.getString("address");
 96                 String sender = rs.getString("sender");
 97                 String tittle = rs.getString("tittle");
 98                 String content = rs.getString("content");
 99                 String sendDate = rs.getString("sendDate");
100                 int state = rs.getInt("state");
101                 email = new Email(id,address,sender,tittle,content,sendDate,state);
102                 //listEmails.add(new Email(id,address,sender,tittle,content,sendDate,state));
103             }
104             
105         } catch (SQLException e) {
106             e.printStackTrace();
107         }finally{
108             JDBCUtils.closeAll(conn, ps, rs);
109         }
110 
111         return email;
112     }
113 
114     @Override
115     public List<Email> selAll() {
116         Connection conn = null;
117         PreparedStatement ps = null;
118         ResultSet rs = null;
119         List<Email> listEmails = new ArrayList<Email>();
120         try {
121             conn = JDBCUtils.getConn();
122             String sql = "SELECT id,address,sender,tittle,content,sendDate,state FROM email";
123             ps = (PreparedStatement) conn.prepareStatement(sql);
124             rs = ps.executeQuery();
125             while (rs.next()) {
126                 // 8.获取ResuleSet对象的元素
127                 long id = rs.getLong("id");
128                 String address = rs.getString("address");
129                 String sender = rs.getString("sender");
130                 String tittle = rs.getString("tittle");
131                 String content = rs.getString("content");
132                 String sendDate = rs.getString("sendDate");
133                 int state = rs.getInt("state");
134                 listEmails.add(new Email(id,address,sender,tittle,content,sendDate,state));
135             }
136             
137         } catch (SQLException e) {
138             e.printStackTrace();
139         }finally{
140             JDBCUtils.closeAll(conn, ps, rs);
141         }
142 
143         return listEmails;
144     }
145 
146 
147 }
dao.impl.IEmailDAOImpl

 

 1 package com.wl.email.dao.impl;
 2 
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.util.List;
 6 
 7 import com.mysql.jdbc.Connection;
 8 import com.mysql.jdbc.PreparedStatement;
 9 import com.wl.email.dao.IUserDAO;
10 import com.wl.email.domain.User;
11 import com.wl.email.utils.JDBCUtils;
12 
13 public class IUserDAOImpl implements IUserDAO {
14 
15     @Override
16     public void add(User user) {
17         Connection conn = null;
18         PreparedStatement ps = null;
19         try {
20             conn = JDBCUtils.getConn();
21             String sql = "INSERT INTO user(username,password) VALUES(?,?); ";
22             ps = (PreparedStatement) conn.prepareStatement(sql);
23             ps.setString(1, user.getUsername());
24             ps.setString(2, user.getPassword());
25             int row = ps.executeUpdate();
26             System.out.println((row > 0)?"添加成功":"添加失败");
27         } catch (SQLException e) {
28             
29             e.printStackTrace();
30         }finally{
31             JDBCUtils.closeAll(conn, ps, null);
32         }
33 
34     }
35 
36     @Override
37     public void delById(Long id) {
38         // TODO Auto-generated method stub
39 
40     }
41 
42     @Override
43     public void update(User user) {
44         // TODO Auto-generated method stub
45 
46     }
47 
48     @Override
49     public User selOneByUser(User user) {
50         Connection conn = null;
51         PreparedStatement ps = null;
52         ResultSet rs = null;
53         User u = new User();
54         try {
55             conn = JDBCUtils.getConn();
56             String sql = "select * from user where username=? and password=?";
57             ps = (PreparedStatement) conn.prepareStatement(sql);
58             ps.setString(1, user.getUsername());
59             ps.setString(2, user.getPassword());
60             rs = ps.executeQuery();
61             if(rs.next()){
62                 long id = rs.getLong("id");
63                 String userName = rs.getString("username");
64                 String pwd = rs.getString("password");
65                 u = new User(id,userName,pwd);
66             }
67         } catch (SQLException e) {
68             
69             e.printStackTrace();
70         }finally{
71             JDBCUtils.closeAll(conn, ps, rs);
72         }
73         return u;
74     }
75 
76     @Override
77     public List<User> selAll() {
78         // TODO Auto-generated method stub
79         return null;
80     }
81 
82 }
dao.impl.IUserDAOImpl

 

1 package com.wl.email.service;
2 
3 import com.wl.email.domain.User;
4 
5 public interface IUserService {
6     User login(String username, String password);
7     void register(String username, String password);
8 }
service.IUserService

 

 1 package com.wl.email.service;
 2 
 3 import java.util.List;
 4 
 5 import com.wl.email.domain.Email;
 6 
 7 public interface IEmailService {
 8     void add(Email email);
 9     void delById(Long id);
10     void update(Long id);
11     Email selOneById(Long id);
12     List<Email> getEmailList();
13 
14 }
service.IEmailService

 

 1 package com.wl.email.service.impl;
 2 
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.util.List;
 6 
 7 import com.mysql.jdbc.Connection;
 8 import com.mysql.jdbc.PreparedStatement;
 9 import com.wl.email.dao.IUserDAO;
10 import com.wl.email.dao.impl.IEmailDAOImpl;
11 import com.wl.email.dao.impl.IUserDAOImpl;
12 import com.wl.email.domain.Email;
13 import com.wl.email.domain.User;
14 import com.wl.email.utils.JDBCUtils;
15 
16 public class IEmailServiceImpl {
17     private IEmailDAOImpl ieDAO = new IEmailDAOImpl();
18     private IUserDAO iuDAO = new IUserDAOImpl();
19     // 添加邮件
20     public void add(Email email) {
21         System.out.println("ServiceADD");
22         //ieDAO.add(email);
23     }
24     // 点击详情,自动修改状态
25     public void update(Long id) {
26         ieDAO.update(id);
27     }
28     // 删除
29     public void delById(Long id) {
30         ieDAO.delById(id);
31     }
32     // 查询一个
33     public Email selOneById(Long id) {
34         Email email = ieDAO.selOneById(id);
35         return email;
36     }
37     
38     // 查询全部
39     public List<Email> getEmailList() {
40         List<Email> list = ieDAO.selAll();
41         return list;
42         
43     }
44     
45 }
service.impl.IEmailServiceImpl

 

 1 package com.wl.email.service.impl;
 2 
 3 import com.wl.email.dao.IUserDAO;
 4 import com.wl.email.dao.impl.IUserDAOImpl;
 5 import com.wl.email.domain.User;
 6 
 7 public class IUserServiceImpl {
 8 
 9     private IUserDAO iuDAO = new IUserDAOImpl();
10 
11     // 登录查询
12     public User login(String username, String password) {
13         User user = iuDAO.selOneByUser(new User(new Long(100),username,password));
14         return user;
15     }
16     // 注册
17     public void register(String username, String password) {
18         iuDAO.add(new User(new Long(100),username,password));
19         
20     }
21 }
service.impl.IEmailUserImpl

 

posted @ 2022-05-29 20:17  L'童话故事  阅读(23)  评论(0编辑  收藏  举报