Ejemplo n.º 1
0
  @Override
  public User findByUID(int uid) throws Exception {
    // TODO Auto-generated method stub
    // 连接数据库
    DatabaseConnection dbc = new DatabaseConnection();
    Connection conn = dbc.getConnection();
    PreparedStatement pStatement = null;
    // 声明变量用于存取结果
    User user = new User();
    // 搜索语句
    String query = "select * from user where uid=" + uid;
    try {
      // 执行搜索
      pStatement = conn.prepareStatement(query);
      ResultSet rs = pStatement.executeQuery();

      while (rs.next()) {
        user.setMBID(rs.getInt("MBID"));
        user.setFullName(rs.getString("FullName"));
        user.setAvatarUrl(rs.getString("AvatarUrl"));
        user.setEmail(rs.getString("Email"));
        user.setPassWord(rs.getString("PassWord"));
        user.setUID(rs.getInt("UID"));
      }
      // 判断是否为空
      if (user.getFullName() != null) {
        System.out.println("findByUID Successful!");
      } else {
        System.out.println("findByUID No record satisfied!");
        user = null;
      }
      // 关闭连接,返回结果
      // rs.close();
      dbc.close();
      return user;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }
Ejemplo n.º 2
0
  @Override
  public User updateProfile(User user) throws Exception {
    // TODO Auto-generated method stub
    // 连接数据库
    DatabaseConnection dbc = new DatabaseConnection();
    Connection conn = dbc.getConnection();
    PreparedStatement pStatement = null;
    // 声明变量用于存取结果
    User updatedUser = new User();
    // 搜索语句
    String query =
        "UPDATE user SET Email = ?, FullName = ?, AvatarUrl = ?, Password = ? " + "WHERE  uid = ?";
    try {
      // 执行搜索
      pStatement = conn.prepareStatement(query);
      pStatement.setString(1, user.getEmail());
      pStatement.setString(2, user.getFullName());
      pStatement.setString(3, user.getAvatarUrl());
      pStatement.setString(4, user.getPassWord());
      pStatement.setInt(5, user.getUID());
      int row = pStatement.executeUpdate();

      // 判断是否为空
      if (row > 0) {
        updatedUser = user;
        System.out.println("updateProfile Successful!");
      } else {
        System.out.println("updateProfile No record satisfied!");
        user = null;
      }
      // 关闭连接,返回结果
      // rs.close();
      dbc.close();
      return updatedUser;
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }