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(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which returns a single ResultSet object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (Id int, b CLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("c:/Java_Dev/data.txt");

    FileReader reader = new FileReader(file);
    pstmt.setCharacterStream(1, reader);

    pstmt.execute();/*from  ww  w .ja  va  2  s.  c o  m*/

    ResultSet resultSet = pstmt.executeQuery("select b from survey ");

    File data = new File("C:\\a.txt");
    Reader dataReader = resultSet.getCharacterStream(1);
    FileWriter writer = new FileWriter(data);
    char[] buffer = new char[1];
    while (dataReader.read(buffer) > 0) {
        writer.write(buffer);
    }
    writer.close();

    reader.close();
    st.close();
    conn.close();
}

From source file:com.uit.anonymousidentity.Repository.Nonces.NonceJDBCTemplate.java

public Nonce getNonceById(Integer id) throws SQLException {
    String sql = "select * from " + TABLE_NAME + " where " + ID + " = " + id;
    PreparedStatement pst = dataSource.getConnection().prepareStatement(sql);
    ResultSet rs = pst.executeQuery(sql);
    if (rs.next()) {
        //data valid
        Integer nonceID = rs.getInt(ID);
        String nonceSID = rs.getString(SID);
        byte[] nonceBytes = rs.getBytes(VALUE);
        Nonce nonce = new Nonce();
        nonce.setId(nonceID);/* w w w . j a v  a 2s.  c om*/
        nonce.setIssuerSid(nonceSID);
        nonce.setByteArray(nonceBytes);
        return nonce;
    } else {
        return null;
    }

}

From source file:com.uit.anonymousidentity.Repository.Nonces.NonceJDBCTemplate.java

public Set<Nonce> getNoncesBySID(String sid) throws SQLException {
    String sql = "select * from " + TABLE_NAME + " where " + SID + " = " + "'" + sid + "'";
    PreparedStatement pst = dataSource.getConnection().prepareStatement(sql);
    ResultSet rs = pst.executeQuery(sql);
    if (rs.next()) {
        //data valid
        Integer nonceID;//from  w  w  w .  j  av a  2  s  .  c  o m
        String nonceSID;
        byte[] nonceBytes;
        Set<Nonce> set = new HashSet<>();
        do {
            nonceID = rs.getInt(ID);
            nonceSID = rs.getString(SID);
            nonceBytes = rs.getBytes(VALUE);
            Nonce nonce = new Nonce();
            nonce.setId(nonceID);
            nonce.setIssuerSid(nonceSID);
            nonce.setByteArray(nonceBytes);
            set.add(nonce);
        } while (rs.next());
        return set;
    } else {
        return null;
    }
}