Exemple #1
0
  /**
   * Determine whether or a not a User with the supplied researcherID exists
   *
   * @param username The researcherID to test
   * @return true if the user exists, false if not
   * @throws SQLException if a database error was encountered
   */
  public static boolean userExists(int researcherID) throws SQLException {
    boolean returnVal = false;

    // Get our connection to the database.
    Connection conn = DBConnectionManager.getConnection("yrc");
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
      stmt = conn.prepareStatement("SELECT researcherID FROM tblUsers WHERE researcherID = ?");
      stmt.setInt(1, researcherID);

      rs = stmt.executeQuery();

      // No rows returned.
      if (!rs.next()) {
        returnVal = false;
      } else {
        returnVal = true;
      }

      rs.close();
      rs = null;

      stmt.close();
      stmt = null;

      conn.close();
      conn = null;
    } finally {

      // Always make sure result sets and statements are closed,
      // and the connection is returned to the pool
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException e) {;
        }
        rs = null;
      }
      if (stmt != null) {
        try {
          stmt.close();
        } catch (SQLException e) {;
        }
        stmt = null;
      }
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException e) {;
        }
        conn = null;
      }
    }

    return returnVal;
  }
 /**
  * 获得数据库连接
  *
  * @return : 一个连接
  */
 public void getConnection() {
   if (conn == null) {
     try {
       conn = dbManager.getConnection();
     } catch (SQLException ex) {
       Debug.getAppErrLogger().error("取数据库连接错误!", ex);
     }
   }
 }
Exemple #3
0
  /**
   * Determine whether or a not a Researcher with the supplied email exists
   *
   * @param email The email to test
   * @return The researcher ID of the researcher if it exists, -1 if it doesn't
   * @throws SQLException if a database error was encountered
   */
  public static int emailExists(String email) throws SQLException {
    int returnVal = -1;

    if (email == null || email.equals("")) {
      return -1;
    }

    // Get our connection to the database.
    Connection conn = DBConnectionManager.getConnection("yrc");
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
      stmt =
          conn.prepareStatement(
              "SELECT researcherID FROM tblResearchers WHERE researcherEmail = ?");
      stmt.setString(1, email);

      rs = stmt.executeQuery();

      // No rows returned.
      if (!rs.next()) {
        returnVal = -1;
      } else {
        returnVal = rs.getInt("researcherID");
      }

      rs.close();
      rs = null;

      stmt.close();
      stmt = null;

      conn.close();
      conn = null;
    } finally {

      // Always make sure result sets and statements are closed,
      // and the connection is returned to the pool
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException e) {;
        }
        rs = null;
      }
      if (stmt != null) {
        try {
          stmt.close();
        } catch (SQLException e) {;
        }
        stmt = null;
      }
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException e) {;
        }
        conn = null;
      }
    }

    return returnVal;
  }
Exemple #4
0
  /**
   * Get a populated User object corresponding to a username.
   *
   * @param username The username to test
   * @return The User object corresponding to that username.
   * @throws NoSuchUserException if that username does not exist.
   * @throws SQLException if a database error was encountered.
   */
  public static User getUser(String username) throws NoSuchUserException, SQLException {
    // The User to return
    User theUser;

    // Make sure the username isn't null
    if (username == null) {
      throw new NoSuchUserException("got null for username in UserUtils.getUser");
    }

    // Get our connection to the database.
    Connection conn = DBConnectionManager.getConnection("yrc");
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try {
      stmt = conn.prepareStatement("SELECT researcherID FROM tblUsers WHERE username = ?");
      stmt.setString(1, username);

      rs = stmt.executeQuery();

      // No rows returned.
      if (!rs.next()) {
        throw new NoSuchUserException("Username not found.");
      }

      theUser = new User();

      try {
        theUser.load(rs.getInt("researcherID"));
      } catch (InvalidIDException e) {
        throw new NoSuchUserException(
            "Somehow, we got an invalid ID ("
                + rs.getInt("researcherID")
                + ") after we got the ID from the username...  This can't be good.");
      }

      rs.close();
      rs = null;

      stmt.close();
      stmt = null;

      conn.close();
      conn = null;
    } finally {

      // Always make sure result sets and statements are closed,
      // and the connection is returned to the pool
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException e) {;
        }
        rs = null;
      }
      if (stmt != null) {
        try {
          stmt.close();
        } catch (SQLException e) {;
        }
        stmt = null;
      }
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException e) {;
        }
        conn = null;
      }
    }

    return theUser;
  }