我使用我同学实训时编写的一段代码,这是一个数据库的操作类.
package imp; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import tools.ConnectionManager; import bean.User; import dao.UserDao; /* 用户操作功能类 */ public class UserDaoImp implements UserDao { //数据库链接函数 public Connection con = null; PreparedStatement ps = null; public ResultSet rs = null; /* 添加用户 */ @Override public int insertUse(User user) { int result = 0;//结果集 con = ConnectionManager.getConnection(); String sql = "insert into user(uId,uName,uPassword,uPhone,uAddress,uBalance) values(?,?,?,?,?,?)"; try { ps = con.prepareStatement(sql); ps.setString(1, user.getuId()); ps.setString(2, user.getuName()); ps.setString(3, user.getuPassword()); ps.setString(4, user.getuPhone()); ps.setString(5, user.getuAddress()); ps.setString(6, user.getuBalance()); result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return result; } /* 检查用户登录 */ @Override public User checkUser(String uid, String upassword) { User user = new User(); con = ConnectionManager.getConnection(); String sql = "select * from user where uId=? and uPassword=?";//建立在用户名和密码是否匹配的基础上 try { ps = con.prepareStatement(sql); ps.setString(1, uid); ps.setString(2, upassword); rs = ps.executeQuery(); if (rs.next() == false) { user.setuId("00000000"); } else { user.setuId("uId"); } } catch (SQLException e) { e.printStackTrace(); } return user; } /* 搜索用户信息 */ @Override public User searchUser(String uid) { User user = new User(); con = ConnectionManager.getConnection(); String sql = "select * from user where uId=?"; try { ps = con.prepareStatement(sql); ps.setString(1, uid); rs = ps.executeQuery(); while (rs.next()) { user.setuId(rs.getString("uId")); user.setuName(rs.getString("uName")); user.setuPassword(rs.getString("uPassword")); user.setuAddress(rs.getString("uAddress")); user.setuPhone(rs.getString("uPhone")); user.setuBalance(rs.getString("uBalance")); } } catch (SQLException e) { e.printStackTrace(); } return user; } /* 列出用户列表信息 */ @Override public List<User> getAllUser() { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; List<User> list = new ArrayList<User>(); try { con = ConnectionManager.getConnection(); ps = con.prepareStatement("select * from user"); rs = ps.executeQuery(); while (rs.next()) { User user = new User(); user.setuId(rs.getString("uId")); user.setuName(rs.getString("uName")); user.setuAddress(rs.getString("uAddress")); user.setuPhone(rs.getString("uPhone")); user.setuBalance(rs.getString("uBalance")); list.add(user); } } catch (SQLException e) { e.printStackTrace(); } return list; } /* 更新管理员信息 */ @Override public int updateAdmin(String uid, User user) { int result = 0; con = ConnectionManager.getConnection(); String sql = "UPDATE user SET uName=?, uPassword=?, uPhone=?, uAddress=?, uBalance=? WHERE `uId`=?;";//通过用户ID进行修改 try { ps = con.prepareStatement(sql); ps.setString(1, user.getuName()); ps.setString(2, user.getuPassword()); ps.setString(3, user.getuPhone()); ps.setString(4, user.getuAddress()); ps.setString(5, user.getuBalance()); ps.setString(6, uid); result = ps.executeUpdate(); if (result != 0) { result = 1; } else { result = 0; } } catch (SQLException e) { e.printStackTrace(); } return result; } /* 删除用户 */ @Override public int deleteUser(String uid) { int result = 0; con = ConnectionManager.getConnection(); String sql = "DELETE FROM user WHERE uId=?";//通过用户ID的索引进行删除 try { ps = con.prepareStatement(sql); ps.setString(1, uid); result = ps.executeUpdate(); if (result != 0) { result = 1; } else { result = 0; } } catch (SQLException e) { e.printStackTrace(); } return result; } /* 更新用户信息 */ @Override public int updateUser(String uid, User user) { int result = 0; con = ConnectionManager.getConnection(); String sql = "UPDATE user SET uName=?, uPassword=?, uPhone=?, uAddress=? WHERE `uId`=?;";//通过用户ID进行更新 try { ps = con.prepareStatement(sql); ps.setString(1, user.getuName()); ps.setString(2, user.getuPassword()); ps.setString(3, user.getuPhone()); ps.setString(4, user.getuAddress()); ps.setString(5, uid); result = ps.executeUpdate(); if (result != 0) { result = 1; } else { result = 0; } } catch (SQLException e) { e.printStackTrace(); } return result; } /* 模糊查询 */ @Override public List fuzzySearchUser(String uname) { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; List<User> list = new ArrayList<User>(); try { con = ConnectionManager.getConnection(); ps = con.prepareStatement("select * from user where uname like '%"+uname+"%'");//用uname参数执行模糊查询 rs = ps.executeQuery(); while (rs.next()) { User user = new User(); user.setuId(rs.getString("uId")); user.setuName(rs.getString("uName")); user.setuAddress(rs.getString("uAddress")); user.setuPhone(rs.getString("uPhone")); user.setuBalance(rs.getString("uBalance")); list.add(user); } } catch (SQLException e) { e.printStackTrace(); } return list; } }
评审的过程中仍然要注意的是不要改源代码.
下面是我对我的这个类的Code Review.
- 注释太随意,类的功能说明不完全.