My code review
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.util.ArrayList; 8 import java.util.List; 9 10 import tools.ConnectionManager; 11 12 import bean.User; 13 import dao.UserDao; 14 /* 15 用户操作功能类 16 */ 17 public class UserDaoImp implements UserDao { 18 //数据库链接函数 19 public Connection con = null; 20 PreparedStatement ps = null; 21 public ResultSet rs = null; 22 /* 23 添加用户 24 */ 25 @Override 26 public int insertUse(User user) { 27 int result = 0;//结果集 28 con = ConnectionManager.getConnection(); 29 String sql = "insert into user(uId,uName,uPassword,uPhone,uAddress,uBalance) values(?,?,?,?,?,?)"; 30 31 try { 32 ps = con.prepareStatement(sql); 33 ps.setString(1, user.getuId()); 34 ps.setString(2, user.getuName()); 35 ps.setString(3, user.getuPassword()); 36 ps.setString(4, user.getuPhone()); 37 ps.setString(5, user.getuAddress()); 38 ps.setString(6, user.getuBalance()); 39 result = ps.executeUpdate(); 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 return result; 44 } 45 /* 46 检查用户登录 47 */ 48 @Override 49 public User checkUser(String uid, String upassword) { 50 User user = new User(); 51 con = ConnectionManager.getConnection(); 52 String sql = "select * from user where uId=? and uPassword=?";//建立在用户名和密码是否匹配的基础上 53 try { 54 ps = con.prepareStatement(sql); 55 ps.setString(1, uid); 56 ps.setString(2, upassword); 57 rs = ps.executeQuery(); 58 if (rs.next() == false) { 59 user.setuId("00000000"); 60 } else { 61 user.setuId("uId"); 62 } 63 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 } 67 return user; 68 } 69 /* 70 搜索用户信息 71 */ 72 @Override 73 public User searchUser(String uid) { 74 User user = new User(); 75 con = ConnectionManager.getConnection(); 76 String sql = "select * from user where uId=?"; 77 try { 78 ps = con.prepareStatement(sql); 79 ps.setString(1, uid); 80 rs = ps.executeQuery(); 81 while (rs.next()) { 82 user.setuId(rs.getString("uId")); 83 user.setuName(rs.getString("uName")); 84 user.setuPassword(rs.getString("uPassword")); 85 user.setuAddress(rs.getString("uAddress")); 86 user.setuPhone(rs.getString("uPhone")); 87 user.setuBalance(rs.getString("uBalance")); 88 } 89 90 } catch (SQLException e) { 91 e.printStackTrace(); 92 } 93 return user; 94 } 95 /* 96 列出用户列表信息 97 */ 98 @Override 99 public List<User> getAllUser() { 100 Connection con = null; 101 PreparedStatement ps = null; 102 ResultSet rs = null; 103 List<User> list = new ArrayList<User>(); 104 try { 105 con = ConnectionManager.getConnection(); 106 ps = con.prepareStatement("select * from user"); 107 rs = ps.executeQuery(); 108 while (rs.next()) { 109 User user = new User(); 110 user.setuId(rs.getString("uId")); 111 user.setuName(rs.getString("uName")); 112 user.setuAddress(rs.getString("uAddress")); 113 user.setuPhone(rs.getString("uPhone")); 114 user.setuBalance(rs.getString("uBalance")); 115 list.add(user); 116 } 117 } catch (SQLException e) { 118 e.printStackTrace(); 119 } 120 return list; 121 } 122 /* 123 更新管理员信息 124 */ 125 @Override 126 public int updateAdmin(String uid, User user) { 127 int result = 0; 128 con = ConnectionManager.getConnection(); 129 String sql = "UPDATE user SET uName=?, uPassword=?, uPhone=?, uAddress=?, uBalance=? WHERE `uId`=?;";//通过用户ID进行修改 130 131 try { 132 ps = con.prepareStatement(sql); 133 ps.setString(1, user.getuName()); 134 ps.setString(2, user.getuPassword()); 135 ps.setString(3, user.getuPhone()); 136 ps.setString(4, user.getuAddress()); 137 ps.setString(5, user.getuBalance()); 138 ps.setString(6, uid); 139 result = ps.executeUpdate(); 140 if (result != 0) { 141 result = 1; 142 } else { 143 result = 0; 144 } 145 } catch (SQLException e) { 146 e.printStackTrace(); 147 } 148 return result; 149 } 150 /* 151 删除用户 152 */ 153 @Override 154 public int deleteUser(String uid) { 155 int result = 0; 156 con = ConnectionManager.getConnection(); 157 String sql = "DELETE FROM user WHERE uId=?";//通过用户ID的索引进行删除 158 159 try { 160 ps = con.prepareStatement(sql); 161 ps.setString(1, uid); 162 result = ps.executeUpdate(); 163 if (result != 0) { 164 result = 1; 165 } else { 166 result = 0; 167 } 168 } catch (SQLException e) { 169 e.printStackTrace(); 170 } 171 return result; 172 } 173 /* 174 更新用户信息 175 */ 176 @Override 177 public int updateUser(String uid, User user) { 178 int result = 0; 179 con = ConnectionManager.getConnection(); 180 String sql = "UPDATE user SET uName=?, uPassword=?, uPhone=?, uAddress=? WHERE `uId`=?;";//通过用户ID进行更新 181 182 try { 183 ps = con.prepareStatement(sql); 184 ps.setString(1, user.getuName()); 185 ps.setString(2, user.getuPassword()); 186 ps.setString(3, user.getuPhone()); 187 ps.setString(4, user.getuAddress()); 188 ps.setString(5, uid); 189 result = ps.executeUpdate(); 190 if (result != 0) { 191 result = 1; 192 } else { 193 result = 0; 194 } 195 } catch (SQLException e) { 196 e.printStackTrace(); 197 } 198 return result; 199 } 200 /* 201 模糊查询 202 */ 203 @Override 204 public List fuzzySearchUser(String uname) { 205 Connection con = null; 206 PreparedStatement ps = null; 207 ResultSet rs = null; 208 List<User> list = new ArrayList<User>(); 209 try { 210 con = ConnectionManager.getConnection(); 211 ps = con.prepareStatement("select * from user where uname like '%"+uname+"%'");//用uname参数执行模糊查询 212 rs = ps.executeQuery(); 213 while (rs.next()) { 214 User user = new User(); 215 user.setuId(rs.getString("uId")); 216 user.setuName(rs.getString("uName")); 217 user.setuAddress(rs.getString("uAddress")); 218 user.setuPhone(rs.getString("uPhone")); 219 user.setuBalance(rs.getString("uBalance")); 220 list.add(user); 221 } 222 } catch (SQLException e) { 223 e.printStackTrace(); 224 } 225 return list; 226 } 227 }
以我现在的水平看过去自己所未能发现的问题,求轻喷,如果可以,也帮我改进下我的不合理之处,不甚感谢!
1.代码注释不详细,每个类都没有明确写出功能(基本没有注释,上面的注释都是后来加上的,现在才养起这个习惯并坚持下去)
2.有些参数没有明确指定在哪里调用,可能会出现跑偏或用错函数的问题
3.java.util.*慎用(我觉得这不像写代码的,像拐卖人口的)
4.虽说只要实现了功能就好,可还是要注意顺序问题,最好有明确的顺序,可以更好地找到功能并对应
5.最好不要使用组合声明,应分开并注释这些声明分别对应哪些指定的函数操作。
6.入行时间太少,手太生,只关注了程序是否能跑起来,并未关注程序的严谨性和逻辑性。