小蜜锋 - 云代码空间
—— 技术宅拯救世界!
package com.gym.dao; import java.util.List; import com.gym.model.UserModel; public interface UserDao { /** * 添加用户 * * @param userModel * @return */ public int addUser(UserModel userModel); /** * 删除用户 * * @param aId * @return */ public int delUser(String uId); /** * 修改用户信息 * * @param userModel * @return */ public int alterUser(UserModel userModel); /** * 修改用户密码 * * @param userModel * @return */ public int alterUserPwd(UserModel userModel); /** * 查询所有用户 * * @return */ public List queryUser(); /** * 根据用户名字查询用户 * * @param aName * @return */ public UserModel queryUserByName(String uName); /** * 根据用户id查询用户 * * @param aId * @return */ public UserModel queryUserById(String uId); /** * 查询此用户id是否存在 * * @param aId * @return */ public boolean isExist(String uId); /** * 启用用户 * * @param aId * @return */ public boolean onUser(String uId); /** * 禁用用户 * * @param aId * @return */ public boolean offUser(String uId); /** * 验证用户名密码 * * @param userModel * @return */ public UserModel checkUser(UserModel userModel); /** * 查询我的预约 * * @param userModel * @return */ public List queryMyBook(UserModel userModel); /** * 查询我的租借 * * @param userModel * @return */ public List queryMyRent(UserModel userModel); }以上是UserDao接口,具备用户的增删改查基本操作。其中添加、删除、修改类操作一般返回int类型,这是sql语句执行后影响的行数,如果为1,说明操作成功,否则,说明操作失败;而查询类操作一般返回List对象,List中每一行对应一个userModel,这是一个javabean,是MVC中的M(Model),关于userModel下一篇文章会讲到。这个接口的实现类代码稍长,就不全部粘贴过来了。
public int addUser(UserModel userModel) { // TODO Auto-generated method stub Statement statement = DbConnection.getStatement(); int affect = 0; String sql = "INSERT INTO `user` " + "(uId, uName, uPassword, uEmail, uIdCard, uPhone, date, status)" + "VALUES( '" + userModel.getuId() + "', '" + userModel.getuName() + "', '" + userModel.getuPassword() + "', '" + userModel.getuEmail() + "', '" + userModel.getuIdCard() + "', '" + userModel.getuPhone() + "', '" + userModel.getDate() + "','1')"; // System.out.println(sql); try { affect = statement.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return affect; }注册的时候会调用到这个代码,如果要查看执行的sql语句,可以打开// System.out.println(sql);前面的开关。
INSERT INTO `user` (uId, uName, uPassword, uEmail, uIdCard, uPhone, date, status)VALUES( '201011700000', '郭剑锋', '0e484dad66ec4f08e101ac9b28bc805d', '1421560459@qq.com', '400000000000000000', '13000000000', '2013-2-3 21:12:20','1')密码在服务层已经经过md5加密。
public List queryUser() { // TODO Auto-generated method stub Statement statement = DbConnection.getStatement(); ResultSet resultSet = null; UserModel userModel = null; List list = new ArrayList(); String sql = "SELECT * FROM `user`"; System.out.println(sql); try { resultSet = (ResultSet) statement.executeQuery(sql); while (resultSet.next()) { userModel = new UserModel(); userModel.setDate(resultSet.getString("date")); userModel.setStatus(resultSet.getString("status")); userModel.setuEmail(resultSet.getString("uEmail")); userModel.setuId(resultSet.getString("uId")); userModel.setuIdCard(resultSet.getString("uIdcard")); userModel.setuName(resultSet.getString("uName")); userModel.setuPassword(resultSet.getString("uPassword")); userModel.setuPhone(resultSet.getString("uPhone")); list.add(userModel); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }
<c:forEach items="${requestScope.userList}" var="list"> <tr> <td>${list.getuId()}</td> <td>${list.getuName()}</td> <td>${list.getuEmail()}</td> <td>${list.getuIdCard()}</td> <td>${list.getStatus()}</td> <td><a href="index.html?action=deluser&uid=${list.getuId()}">封禁</a></td> </tr> </c:forEach>注意顶部要导入jstl标签库
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
/** * 获取 Statement * * @return * @throws SQLException */ public static Statement getStatement() { try { // 判断conn是否为空或已关闭 if (conn == null || conn.isClosed()) { // 重新连接数据库 String url = "jdbc:mysql://localhost/gymdb?user=" + dbUser + "&password=" + dbPassword + "&characterEncoding=utf-8"; try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { conn = DriverManager.getConnection(url, dbUser, dbPassword); System.out.println("数据库连接成功"); statement = (Statement) conn.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return statement; } else {// 使用旧连接 statement = (Statement) conn.createStatement(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return statement; }