标签:
实体类User:
1 package com.xxyh.jdbc.domain; 2 3 import java.util.Date; 4 5 public class User { 6 private int id; 7 private String name; 8 private Date birthday; 9 private float money; 10 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public Date getBirthday() { 24 return birthday; 25 } 26 public void setBirthday(Date birthday) { 27 this.birthday = birthday; 28 } 29 public float getMoney() { 30 return money; 31 } 32 public void setMoney(float money) { 33 this.money = money; 34 } 35 36 @Override 37 public String toString() { 38 return "[" + getId() + "," + getName() + "," + getBirthday() + "," + getMoney() + "]"; 39 } 40 }
UserDao接口:
1 package com.xxyh.jdbc.dao; 2 import com.xxyh.jdbc.domain.User; 3 public interface UserDao { 4 5 /* 添加一个user */ 6 public void addUser(User user); 7 8 /* 根据id查找一个user */ 9 public User getUser(int userId); 10 11 /* 根据name查找一个user */ 12 public User findUser(String name); 13 14 /* 更新一个user信息 */ 15 public void update(User user); 16 17 /* 删除一个user */ 18 public void delete(User user); 19 20 }
添加一个用于处理异常的异常类:
1 package com.xxyh.jdbc.dao; 2 public class DaoException extends RuntimeException { 3 private static final long serialVersionUID = 1L; 4 public DaoException() { 5 // TODO Auto-generated constructor stub 6 } 7 public DaoException(String message) { 8 super(message); 9 // TODO Auto-generated constructor stub 10 } 11 public DaoException(Throwable cause) { 12 super(cause); 13 // TODO Auto-generated constructor stub 14 } 15 public DaoException(String message, Throwable cause) { 16 super(message, cause); 17 // TODO Auto-generated constructor stub 18 } 19 public DaoException(String message, Throwable cause, 20 boolean enableSuppression, boolean writableStackTrace) { 21 super(message, cause, enableSuppression, writableStackTrace); 22 // TODO Auto-generated constructor stub 23 } 24 }
UserDao的实现:
1 package com.xxyh.jdbc.dao.impl; 2 import java.sql.Connection; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import com.xxyh.jdbc.JdbcUtils; 7 import com.xxyh.jdbc.JdbcUtilsSingleton; 8 import com.xxyh.jdbc.dao.DaoException; 9 import com.xxyh.jdbc.dao.UserDao; 10 import com.xxyh.jdbc.domain.User; 11 public class UserDaoJdbcImpl implements UserDao { 12 @Override 13 public void addUser(User user) { 14 Connection conn = null; 15 PreparedStatement ps = null; 16 ResultSet rs = null; 17 try { 18 conn = JdbcUtils.getConnection(); 19 String sql = "insert into user(name, birthday, money) values(?,?,?)"; 20 ps = conn.prepareStatement(sql); 21 ps.setString(1, user.getName()); 22 ps.setDate(2, new java.sql.Date(user.getBirthday().getTime())); 23 ps.setFloat(3, user.getMoney()); 24 ps.executeUpdate(); 25 } catch (SQLException e) { 26 throw new DaoException(e.getMessage(), e); 27 } finally { 28 JdbcUtils.close(rs, ps, conn); 29 } 30 } 31 @Override 32 public User getUser(int userId) { 33 Connection conn = null; 34 PreparedStatement ps = null; 35 ResultSet rs = null; 36 User user = null; 37 try { 38 conn = JdbcUtilsSingleton.getInstance().getConnection(); 39 String sql = "select id,name,birthday,money from user where id=?"; 40 ps = conn.prepareStatement(sql); 41 ps.setInt(1, userId); 42 rs = ps.executeQuery(); 43 while (rs.next()) { 44 user = mappingUser(rs); 45 } 46 return user; 47 } catch(SQLException e) { 48 throw new DaoException(e.getMessage(), e); 49 } finally { 50 JdbcUtilsSingleton.getInstance().close(rs, ps, conn); 51 } 52 } 53 @Override 54 public User findUser(String name) { 55 Connection conn = null; 56 PreparedStatement ps = null; 57 ResultSet rs = null; 58 User user = null; 59 try { 60 conn = JdbcUtils.getConnection(); 61 String sql = "select id,name,money,birthday from user where name=?"; 62 ps = conn.prepareStatement(sql); 63 ps.setString(1, name); 64 rs = ps.executeQuery(); 65 while(rs.next()) { 66 user = mappingUser(rs); 67 } 68 return user; 69 } catch (SQLException e) { 70 throw new DaoException(e.getMessage(), e); 71 } finally { 72 JdbcUtils.close(rs, ps, conn); 73 } 74 } 75 @Override 76 public void update(User user) { 77 Connection conn = null; 78 PreparedStatement ps = null; 79 ResultSet rs = null; 80 try { 81 conn = JdbcUtils.getConnection(); 82 String sql = "update user set name=?,birthday=?,money=? where id=?"; 83 ps = conn.prepareStatement(sql); 84 ps.setString(1, user.getName()); 85 ps.setDate(2, new java.sql.Date(user.getBirthday().getTime())); 86 ps.setFloat(3, user.getMoney()); 87 ps.setInt(4, user.getId()); 88 ps.executeUpdate(); 89 } catch(SQLException e) { 90 throw new DaoException(e.getMessage(), e); 91 } finally { 92 JdbcUtils.close(rs, ps, conn); 93 } 94 } 95 @Override 96 public void delete(User user) { 97 Connection conn = null; 98 PreparedStatement ps = null; 99 ResultSet rs = null; 100 try { 101 conn = JdbcUtils.getConnection(); 102 String sql = "delete from user where id=?"; 103 ps = conn.prepareStatement(sql); 104 ps.setInt(1, user.getId()); 105 ps.executeUpdate(); 106 } catch (SQLException e) { 107 throw new DaoException(e.getMessage(), e); 108 } finally { 109 JdbcUtils.close(rs, ps, conn); 110 } 111 } 112 113 private User mappingUser(ResultSet rs) throws SQLException { 114 User user; 115 user = new User(); 116 user.setId(rs.getInt("id")); 117 user.setName(rs.getString("name")); 118 user.setBirthday(rs.getDate("birthday")); 119 user.setMoney(rs.getFloat("money")); 120 return user; 121 } 122 }
1 package com.xxyh.jdbc.dao; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.util.Properties; 5 public class DaoFactory { 6 private static DaoFactory instance = null; 7 private static UserDao userDao = null; 8 9 private DaoFactory() { 10 11 try { 12 Properties prop = new Properties(); 13 //InputStream inStream = new FileInputStream(new File("src/daoconfig.properties")); 14 /* 这种方式的好处是不必关心文件的具体路径 */ 15 InputStream inStream = DaoFactory.class.getClassLoader().getResourceAsStream("daoconfig.properties"); 16 prop.load(inStream); 17 String userDaoClass = prop.getProperty("userDaoClass"); 18 Class<?> clazz = Class.forName(userDaoClass); 19 //System.out.println(clazz); 20 userDao = (UserDao) clazz.newInstance(); 21 } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { 22 throw new ExceptionInInitializerError(e); 23 } 24 } 25 26 public static DaoFactory getInstance() { 27 if (instance == null) { 28 synchronized (DaoFactory.class) { 29 if (instance == null) 30 instance = new DaoFactory(); 31 } 32 } 33 return instance; 34 } 35 36 public UserDao getUserDao() { 37 //System.out.println(userDao); 38 return userDao; 39 } 40 }
配置文件daoconfig.properties:
userDaoClass=com.xxyh.jdbc.dao.impl.UserDaoJdbcImpl
JDBC学习笔记(10):DAO设计思想与骨架搭建(小练习)
标签:
原文地址:http://www.cnblogs.com/xiaoxiaoyihan/p/4354676.html