Example usage for java.sql PreparedStatement executeQuery

List of usage examples for java.sql PreparedStatement executeQuery

Introduction

In this page you can find the example usage for java.sql PreparedStatement executeQuery.

Prototype

ResultSet executeQuery() throws SQLException;

Source Link

Document

Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.

Usage

From source file:cn.edu.zju.acm.onlinejudge.persistence.sql.Database.java

/**
 * Gets the last id./* ww w . j a  v a  2 s  .c o  m*/
 * 
 * @param conn
 * @param ps
 * @param rs
 * @return the last id
 * @throws SQLException
 */
public static long getLastId(Connection conn) throws SQLException {
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(Database.GET_LAST_ID);
        ResultSet rs = ps.executeQuery();
        rs.next();
        return rs.getLong(1);
    } finally {
        Database.dispose(ps);
    }
}

From source file:com.concursive.connect.web.modules.activity.utils.ProjectHistoryUtils.java

public static int queryAdditionalCommentsCount(Connection db, ProjectHistory projectHistory)
        throws SQLException {
    int count = 0;
    int topId = projectHistory.getTopId();
    if (topId == -1) {
        topId = projectHistory.getId();/*from ww  w.  j a v  a 2s.  c  o m*/
    }
    PreparedStatement pst = db.prepareStatement("SELECT count(*) AS comment_count " + "FROM project_history "
            + "WHERE top_id = ? AND position > ? ");
    pst.setInt(1, topId);
    pst.setInt(2, projectHistory.getPosition());
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        count = rs.getInt("comment_count");
    }
    rs.close();
    pst.close();
    return count;
}

From source file:com.nabla.dc.server.handler.fixed_asset.Asset.java

static private int getAssetDepreciationBeforeDisposal(final Connection conn, int assetId) throws SQLException {
    final PreparedStatement stmt = StatementFormat.prepare(conn,
            "SELECT SUM(amount) FROM fa_transaction WHERE fa_asset_id=? AND class='DEP';", assetId);
    try {/*from   ww  w.  ja  v a2  s.  com*/
        final ResultSet rs = stmt.executeQuery();
        try {
            return rs.next() ? rs.getInt(1) : 0;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:com.sql.CaseType.java

/**
 * Gathers a list of active case types for finding the proper section based 
 * on the case number.//from   w ww  .  j  a v a2  s . co m
 * 
 * @param section For which section the method is currently processing
 * @return List CaseTypeModel
 */
public static List<CaseTypeModel> getCaseTypesBySection(String section) {
    List<CaseTypeModel> list = new ArrayList();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "SELECT * FROM CaseType WHERE active = 1 AND section = ?";
        ps = conn.prepareStatement(sql);
        ps.setString(1, section);
        rs = ps.executeQuery();
        while (rs.next()) {
            CaseTypeModel item = new CaseTypeModel();
            item.setId(rs.getInt("id"));
            item.setActive(rs.getBoolean("active"));
            item.setSection(rs.getString("Section"));
            item.setCaseType(rs.getString("caseType"));
            item.setDescription(rs.getString("Description"));
            list.add(item);
        }
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
    return list;
}

From source file:com.nabla.dc.server.handler.fixed_asset.Asset.java

static private int getAssetCostBeforeDisposal(final Connection conn, int assetId) throws SQLException {
    final PreparedStatement stmt = StatementFormat.prepare(conn,
            "SELECT SUM(amount) FROM fa_transaction WHERE fa_asset_id=? AND class='COST';", assetId);
    try {//from  w w  w . j a  v a  2s. co  m
        final ResultSet rs = stmt.executeQuery();
        try {
            return rs.next() ? rs.getInt(1) : 0;
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:org.works.integration.storedproc.derby.DerbyStoredProcedures.java

public static void findCoffee(int coffeeId, String[] coffeeDescription) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try {/*from   www.  j  a  v a 2s.  c  o m*/
        connection = DriverManager.getConnection("jdbc:default:connection");
        String sql = "SELECT * FROM COFFEE_BEVERAGES WHERE ID = ? ";
        statement = connection.prepareStatement(sql);
        statement.setLong(1, coffeeId);

        ResultSet resultset = statement.executeQuery();
        resultset.next();
        coffeeDescription[0] = resultset.getString("COFFEE_DESCRIPTION");

    } finally {
        JdbcUtils.closeStatement(statement);
        JdbcUtils.closeConnection(connection);
    }

}

From source file:at.becast.youploader.account.Account.java

public static Account read(int id) throws IOException {
    PreparedStatement stmt;
    try {// w  ww.j a v  a  2s . c  o m
        stmt = c.prepareStatement("SELECT * FROM `accounts` WHERE `id`=? LIMIT 1");
        stmt.setInt(1, id);
        ResultSet rs = stmt.executeQuery();
        ObjectMapper mapper = new ObjectMapper();
        List<Cookie> c = mapper.readValue(rs.getString("cookie"), new TypeReference<List<Cookie>>() {
        });
        String name = rs.getString("name");
        String token = rs.getString("refresh_token");
        stmt.close();
        rs.close();
        return new Account(id, name, token, c);
    } catch (SQLException e) {
        LOG.error("Account read error!", e);
        return null;
    }
}

From source file:com.sql.Activity.java

/**
 * Gathers a list of tiles that are awaiting a timestamp
 * //from   www.ja  va2  s . c  o  m
 * @return List (ActivityModel)
 */
public static List<ActivityModel> getFilesToStamp() {
    List<ActivityModel> list = new ArrayList();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "SELECT * FROM Activity WHERE awaitingTimestamp = 1";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            ActivityModel type = new ActivityModel();
            type.setId(rs.getInt("id"));
            type.setCaseYear(rs.getString("caseYear"));
            type.setCaseType(rs.getString("caseType"));
            type.setCaseMonth(rs.getString("caseMonth"));
            type.setCaseNumber(rs.getString("caseNumber"));
            type.setDate(rs.getTimestamp("date"));
            type.setFileName(rs.getString("fileName"));
            list.add(type);
        }
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
    return list;
}

From source file:at.becast.youploader.account.Account.java

public static Account read(String name) throws IOException {
    PreparedStatement stmt;
    try {//from ww w . j a  v  a 2  s .c  o m
        stmt = c.prepareStatement("SELECT * FROM `accounts` WHERE `name`=? LIMIT 1");
        stmt.setString(1, name);
        ResultSet rs = stmt.executeQuery();
        ObjectMapper mapper = new ObjectMapper();
        List<Cookie> c = mapper.readValue(rs.getString("cookie"), new TypeReference<List<Cookie>>() {
        });
        int id = rs.getInt("id");
        String token = rs.getString("refresh_token");
        stmt.close();
        rs.close();
        return new Account(id, name, token, c);
    } catch (SQLException e) {
        LOG.error("Account read error!", e);
        return null;
    }
}

From source file:com.keybox.manage.db.ProfileDB.java

/**
 * returns profile based on id/*w w  w .  j ava 2  s.c om*/
 *
 * @param con db connection object
 * @param profileId profile id
 * @return profile
 */
public static Profile getProfile(Connection con, Long profileId) {

    Profile profile = null;
    try {
        PreparedStatement stmt = con.prepareStatement("select * from profiles where id=?");
        stmt.setLong(1, profileId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            profile = new Profile();
            profile.setId(rs.getLong("id"));
            profile.setNm(rs.getString("nm"));
            profile.setDesc(rs.getString("desc"));
            profile.setHostSystemList(ProfileSystemsDB.getSystemsByProfile(con, profileId));

        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }

    return profile;
}