码迷,mamicode.com
首页 > 其他好文 > 详细

完成整个DAO的实现及测试代码

时间:2014-08-22 23:42:09      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   for   ar   

bubuko.com,布布扣
 1 package cn.itcast.domain;
 2 
 3 import java.util.Date;
 4 
 5 public class User {
 6     
 7     private int id;
 8     private String name;
 9     private Date birthday;
10     private float money;
11 
12     public int getId() {
13         return id;
14     }
15 
16     public void setId(int id) {
17         this.id = id;
18     }
19 
20     public String getName() {
21         return name;
22     }
23 
24     public void setName(String name) {
25         this.name = name;
26     }
27 
28     public Date getBirthday() {
29         return birthday;
30     }
31 
32     public void setBirthday(Date birthday) {
33         this.birthday = birthday;
34     }
35 
36     public float getMoney() {
37         return money;
38     }
39 
40     public void setMoney(float money) {
41         this.money = money;
42     }
43 
44 }
User
bubuko.com,布布扣
 1 package cn.itcast.dao;
 2 
 3 import java.sql.SQLException;
 4 
 5 import cn.itcast.domain.User;
 6 
 7 public interface UserDao {
 8     
 9     public void addUser(User user) throws SQLException;
10     
11     public User getUser(int userId);
12     
13     public void update(User user);
14     
15     public void delete(User user);
16     
17     public User findUser(String userName,String password);
18     
19     
20 
21 }
UserDao
bubuko.com,布布扣
  1 package cn.itcast.dao.impl;
  2 
  3 import java.sql.Connection;
  4 import java.sql.Date;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.sql.Statement;
  9 
 10 import cn.itcast.dao.UserDao;
 11 import cn.itcast.domain.User;
 12 import cn.itcast.exception.DaoException;
 13 import cn.itcast.jdbc.jdbcUtils;
 14 
 15 public class UserDaoJdbcImpl implements UserDao {
 16 
 17     @Override
 18     public void addUser(User user) throws DaoException {
 19 
 20         Connection conn = null;
 21         PreparedStatement ps = null;
 22         ResultSet rs = null;
 23 
 24         try {
 25             // 建立连接
 26             conn = jdbcUtils.getConnection();
 27 
 28             String sql = "insert into user(name,birthday,money) values(?,?,?)";
 29             ps = conn.prepareStatement(sql);
 30 
 31             ps.setString(1, user.getName());
 32             ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
 33             ps.setFloat(3, user.getMoney());
 34 
 35             ps.executeUpdate();
 36 
 37         } catch (SQLException e) {
 38             throw new DaoException(e.getMessage(), e);
 39         } finally {
 40             jdbcUtils.free(rs, ps, conn);
 41         }
 42 
 43     }
 44 
 45     @Override
 46     public User getUser(int userId) {
 47         Connection conn = null;
 48         PreparedStatement ps = null;
 49         ResultSet rs = null;
 50         User user = null;
 51 
 52         try {
 53             conn = jdbcUtils.getConnection();
 54             String sql = "select id,name,money,birthday from user where id = ?";
 55             ps = conn.prepareStatement(sql);
 56             ps.setInt(1, userId);
 57             rs = ps.executeQuery();
 58 
 59             while (rs.next()) {
 60                 
 61                 user = mappingUser(rs);
 62 
 63             }
 64 
 65         } catch (SQLException e) {
 66             throw new DaoException(e.getMessage());
 67         } finally {
 68             jdbcUtils.free(rs, ps, conn);
 69         }
 70 
 71         return user;
 72     }
 73 
 74     public User mappingUser(ResultSet rs) throws SQLException {
 75         User user = new User();
 76         user.setId(rs.getInt("id"));
 77         user.setName(rs.getString("name"));
 78         user.setMoney(rs.getFloat("money"));
 79         user.setBirthday(rs.getDate("birthday"));
 80         return user;
 81     }
 82 
 83     @Override
 84     public void update(User user) {
 85         Connection conn = null;
 86         PreparedStatement ps = null;
 87         ResultSet rs = null;
 88 
 89         try {
 90             conn = jdbcUtils.getConnection();
 91             String sql = "update user set name = ?,money=? where id=?";
 92             ps = conn.prepareStatement(sql);
 93             ps.setString(1, user.getName());
 94             //ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
 95             ps.setFloat(2, user.getMoney());
 96             ps.setInt(3, user.getId());
 97 
 98             ps.executeUpdate();
 99 
100         } catch (SQLException e) {
101             new DaoException(e.getMessage());
102         } finally {
103             jdbcUtils.free(rs, ps, conn);
104         }
105 
106     }
107 
108     @Override
109     public void delete(User user) {
110 
111         Connection conn = null;
112         Statement st = null;
113         ResultSet rs = null;
114 
115         try {
116             conn = jdbcUtils.getConnection();
117             st = conn.createStatement();
118             String sql = "delete from user where id=" + user.getId();
119             st.executeUpdate(sql);
120 
121         } catch (SQLException e) {
122             throw new DaoException(e.getMessage());
123         } finally {
124             jdbcUtils.free(rs, st, conn);
125         }
126 
127     }
128 
129     @Override
130     public User findUser(String userName, String password) {
131 
132         Connection conn = null;
133         PreparedStatement ps = null;
134         ResultSet rs = null;
135         User user = null;
136 
137         try {
138             conn = jdbcUtils.getConnection();
139             String sql = "select id,name,money,birthday from user where name = ?";
140             ps = conn.prepareStatement(sql);
141             ps.setString(1, userName);
142             rs = ps.executeQuery();
143 
144             while (rs.next()) {
145                 user = mappingUser(rs);
146             }
147 
148         } catch (SQLException e) {
149             throw new DaoException(e.getMessage());
150         } finally {
151             jdbcUtils.free(rs, ps, conn);
152         }
153 
154         return user;
155     }
156 
157 }
UserDaoJdbcImpl
bubuko.com,布布扣
 1 package cn.itcast.exception;
 2 
 3 public class DaoException extends RuntimeException {
 4     
 5     private static final long serialVersionUID = 1L;
 6 
 7     public DaoException() {
 8         super();
 9         // TODO Auto-generated constructor stub
10     }
11 
12     public DaoException(String message, Throwable cause) {
13         super(message, cause);
14         // TODO Auto-generated constructor stub
15     }
16 
17     public DaoException(String message) {
18         super(message);
19         // TODO Auto-generated constructor stub
20     }
21 
22     public DaoException(Throwable cause) {
23         super(cause);
24         // TODO Auto-generated constructor stub
25     }
26     
27 }
DaoException
bubuko.com,布布扣
 1 package cn.itcast.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 public class jdbcUtils {
10 
11     private static String url = "jdbc:mysql://localhost:3306/jdbc";
12     private static String user = "root";
13     private static String password = "123";
14 
15     private jdbcUtils() {
16 
17     }
18 
19     static {
20         try {
21             Class.forName("com.mysql.jdbc.Driver");
22         } catch (ClassNotFoundException e) {
23             e.printStackTrace();
24         }
25     }
26 
27     public static Connection getConnection() throws SQLException {
28         return DriverManager.getConnection(url, user, password);
29     }
30 
31     public static void free(ResultSet rs, Statement st, Connection conn) {
32 
33         try {
34             if (rs != null)
35                 rs.close();
36         } catch (SQLException e) {
37             e.printStackTrace();
38         } finally {
39 
40             try {
41                 if (st != null)
42                     st.close();
43             } catch (SQLException e) {
44                 e.printStackTrace();
45             } finally {
46 
47                 try {
48                     if (conn != null)
49                         conn.close();
50                 } catch (SQLException e) {
51                     e.printStackTrace();
52                 }
53             }
54 
55         }
56 
57     }
58 }
jdbcUtils
bubuko.com,布布扣
 1 package cn.itcast.dao;
 2 
 3 import java.sql.SQLException;
 4 import java.util.Date;
 5 
 6 import cn.itcast.dao.impl.UserDaoJdbcImpl;
 7 import cn.itcast.domain.User;
 8 
 9 public class UserTest {
10 
11     /**
12      * @param args
13      * @throws SQLException 
14      */
15     public static void main(String[] args) throws SQLException {
16         
17         UserDao userDao = new UserDaoJdbcImpl();
18         
19         //User user = new User();
20         //user.setBirthday(new Date());
21         //user.setName("dao name1");
22         //user.setMoney(1000.0f);
23         
24 //        userDao.addUser(user);
25 //        
26 //        User u = userDao.findUser(user.getName(),null);
27 //        System.out.println(u.getId());
28         
29         User u = userDao.getUser(1);
30 
31         u.setMoney(2000.0f);
32         userDao.update(u);
33         
34         User u1 = userDao.getUser(1);
35         userDao.delete(u1);
36 
37     }
38 
39 }
UserTest

 

完成整个DAO的实现及测试代码

标签:style   blog   http   color   java   os   io   for   ar   

原文地址:http://www.cnblogs.com/aineko/p/3930349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!