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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| package dao;
import db.DBUtil; import model.Goddess;
import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.Map;
public class GoddessDao {
public void addGoddess(Goddess goddess) throws SQLException { Connection conn = DBUtil.getConnection(); String sql = "" + "insert into imooc_goddess" + "(user_name, sex, age, birthday, email, mobile, "+ "create_user, create_date, update_user, update_date, isdel)" + "values("+ "?, ?, ?, ?, ?, ?, ?, current_date(), ?, current_date(), ?)"; PreparedStatement ptmt = conn.prepareStatement(sql); ptmt.setString(1, goddess.getUser_name()); ptmt.setInt(2, goddess.getSex()); ptmt.setInt(3, goddess.getAge()); ptmt.setDate(4, new Date(goddess.getBirthday().getTime())); ptmt.setString(5, goddess.getEmail()); ptmt.setString(6, goddess.getMobile()); ptmt.setString(7, goddess.getCreate_user()); ptmt.setString(8, goddess.getUpdate_user()); ptmt.setInt(9, goddess.getIsdel()); ptmt.execute(); }
public void updateGoddess(Goddess goddess) throws SQLException { Connection conn = DBUtil.getConnection(); String sql = "" + " update imooc_goddess" + " set user_name=?, sex=?, age=?, birthday=?, email=?, mobile=?, "+ " update_user=?, update_date=current_date(), isdel=?"+ " where id = ?"; PreparedStatement ptmt = conn.prepareStatement(sql); ptmt.setString(1, goddess.getUser_name()); ptmt.setInt(2, goddess.getSex()); ptmt.setInt(3, goddess.getAge()); ptmt.setDate(4, new Date(goddess.getBirthday().getTime())); ptmt.setString(5, goddess.getEmail()); ptmt.setString(6, goddess.getMobile()); ptmt.setString(7, goddess.getUpdate_user()); ptmt.setInt(8, goddess.getIsdel()); ptmt.setInt(9, goddess.getId()); ptmt.execute(); }
public void deleteGoddess(int id) throws SQLException { Connection conn = DBUtil.getConnection(); String sql = "" + " delete from imooc_goddess" + " where id = ?"; PreparedStatement ptmt = conn.prepareStatement(sql); ptmt.setInt(1, id); ptmt.execute(); }
public List<Goddess> query() throws SQLException { List<Goddess> gs = new ArrayList<>(); Connection conn = DBUtil.getConnection(); Statement stmt = conn.createStatement(); StringBuilder sb = new StringBuilder("select * from imooc_goddess"); ResultSet rs = stmt.executeQuery(sb.toString());
Goddess g = null; while (rs.next()){ g = new Goddess(); g.setId(rs.getInt("id")); g.setUser_name(rs.getString("user_name")); g.setAge(rs.getInt("age")); g.setSex(rs.getInt("sex")); g.setBirthday(rs.getDate("birthday")); g.setEmail(rs.getString("email")); g.setMobile(rs.getString("mobile")); g.setCreate_date(rs.getDate("create_date")); g.setCreate_user(rs.getString("create_user")); g.setUpdate_date(rs.getDate("update_date")); g.setUpdate_user(rs.getString("update_user")); g.setIsdel(rs.getInt("isdel")); gs.add(g); } return gs; }
public List<Goddess> query(List<Map<String, Object>> params) throws SQLException { List<Goddess> gs = new ArrayList<>(); Connection conn = DBUtil.getConnection(); StringBuilder sb = new StringBuilder("select * from imooc_goddess where 1=1"); if (params != null && params.size() > 0){ for (int i = 0; i < params.size(); i++) { Map<String, Object> map = params.get(i); sb.append(" and " + map.get("name") + " " + map.get("rela") + " " + map.get("value")); } } System.out.println(sb.toString()); PreparedStatement ptmt = conn.prepareStatement(sb.toString()); ResultSet rs = ptmt.executeQuery();
Goddess g = null; while (rs.next()){ g = new Goddess(); g.setUser_name(rs.getString("user_name")); g.setAge(rs.getInt("age")); g.setSex(rs.getInt("sex")); g.setBirthday(rs.getDate("birthday")); g.setEmail(rs.getString("email")); g.setMobile(rs.getString("mobile")); g.setCreate_date(rs.getDate("create_date")); g.setCreate_user(rs.getString("create_user")); g.setUpdate_date(rs.getDate("update_date")); g.setUpdate_user(rs.getString("update_user")); g.setIsdel(rs.getInt("isdel")); gs.add(g); } return gs; }
public Goddess get(int id) throws SQLException { Connection conn = DBUtil.getConnection(); String sql = "" + " select * from imooc_goddess" + " where id = ?"; PreparedStatement ptmt = conn.prepareStatement(sql); ptmt.setInt(1, id);
ResultSet rs = ptmt.executeQuery(); Goddess g = null; while (rs.next()){ g = new Goddess(); g.setUser_name(rs.getString("user_name")); g.setAge(rs.getInt("age")); g.setSex(rs.getInt("sex")); g.setBirthday(rs.getDate("birthday")); g.setEmail(rs.getString("email")); g.setMobile(rs.getString("mobile")); g.setCreate_date(rs.getDate("create_date")); g.setCreate_user(rs.getString("create_user")); g.setUpdate_date(rs.getDate("update_date")); g.setUpdate_user(rs.getString("update_user")); g.setIsdel(rs.getInt("isdel")); } return g; } }
|