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:Main.java

public static byte[] getBLOB(int id, Connection conn) throws Exception {
    ResultSet rs = null;/*ww  w  .j a  va 2 s .c o m*/
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {
        pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        Blob blob = rs.getBlob("photo");
        // materialize BLOB onto client
        return blob.getBytes(1, (int) blob.length());
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:com.khartec.waltz.jobs.InvolvementHarness.java

private static void viaJdbc(DataSource dataSource) {

    try (Connection conn = dataSource.getConnection();) {

        System.out.println("-- jdbc start");
        long start = System.currentTimeMillis();

        PreparedStatement pstmt = conn.prepareStatement(qry);
        ResultSet rs = pstmt.executeQuery();

        int c = 0;

        while (rs.next()) {
            c++;// ww w. jav a 2s. c  om
        }

        System.out.println(c);
        long duration = System.currentTimeMillis() - start;
        System.out.println("-- jdbc end " + duration);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

From source file:net.sf.sprockets.sql.Statements.java

/**
 * Execute the query, get the int value in the first row and column of the result set, and close
 * the statement./*from  ww w . j  ava  2s  . co m*/
 * 
 * @param stmt
 *            must already have parameters set
 * @return {@link Integer#MIN_VALUE} if the result set is empty
 */
public static int firstInt(PreparedStatement stmt) throws SQLException {
    ResultSet rs = stmt.executeQuery();
    int i = rs.next() ? rs.getInt(1) : Integer.MIN_VALUE;
    stmt.close();
    return i;
}

From source file:net.sf.sprockets.sql.Statements.java

/**
 * Execute the query, get the long value in the first row and column of the result set, and
 * close the statement.//from  ww  w  .ja v a 2 s  .  co  m
 * 
 * @param stmt
 *            must already have parameters set
 * @return {@link Long#MIN_VALUE} if the result set is empty
 */
public static long firstLong(PreparedStatement stmt) throws SQLException {
    ResultSet rs = stmt.executeQuery();
    long l = rs.next() ? rs.getLong(1) : Long.MIN_VALUE;
    stmt.close();
    return l;
}

From source file:Main.java

public static String getCLOB(int id) throws Exception {
    Connection conn = null;/*from   www  .  j a va  2 s .  c  o  m*/
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT clobData FROM tableName WHERE id = ?";

    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        Clob clob = rs.getClob(1);
        // materialize CLOB onto client
        String wholeClob = clob.getSubString(1, (int) clob.length());
        return wholeClob;
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:net.sf.sprockets.sql.Statements.java

/**
 * Execute the query, get the long values in the first row of the result set, and close the
 * statement.// w  ww  .  ja  va 2s .  c om
 * 
 * @param stmt
 *            must already have parameters set
 * @return null if the result set is empty
 * @since 1.4.0
 */
public static long[] firstLongRow(PreparedStatement stmt) throws SQLException {
    long[] l = null;
    ResultSet rs = stmt.executeQuery();
    if (rs.next()) {
        l = new long[rs.getMetaData().getColumnCount()];
        for (int i = 0; i < l.length; i++) {
            l[i] = rs.getLong(i + 1);
        }
    }
    stmt.close();
    return l;
}

From source file:net.sf.sprockets.sql.Statements.java

/**
 * Execute the query, get the long values in the first column of the result set, and close the
 * statement.//  w w  w  .  jav a 2s .  c  o  m
 * 
 * @param stmt
 *            must already have parameters set
 * @return null if the result set is empty
 * @since 1.4.0
 */
public static long[] allLongs(PreparedStatement stmt) throws SQLException {
    long[] l = null;
    ResultSet rs = stmt.executeQuery();
    if (rs.next()) {
        LongList list = new ArrayLongList();
        do {
            list.add(rs.getLong(1));
        } while (rs.next());
        l = list.toArray();
    }
    stmt.close();
    return l;
}

From source file:net.sf.sprockets.sql.Statements.java

/**
 * Execute the query, get the String values in the first column of the result set, and close the
 * statement./*from   www .j a  va  2s.com*/
 * 
 * @param stmt
 *            must already have parameters set
 * @return null if the result set is empty
 * @since 1.5.0
 */
public static List<String> allStrings(PreparedStatement stmt) throws SQLException {
    List<String> s = null;
    ResultSet rs = stmt.executeQuery();
    if (rs.next()) {
        s = new ArrayList<String>();
        do {
            s.add(rs.getString(1));
        } while (rs.next());
    }
    stmt.close();
    return s;
}

From source file:com.l2jfree.gameserver.instancemanager.RaidPointsManager.java

public static void init() {
    _list.clear();//from   w  w w.ja  v a2 s  .  com

    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement = con.prepareStatement("SELECT * FROM character_raid_points");
        ResultSet rset = statement.executeQuery();

        while (rset.next())
            getList(rset.getInt("charId")).put(rset.getInt("boss_id"), rset.getInt("points"));

        rset.close();
        statement.close();
    } catch (Exception e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.linkedin.databus.bootstrap.utils.BootstrapDBCleanerMain.java

private static List<String> getSourceNames(Connection conn) throws SQLException {
    List<String> srcNames = new ArrayList<String>();
    String sql = "select src from bootstrap_sources";
    ResultSet rs = null;//w ww .j  av a 2  s  .c om
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement(sql);
        rs = stmt.executeQuery();

        while (rs.next()) {
            String name = rs.getString(1);
            srcNames.add(name);
        }
    } finally {
        DBHelper.close(rs, stmt, null);
    }

    return srcNames;
}