Code example for PreparedStatement

Methods: executeUpdate, setBytes, setString

0
    private static final Logger LOG = LoggerFactory.getLogger(DefaultJDBCAdapter.class); 
 
    protected Statements statements;
 
    protected void setBinaryData(PreparedStatement s, int index, byte data[]) throws SQLException {
        s.setBytes(index, data);
    } 
 
    protected byte[] getBinaryData(ResultSet rs, int index) throws SQLException {
        return rs.getBytes(index);
    } 
 
    public void doCreateTables(Connection connection) throws SQLException, IOException {
        Statement s = null;
        try { 
            // Check to see if the table already exists. If it does, then don't 
            // log warnings during startup. 
            // Need to run the scripts anyways since they may contain ALTER 
            // statements that upgrade a previous version of the table 
            boolean alreadyExists = false;
            ResultSet rs = null;
            try { 
                rs = connection.getMetaData().getTables(null, null, statements.getFullStoreTableName(),
                        new String[] {"TABLE" });
                alreadyExists = rs.next();
            } catch (Throwable ignore) {
                // Do nothing 
            } finally { 
                close(rs);
            } 
 
            // If the dataSource is a managed DataSource, executing a statement 
            // that throws 
            // an exception will make the connection unusable. 
            // So if the table already exists, do not try to re-create them 
            if (alreadyExists) {
                return; 
            } 
 
            s = connection.createStatement();
            String[] createStatments = statements.getCreateSchemaStatements();
            for (int i = 0; i < createStatments.length; i++) {
                // This will fail usually since the tables will be 
                // created already. 
                try { 
                    LOG.debug("Executing SQL: " + createStatments[i]);
                    s.execute(createStatments[i]);
                } catch (SQLException e) {
                    if (alreadyExists) {
                        LOG.debug("Could not create JDBC tables; The message table already existed." + " Failure was: " 
                                + createStatments[i] + " Message: " + e.getMessage() + " SQLState: " + e.getSQLState()
                                + " Vendor code: " + e.getErrorCode());
                    } else { 
                        LOG.warn("Could not create JDBC tables; they could already exist." + " Failure was: " 
                                + createStatments[i] + " Message: " + e.getMessage() + " SQLState: " + e.getSQLState()
                                + " Vendor code: " + e.getErrorCode());
                        JDBCAdapterFactory.log("Failure details: ", e);
                    } 
                } 
            } 
        } finally { 
            close(s);
        } 
    } 
 
    public void doDropTables(Connection connection) throws SQLException, IOException {
        Statement s = null;
        try { 
            s = connection.createStatement();
            String[] dropStatments = statements.getDropSchemaStatements();
            for (int i = 0; i < dropStatments.length; i++) {
                // This will fail usually since the tables will be 
                // created already. 
                try { 
                    s.execute(dropStatments[i]);
                } catch (SQLException e) {
                    LOG.warn("Could not drop JDBC tables; they may not exist." + " Failure was: " + dropStatments[i]
                            + " Message: " + e.getMessage() + " SQLState: " + e.getSQLState() + " Vendor code: "
                            + e.getErrorCode());
                    JDBCAdapterFactory.log("Failure details: ", e);
                } 
            } 
        } finally { 
            close(s);
        } 
    } 
 
    public void doStoreData(Connection connection, String id, byte[] data) throws SQLException, IOException {
        PreparedStatement s = null;
        try { 
            if (s == null) {
                s = connection.prepareStatement(statements.getStoreDataStatement());
            } 
            s.setString(1, id);
            setBinaryData(s, 2, data);
            if (s.executeUpdate() != 1) {
                throw new SQLException("Failed to insert data");
            } 
        } finally { 
            close(s);
        } 
    } 
 
    public byte[] doLoadData(Connection connection, String id) throws SQLException, IOException {
        PreparedStatement s = null;
        ResultSet rs = null;
        try { 
            s = connection.prepareStatement(statements.getFindDataStatement());
            s.setString(1, id);
            rs = s.executeQuery();
            if (!rs.next()) {
                return null; 
            } 
            return getBinaryData(rs, 1);
        } finally { 
            close(rs);
            close(s);
        } 
    } 
 
    public void doUpdateData(Connection connection, String id, byte[] data) throws SQLException, IOException {
        PreparedStatement s = null;
        try { 
            if (s == null) {
                s = connection.prepareStatement(statements.getUpdateDataStatement());
            } 
            s.setString(2, id);
            setBinaryData(s, 1, data);
            if (s.executeUpdate() != 1) {
                throw new SQLException("Failed to update data");
            } 
        } finally { 
            close(s);
        } 
    } 
 
    public void doRemoveData(Connection connection, String id) throws SQLException, IOException {
        PreparedStatement s = null;