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:org.works.integration.storedproc.derby.DerbyStoredProcedures.java

public static void findAllCoffeeBeverages(ResultSet[] coffeeBeverages) throws SQLException {

    Connection connection = null;
    PreparedStatement statement = null;

    connection = DriverManager.getConnection("jdbc:default:connection");
    String sql = "SELECT * FROM COFFEE_BEVERAGES";
    statement = connection.prepareStatement(sql);
    coffeeBeverages[0] = statement.executeQuery();

}

From source file:com.senior.g40.service.UserService.java

public static Profile getProfileByUserId(long userId) throws SQLException {
    Profile pf = null;/*from   w  w  w. jav  a 2s . c  o  m*/

    Connection conn = ConnectionBuilder.getConnection();
    String sqlCmd = "SELECT * FROM `profile` WHERE userId = ?;";
    PreparedStatement pstm = conn.prepareStatement(sqlCmd);
    pstm.setLong(1, userId);
    ResultSet rs = pstm.executeQuery();

    if (rs.next()) {
        pf = new Profile();
        setProfile(rs, pf);
        conn.close();
        return pf;
    }
    conn.close();
    return null;
}

From source file:com.ec2box.manage.db.UserThemeDB.java

/**
 * get user theme// w  w  w.j av a 2 s  .c  o  m
 *
 * @param userId object
 * @return user theme object
 */
public static UserSettings getTheme(Long userId) {

    UserSettings theme = null;
    Connection con = null;
    try {
        con = DBUtils.getConn();

        PreparedStatement stmt = con.prepareStatement("select * from user_theme where user_id=?");
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            theme = new UserSettings();
            theme.setBg(rs.getString("bg"));
            theme.setFg(rs.getString("fg"));
            if (StringUtils.isNotEmpty(rs.getString("d1"))) {
                String[] colors = new String[16];
                colors[0] = rs.getString("d1");
                colors[1] = rs.getString("d2");
                colors[2] = rs.getString("d3");
                colors[3] = rs.getString("d4");
                colors[4] = rs.getString("d5");
                colors[5] = rs.getString("d6");
                colors[6] = rs.getString("d7");
                colors[7] = rs.getString("d8");
                colors[8] = rs.getString("b1");
                colors[9] = rs.getString("b2");
                colors[10] = rs.getString("b3");
                colors[11] = rs.getString("b4");
                colors[12] = rs.getString("b5");
                colors[13] = rs.getString("b6");
                colors[14] = rs.getString("b7");
                colors[15] = rs.getString("b8");
                theme.setColors(colors);
            }
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DBUtils.closeConn(con);
    }

    return theme;

}

From source file:de.thejeterlp.bukkit.login.SQLAccount.java

protected static Account convert(UUID uuid) throws SQLException {
    checkReflection();//www  .j a  va2 s.c  o  m
    Validate.notNull(uuid, "uuid cannot be null!");
    PreparedStatement st = Login.getInstance().getDB()
            .getPreparedStatement("SELECT * FROM `" + Statics.USER_TABLE + "` WHERE `uuid` = ? LIMIT 1;");
    st.setString(1, uuid.toString());
    ResultSet rs = st.executeQuery();
    while (rs.next()) {
        int id = rs.getInt("id");
        Login.getInstance().getDB().closeResultSet(rs);
        Login.getInstance().getDB().closeStatement(st);
        PreparedStatement sta = Login.getInstance().getDB().getPreparedStatement(
                "SELECT * FROM `" + Statics.PASSWORD_TABLE + "` WHERE `userID` = ? LIMIT 1;");
        sta.setInt(1, id);
        ResultSet rset = sta.executeQuery();
        while (rset.next()) {
            String hash = rset.getString("password");
            Login.getInstance().getDB().closeResultSet(rset);
            Login.getInstance().getDB().closeStatement(sta);
            return new Account(id, uuid, hash);
        }
    }
    return null;
}

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

/**
 * get user theme/*from   ww  w.ja v  a 2 s.  c  o  m*/
 *
 * @param userId object
 * @return user theme object
 */
public static UserSettings getTheme(Long userId) {

    UserSettings theme = null;
    Connection con = null;
    try {
        con = DBUtils.getConn();

        PreparedStatement stmt = con.prepareStatement("select * from user_theme where user_id=?");
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            theme = new UserSettings();
            theme.setBg(rs.getString("bg"));
            theme.setFg(rs.getString("fg"));
            if (StringUtils.isNotEmpty(rs.getString("d1"))) {
                String[] colors = new String[16];
                colors[0] = rs.getString("d1");
                colors[1] = rs.getString("d2");
                colors[2] = rs.getString("d3");
                colors[3] = rs.getString("d4");
                colors[4] = rs.getString("d5");
                colors[5] = rs.getString("d6");
                colors[6] = rs.getString("d7");
                colors[7] = rs.getString("d8");
                colors[8] = rs.getString("b1");
                colors[9] = rs.getString("b2");
                colors[10] = rs.getString("b3");
                colors[11] = rs.getString("b4");
                colors[12] = rs.getString("b5");
                colors[13] = rs.getString("b6");
                colors[14] = rs.getString("b7");
                colors[15] = rs.getString("b8");
                theme.setColors(colors);
            }
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return theme;

}

From source file:com.buddycloud.friendfinder.HashUtils.java

public static String retrieveJid(String hash, ComponentDataSource dataSource) {
    PreparedStatement statement = null;
    try {//from   w w  w . ja  v  a 2 s. c  om
        statement = dataSource
                .prepareStatement("SELECT \"jid\" FROM \"contact-matches\" WHERE \"credential-hash\"=?", hash);
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            return resultSet.getString(1);
        }
        return null;
    } catch (SQLException e) {
        LOGGER.error("Could not retrieve jid from hash " + hash, e);
        throw new RuntimeException(e);
    } finally {
        ComponentDataSource.close(statement);
    }
}

From source file:fll.JudgeInformation.java

/**
 * Get all judges stored for this tournament.
 * /*  ww w  . ja va 2s . c om*/
 * @param connection the database
 * @param tournament tournament ID
 * @return the judges
 * @throws SQLException
 */
public static Collection<JudgeInformation> getJudges(final Connection connection, final int tournament)
        throws SQLException {
    Collection<JudgeInformation> judges = new LinkedList<JudgeInformation>();

    ResultSet rs = null;
    PreparedStatement stmt = null;
    try {
        stmt = connection.prepareStatement("SELECT id, category, station FROM Judges WHERE Tournament = ?");
        stmt.setInt(1, tournament);
        rs = stmt.executeQuery();
        while (rs.next()) {
            final String id = rs.getString(1);
            final String category = rs.getString(2);
            final String station = rs.getString(3);
            final JudgeInformation judge = new JudgeInformation(id, category, station);
            judges.add(judge);
        }
    } finally {
        SQLFunctions.close(rs);
        SQLFunctions.close(stmt);
    }

    return judges;
}

From source file:at.molindo.dbcopy.util.Utils.java

public static <T> T executePrepared(Connection c, String query, ResultSetHandler<T> handler, Object... params)
        throws SQLException {
    PreparedStatement stmt = c.prepareStatement(query);
    try {/*from   w ww.  j a v  a2  s.c  om*/
        for (int i = 0; i < params.length; i++) {
            stmt.setObject(i + 1, params[i]);
        }
        ResultSet rs = stmt.executeQuery();
        return handle(rs, handler);
    } finally {
        stmt.close();
    }
}

From source file:com.sql.DocketNotification.java

/**
 * Gather a list of notifications for items that were docketed
 * /*w ww . j  a  va  2 s.  c o  m*/
 * @return
 */
public static List<DocketNotificationModel> getQueuedNotifications() {
    List<DocketNotificationModel> list = new ArrayList();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "SELECT * FROM DocketNotifications";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            DocketNotificationModel item = new DocketNotificationModel();
            item.setId(rs.getInt("id"));
            item.setSection(rs.getString("Section"));
            item.setSendTo(rs.getString("sendTo"));
            item.setMessageSubject(rs.getString("emailSubject"));
            item.setMessageBody(rs.getString("emailBody"));
            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.firewallid.util.FISQL.java

public static String getFirstFieldInsertIfNotExist(Connection conn, String tableName, String field,
        Map<String, String> fields) throws SQLException {
    /* Query *///ww  w  .j a v  a  2 s .  c o  m
    String query = "SELECT " + field + " FROM " + tableName + " WHERE "
            + Joiner.on(" = ? AND ").join(fields.keySet()) + " = ?";

    /* Execute */
    PreparedStatement pst = conn.prepareStatement(query);
    int i = 1;
    for (String value : fields.values()) {
        pst.setString(i, value);
        i++;
    }
    ResultSet executeQuery = pst.executeQuery();
    if (executeQuery.next()) {
        return executeQuery.getString(field);
    }

    /* Row is not exists. Insert */
    query = "INSERT INTO " + tableName + " (" + Joiner.on(", ").join(fields.keySet()) + ") VALUES ("
            + StringUtils.repeat("?, ", fields.size() - 1) + "?)";
    pst = conn.prepareStatement(query);
    i = 1;
    for (String value : fields.values()) {
        pst.setString(i, value);
        i++;
    }
    if (pst.execute()) {
        return null;
    }
    return getFirstFieldInsertIfNotExist(conn, tableName, field, fields);
}