结对互评
1 package imp; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.text.*; 8 import java.util.ArrayList; 9 import java.util.Date; 10 import java.util.List; 11 12 import bean.Admin; 13 import bean.Book; 14 import bean.Lend; 15 import bean.User; 16 17 import tools.ConnectionManager; 18 import dao.AdminDao; 19 20 public class AdminDaoImp implements AdminDao { 21 22 public Connection con = null; 23 PreparedStatement ps = null; 24 public ResultSet rs = null; 25 26 @Override 27 public Admin login(String a_name, String a_password) { 28 Admin admin = new Admin(); 29 con = ConnectionManager.getConnection(); 30 String sql = "select * from admin where a_username=? and a_password=?"; 31 try { 32 ps = con.prepareStatement(sql); 33 ps.setString(1, a_name); 34 ps.setString(2, a_password); 35 rs = ps.executeQuery(); 36 if (rs.next() == false) { 37 admin.setA_username("00000000"); 38 } else { 39 admin.setA_username("a_name"); 40 } 41 42 } catch (SQLException e) { 43 e.printStackTrace(); 44 } 45 return admin; 46 } 47 48 @Override 49 public int addbooks(Book book) { 50 int result = 0; 51 con = ConnectionManager.getConnection(); 52 String sql = "insert into bookinfo(b_name,b_price,b_writer,b_press,b_type,b_location,b_barcoad) values(?,?,?,?,?,?,?)"; 53 54 try { 55 ps = con.prepareStatement(sql); 56 ps.setString(1, book.getB_name()); 57 ps.setString(2, book.getB_price()); 58 ps.setString(3, book.getB_writer()); 59 ps.setString(4, book.getB_press()); 60 ps.setString(5, book.getB_type()); 61 ps.setString(6, book.getB_location()); 62 ps.setString(7, book.getB_barcoad()); 63 result = ps.executeUpdate(); 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 } 67 return result; 68 } 69 70 @Override 71 public List<User> getAllUser() { 72 List<User> list = new ArrayList<User>(); 73 try { 74 con = ConnectionManager.getConnection(); 75 ps = con.prepareStatement("select * from userinfo"); 76 rs = ps.executeQuery(); 77 while (rs.next()) { 78 User user = new User(); 79 user.setR_id(rs.getString("r_ID")); 80 user.setR_name(rs.getString("r_name")); 81 user.setR_sex(rs.getString("r_sex")); 82 user.setR_occ(rs.getString("r_occ")); 83 user.setR_IDnum(rs.getString("r_IDnum")); 84 user.setTel(rs.getString("r_tel")); 85 user.setR_mail(rs.getString("r_mail")); 86 list.add(user); 87 } 88 } catch (SQLException e) { 89 e.printStackTrace(); 90 } 91 return list; 92 } 93 94 @Override 95 public List<Book> getAllBook() { 96 List<Book> list = new ArrayList<Book>(); 97 try { 98 con = ConnectionManager.getConnection(); 99 ps = con.prepareStatement("select * from bookinfo"); 100 rs = ps.executeQuery(); 101 while (rs.next()) { 102 Book book = new Book(); 103 book.setB_ID(rs.getString("b_ID")); 104 book.setB_name(rs.getString("b_name")); 105 book.setB_price(rs.getString("b_price")); 106 book.setB_writer(rs.getString("b_writer")); 107 book.setB_press(rs.getString("b_press")); 108 book.setB_type(rs.getString("b_type")); 109 book.setB_location(rs.getString("b_location")); 110 book.setB_barcoad(rs.getString("b_barcoad")); 111 list.add(book); 112 } 113 } catch (SQLException e) { 114 e.printStackTrace(); 115 } 116 return list; 117 } 118 119 @Override 120 public int deletBook(String bid) { 121 int result = 0; 122 Connection connection = ConnectionManager.getConnection(); 123 String sql = "DELETE FROM bookinfo WHERE b_ID=?"; 124 125 try { 126 PreparedStatement preparedStatement = connection.prepareStatement(sql); 127 preparedStatement.setString(1, bid); 128 result = preparedStatement.executeUpdate(); 129 if (result != 0) { 130 result = 1; 131 } else { 132 result = 0; 133 } 134 } catch (SQLException e) { 135 e.printStackTrace(); 136 } 137 return result; 138 } 139 140 @Override 141 public Book getbook(String bid) { 142 Book book = new Book(); 143 con = ConnectionManager.getConnection(); 144 String sql = "select * from bookinfo where b_ID=?"; 145 try { 146 ps = con.prepareStatement(sql); 147 ps.setString(1, bid); 148 rs = ps.executeQuery(); 149 while (rs.next()) { 150 book.setB_ID(rs.getString("b_ID")); 151 book.setB_name(rs.getString("b_name")); 152 book.setB_price(rs.getString("b_price")); 153 book.setB_writer(rs.getString("b_writer")); 154 book.setB_press(rs.getString("b_press")); 155 book.setB_type(rs.getString("b_type")); 156 book.setB_location(rs.getString("b_location")); 157 book.setB_barcoad(rs.getString("b_barcoad")); 158 } 159 160 } catch (SQLException e) { 161 e.printStackTrace(); 162 } 163 return book; 164 } 165 166 @Override 167 public int updatebook(String bid, Book book) { 168 int result = 0; 169 con = ConnectionManager.getConnection(); 170 String sql = "UPDATE bookinfo SET b_name=?, b_price=?, b_writer=?, b_press=?, b_type=?, b_location=?, b_barcoad=? WHERE b_ID=?;"; 171 172 try { 173 ps = con.prepareStatement(sql); 174 ps.setString(1, book.getB_name()); 175 ps.setString(2, book.getB_price()); 176 ps.setString(3, book.getB_writer()); 177 ps.setString(4, book.getB_press()); 178 ps.setString(5, book.getB_type()); 179 ps.setString(6, book.getB_location()); 180 ps.setString(7, book.getB_barcoad()); 181 ps.setString(8, bid); 182 result = ps.executeUpdate(); 183 if (result != 0) { 184 result = 1; 185 } else { 186 result = 0; 187 } 188 } catch (SQLException e) { 189 e.printStackTrace(); 190 } 191 return result; 192 } 193 194 @Override 195 public int borrowBook(Lend lend) { 196 int result = 0; 197 con = ConnectionManager.getConnection(); 198 String sql = "insert into lending(r_tel,b_barcoad,l_borrowtime,l_backtime,l_return,l_punishment) values(?,?,?,?,?,?)"; 199 200 try { 201 ps = con.prepareStatement(sql); 202 ps.setString(1, lend.getR_tel()); 203 ps.setString(2, lend.getB_barcoad()); 204 ps.setString(3, lend.getL_borrowtime()); 205 ps.setString(4, lend.getL_backtime()); 206 ps.setString(5, lend.getL_return()); 207 ps.setString(6, lend.getL_punishment()); 208 result = ps.executeUpdate(); 209 } catch (SQLException e) { 210 e.printStackTrace(); 211 } 212 return result; 213 } 214 215 @Override 216 public int returnbook(Lend lend) { 217 int result = 0; 218 con = ConnectionManager.getConnection(); 219 String sql = "UPDATE lending SET l_return=? WHERE r_tel=? AND b_barcoad=?;"; 220 221 try { 222 ps = con.prepareStatement(sql); 223 ps.setInt(1, 1); 224 ps.setString(2, lend.getR_tel()); 225 ps.setString(3, lend.getB_barcoad()); 226 result = ps.executeUpdate(); 227 if (result != 0) { 228 result = 1; 229 } else { 230 result = 0; 231 } 232 } catch (SQLException e) { 233 e.printStackTrace(); 234 } 235 return result; 236 } 237 238 @Override 239 public void overdueBook() { 240 List<Lend> time = getTime(); 241 DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 242 for (int i = 0; i < time.size(); i++) { 243 Lend lend = new Lend(); 244 lend = (Lend) time.get(i); 245 try { 246 Date date = new Date(); 247 String times = df.format(date); 248 Date d1 = df.parse(lend.getL_backtime()); 249 Date d2 = df.parse(times); 250 long diff = d1.getTime() - d2.getTime(); 251 long punishment = diff / (1000 * 60 * 60 * 24); 252 if (punishment < 0) { 253 punishment(lend.getID()); 254 } 255 } catch (Exception e) { 256 } 257 } 258 } 259 260 261 public void punishment(String ID) { 262 con = ConnectionManager.getConnection(); 263 String sql = "UPDATE lending SET l_punishment=1 WHERE ID=?;"; 264 265 try { 266 ps = con.prepareStatement(sql); 267 ps.setString(1, ID); 268 ps.executeUpdate(); 269 } catch (SQLException e) { 270 e.printStackTrace(); 271 } 272 } 273 274 public List<Lend> getTime() { 275 List<Lend> list = new ArrayList<Lend>(); 276 try { 277 con = ConnectionManager.getConnection(); 278 ps = con.prepareStatement("SELECT * FROM lending WHERE l_punishment = 0;"); 279 rs = ps.executeQuery(); 280 while (rs.next()) { 281 Lend lend = new Lend(); 282 lend.setL_backtime(rs.getString("l_backtime")); 283 lend.setID(rs.getString("ID")); 284 list.add(lend); 285 } 286 } catch (SQLException e) { 287 e.printStackTrace(); 288 } 289 return list; 290 } 291 292 @Override 293 public List<Lend> getoverdue() { 294 overdueBook(); 295 List<Lend> list = new ArrayList<Lend>(); 296 try { 297 con = ConnectionManager.getConnection(); 298 ps = con.prepareStatement("select * from lending WHERE l_punishment = 1;"); 299 rs = ps.executeQuery(); 300 while (rs.next()) { 301 Lend lend = new Lend(); 302 lend.setR_tel(rs.getString("r_tel")); 303 lend.setB_barcoad(rs.getString("b_barcoad")); 304 lend.setL_backtime(rs.getString("l_backtime")); 305 list.add(lend); 306 } 307 } catch (SQLException e) { 308 e.printStackTrace(); 309 } 310 return list; 311 } 312 }
1.代码冗长,可以尝试把其中重复部分做成一个方法,再调用
2.没有注释,不是一个好习惯
3.变量声明不够明确