Search in sources :

Example 81 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericUserDAO method isUsernameRegistered.

/**
	 * @see net.jforum.dao.UserDAO#isUsernameRegistered(java.lang.String)
	 */
public boolean isUsernameRegistered(String username) {
    boolean status = false;
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.isUsernameRegistered"));
        p.setString(1, username);
        rs = p.executeQuery();
        if (rs.next() && rs.getInt("registered") > 0) {
            status = true;
        }
        return status;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 82 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericUserDAO method selectById.

/**
	 * @see net.jforum.dao.UserDAO#selectById(int)
	 */
public User selectById(int userId) {
    String q = SystemGlobals.getSql("UserModel.selectById");
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(q);
        p.setInt(1, userId);
        rs = p.executeQuery();
        User u = new User();
        if (rs.next()) {
            this.fillUserFromResultSet(u, rs);
            u.setPrivateMessagesCount(rs.getInt("private_messages"));
            rs.close();
            p.close();
            // User groups
            p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("UserModel.selectGroups"));
            p.setInt(1, userId);
            rs = p.executeQuery();
            while (rs.next()) {
                Group g = new Group();
                g.setName(rs.getString("group_name"));
                g.setId(rs.getInt("group_id"));
                u.getGroupsList().add(g);
            }
        }
        return u;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : Group(net.jforum.entities.Group) User(net.jforum.entities.User) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 83 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericKarmaDAO method update.

/**
	 * @see net.jforum.dao.KarmaDAO#update(net.jforum.entities.Karma)
	 */
public void update(Karma karma) {
    PreparedStatement p = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.update"));
        p.setInt(1, karma.getPoints());
        p.setInt(2, karma.getId());
        p.executeUpdate();
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(p);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 84 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericKarmaDAO method getPostKarma.

/**
	 * @see net.jforum.dao.KarmaDAO#getPostKarma(int)
	 */
public KarmaStatus getPostKarma(int postId) {
    KarmaStatus karma = new KarmaStatus();
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("KarmaModel.getPostKarma"));
        p.setInt(1, postId);
        rs = p.executeQuery();
        if (rs.next()) {
            karma.setKarmaPoints(Math.round(rs.getDouble(1)));
        }
        return karma;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException) KarmaStatus(net.jforum.entities.KarmaStatus)

Example 85 with DatabaseException

use of net.jforum.exceptions.DatabaseException in project jforum2 by rafaelsteil.

the class GenericKarmaDAO method getMostRatedUserByPeriod.

/**
	 * 
	 * @param sql String
	 * @param firstPeriod Date
	 * @param lastPeriod Date
	 * @return List
	 */
protected List getMostRatedUserByPeriod(String sql, Date firstPeriod, Date lastPeriod) {
    if (firstPeriod.after(lastPeriod)) {
        throw new DatabaseException("First Date needs to be before the Last Date");
    }
    PreparedStatement p = null;
    ResultSet rs = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(sql);
        p.setTimestamp(1, new Timestamp(firstPeriod.getTime()));
        p.setTimestamp(2, new Timestamp(lastPeriod.getTime()));
        rs = p.executeQuery();
        return this.fillUser(rs);
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(rs, p);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DatabaseException(net.jforum.exceptions.DatabaseException) Timestamp(java.sql.Timestamp)

Aggregations

DatabaseException (net.jforum.exceptions.DatabaseException)264 PreparedStatement (java.sql.PreparedStatement)255 SQLException (java.sql.SQLException)254 ResultSet (java.sql.ResultSet)138 List (java.util.List)64 ArrayList (java.util.ArrayList)63 Timestamp (java.sql.Timestamp)17 User (net.jforum.entities.User)15 Iterator (java.util.Iterator)12 Post (net.jforum.entities.Post)9 Date (java.util.Date)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Topic (net.jforum.entities.Topic)8 Connection (java.sql.Connection)5 Bookmark (net.jforum.entities.Bookmark)5 KarmaStatus (net.jforum.entities.KarmaStatus)4 SimpleDateFormat (java.text.SimpleDateFormat)3 PrivateMessage (net.jforum.entities.PrivateMessage)3 Ranking (net.jforum.entities.Ranking)3