Search in sources :

Example 1 with PersistenceCreationException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException in project zoj by licheng.

the class UserPersistenceImpl method getAllCountries.

/**
     * <p>
     * Gets all countries from the persistence layer.
     * </p>
     * 
     * @return a list containing all country names
     */
public List<Country> getAllCountries() throws PersistenceException {
    if (this.allCountries == null) {
        synchronized (this) {
            if (this.allCountries == null) {
                Connection conn = null;
                try {
                    conn = Database.createConnection();
                    PreparedStatement ps = null;
                    try {
                        ps = conn.prepareStatement(UserPersistenceImpl.GET_ALL_COUNTRIES);
                        ResultSet rs = ps.executeQuery();
                        List<Country> countries = new ArrayList<Country>();
                        while (rs.next()) {
                            countries.add(new Country(rs.getLong(DatabaseConstants.COUNTRY_COUNTRY_ID), rs.getString(DatabaseConstants.COUNTRY_NAME)));
                        }
                        this.allCountries = Collections.unmodifiableList(countries);
                    } finally {
                        Database.dispose(ps);
                    }
                } catch (Exception e) {
                    throw new PersistenceCreationException("Failed to get all countries", e);
                } finally {
                    Database.dispose(conn);
                }
            }
        }
    }
    return this.allCountries;
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) Country(cn.edu.zju.acm.onlinejudge.bean.enumeration.Country) PreparedStatement(java.sql.PreparedStatement) PersistenceCreationException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException) PersistenceCreationException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException)

Aggregations

Country (cn.edu.zju.acm.onlinejudge.bean.enumeration.Country)1 PersistenceCreationException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException)1 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1