/** * 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; }