Example usage for java.sql PreparedStatement setInt

List of usage examples for java.sql PreparedStatement setInt

Introduction

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

Prototype

void setInt(int parameterIndex, int x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java int value.

Usage

From source file:com.oracle.products.ProductResource.java

@POST
@Consumes(MediaType.APPLICATION_JSON)/*www  .ja  v a 2 s  .  c  om*/
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> productMap = new LinkedHashMap<String, String>();
    String key = "";
    String val = "";

    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            key = parser.getString();
            break;
        case VALUE_STRING:
            val = parser.getString();
            productMap.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            productMap.put(key, val);
            break;
        default:
            break;
        }
    }
    if (conn == null) {
        return "Not connected";
    } else {
        String query = "INSERT INTO product (product_id,name,description,quantity) VALUES (?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, Integer.parseInt(productMap.get("product_id")));
        pstmt.setString(2, productMap.get("name"));
        pstmt.setString(3, productMap.get("description"));
        pstmt.setInt(4, Integer.parseInt(productMap.get("quantity")));
        pstmt.executeUpdate();
        return "row has been inserted into the database";
    }

}

From source file:dao.CarryonRecentAddQuery.java

/**
 * This method is not called by spring.//  w  w w.  j a v  a 2s  .co m
 * This method adds an entry into pblob
 * @param conn the connection
 * @param ownerid 
 * @param entryid 
 * @param category 
 * @param btitle 
 * @param mimeType 
 * @param bsize 
 * @param zoom 
 * @param caption 
 * @exception BaseDaoException
 */
public void run(Connection conn, String ownerid, String entryid, int category, String btitle, String mimeType,
        long bsize, int zoom, String caption) throws BaseDaoException {

    byte[] capBytes = { ' ' };
    if (!RegexStrUtil.isNull(caption)) {
        capBytes = caption.getBytes();
    }
    String stmt = "insert into pblob values(?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP(), ?, ?, ?, ?)";
    PreparedStatement s = null;
    try {
        s = conn.prepareStatement(stmt);
        s.setLong(1, 0);
        s.setLong(2, new Long(entryid));
        s.setLong(3, new Long(ownerid));
        s.setInt(4, new Integer(category));
        s.setString(5, mimeType);
        s.setString(6, btitle);
        s.setLong(7, bsize);
        s.setInt(8, new Integer(zoom));
        s.setInt(9, 0);
        s.setBytes(10, capBytes);
        s.executeUpdate();
    } catch (SQLException e) {
        logger.error("Error adding a blob.", e);
        throw new BaseDaoException("Error adding pblob " + stmt, e);
    }
}

From source file:dao.DiaryPhotoAddQuery.java

/**
* This method is used to add blobstreams for a user. <code>Userpage</code>
* @param blob - the blob stream//from  w w  w  .j  a v  a2 s. c  o  m
* @param category - the blob type (1 - photos, 2-music, 3-video 4 - documents, 5 - archives)
* @param mimeType - the mime type (image/jpeg, application/octet-stream)
* @param btitle - the title for this blob
* @param bsize - the size of the blob
* @param zoom - the zoom for the blob (used for displaying for image/jpeg)
* @param caption - caption
* @throws Dao Exception - when an error or exception occurs while inserting this blob in DB.
*
 **/
public void addBlob(byte[] blob, int category, String mimeType, String btitle, long bsize, int zoom,
        String caption) throws BaseDaoException {
    long dt = System.currentTimeMillis();
    Connection conn = null;
    String stmt = "insert into diaryphotos values(?, ?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement s = null;
    try {
        conn = dataSource.getConnection();
        s = conn.prepareStatement(stmt);
        s.setLong(1, 0);
        s.setBytes(2, blob);
        s.setInt(3, new Integer(category));
        s.setString(4, mimeType);
        s.setString(5, btitle);
        s.setLong(6, bsize);
        s.setInt(7, new Integer(zoom));
        s.setString(8, caption);
        s.executeUpdate();
    } catch (SQLException e) {
        logger.error("Error adding a blob in diaryphotos ", e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                throw new BaseDaoException("Could not close connection " + stmt, e);
            }
        }
    }
}

From source file:cn.vlabs.duckling.vwb.service.init.provider.DPageInitProvider.java

private DPage createDpage(final int siteId, final DPagePo dpage) {
    final String sql = "insert into vwb_dpage_content_info "
            + "(siteId,resourceid,version,change_time,content,change_by,title) " + " values(?,?,?,?,?,?,?)";
    KeyHolder keyHolder = new GeneratedKeyHolder();
    getJdbcTemplate().update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
            PreparedStatement ps = conn.prepareStatement(sql);
            int i = 0;
            ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setInt(++i, siteId);
            ps.setInt(++i, dpage.getResourceId());
            ps.setInt(++i, dpage.getVersion());
            ps.setTimestamp(++i, new Timestamp(dpage.getTime().getTime()));
            ps.setString(++i, dpage.getContent());
            ps.setString(++i, dpage.getCreator());
            ps.setString(++i, dpage.getTitle());
            return ps;
        }/* ww  w .ja va  2s.  c om*/
    }, keyHolder);
    dpage.setId(Integer.valueOf(keyHolder.getKey().intValue()));
    return DtoPoConvertor.convertPoToDto(dpage);
}

From source file:com.alibaba.otter.node.etl.load.loader.db.interceptor.operation.AbstractOperationInterceptor.java

private void init(final JdbcTemplate jdbcTemplate, final String markTableName, final String markTableColumn) {
    int count = jdbcTemplate
            .queryForInt(MessageFormat.format(checkDataSql, markTableName, GLOBAL_THREAD_COUNT - 1));
    if (count != GLOBAL_THREAD_COUNT) {
        if (logger.isInfoEnabled()) {
            logger.info("Interceptor: init " + markTableName + "'s data.");
        }//  w  w w . j  av a  2  s  .co  m
        TransactionTemplate transactionTemplate = new TransactionTemplate();
        transactionTemplate
                .setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);// ??????
        transactionTemplate.execute(new TransactionCallback() {

            public Object doInTransaction(TransactionStatus status) {
                jdbcTemplate.execute(MessageFormat.format(deleteDataSql, markTableName));
                String batchSql = MessageFormat.format(updateSql,
                        new Object[] { markTableName, markTableColumn });
                jdbcTemplate.batchUpdate(batchSql, new BatchPreparedStatementSetter() {

                    public void setValues(PreparedStatement ps, int idx) throws SQLException {
                        ps.setInt(1, idx);
                        ps.setInt(2, 0);
                        // ps.setNull(3, Types.VARCHAR);
                    }

                    public int getBatchSize() {
                        return GLOBAL_THREAD_COUNT;
                    }
                });
                return null;
            }
        });

        if (logger.isInfoEnabled()) {
            logger.info("Interceptor: Init EROSA Client Data: " + updateSql);
        }
    }

}

From source file:com.ccoe.build.tracking.jdbc.SessionJDBCTemplate.java

public int[] batchUpdateCategory(final List<Session> sessions) {
    int[] updateCounts = jdbcTemplateObject.batchUpdate(
            "update RBT_SESSION set category = ?,  filter = ? where id = ?",
            new BatchPreparedStatementSetter() {
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ps.setString(1, sessions.get(i).getCategory());
                    ps.setString(2, sessions.get(i).getFilter());
                    ps.setInt(3, sessions.get(i).getId());
                }/*w ww.ja  v a 2  s  .c o  m*/

                public int getBatchSize() {
                    return sessions.size();
                }
            });
    return updateCounts;
}

From source file:com.product.Product.java

@POST
@Path("/products")
@Consumes(MediaType.APPLICATION_JSON)//from  ww w  .ja  va2  s  .co  m
@Produces(MediaType.TEXT_PLAIN)
public String postProduct(String str) throws SQLException {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> prodMap = new LinkedHashMap<String, String>();
    String key = "";
    String val = "";

    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            key = parser.getString();
            break;
        case VALUE_STRING:
            val = parser.getString();
            prodMap.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            prodMap.put(key, val);
            break;
        default:
            break;
        }
    }
    if (connect == null) {
        return "Not connected";
    } else {
        String query = "INSERT INTO product (product_id,name,description,quantity) VALUES (?,?,?,?)";
        PreparedStatement prepstmnt = connect.prepareStatement(query);
        prepstmnt.setInt(1, Integer.parseInt(prodMap.get("product_id")));
        prepstmnt.setString(2, prodMap.get("name"));
        prepstmnt.setString(3, prodMap.get("description"));
        prepstmnt.setInt(4, Integer.parseInt(prodMap.get("quantity")));
        prepstmnt.executeUpdate();
        return "row has been inserted into the database";
    }

}

From source file:mx.com.pixup.portal.dao.DisqueraDaoJdbc.java

@Override
public void deleteDisquera(Disquera disquera) {
    Connection connection = DBConecta.getConnection();
    PreparedStatement preparedStatement = null;
    String sql = "delete from disquera  where id = ?";
    try {/*from   w w  w.ja  va 2 s. c  o m*/
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, disquera.getId());
        preparedStatement.execute();
    } catch (Exception e) {
        Logger.getLogger(DBConecta.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:oobbit.orm.Links.java

public List<Link> getAll(int limit) throws SQLException {
    PreparedStatement statement = getConnection().prepareStatement(
            "SELECT * FROM oobbit.links AS q1 LEFT JOIN users AS q2 ON q1.creator = q2.user_id LIMIT ?;");
    statement.setInt(1, limit);

    return parseResults(statement.executeQuery());
}

From source file:org.obiba.onyx.jade.instrument.gehealthcare.AchillesExpressInstrumentRunner.java

public void setAchillesExpressConfig() {

    achillesExpressDb.update("update Configuration set CompressPrompt = ?, BackupPrompt = ?, TargetDevice = ? ",
            new PreparedStatementSetter() {
                public void setValues(PreparedStatement ps) throws SQLException {
                    ps.setInt(1, 2);
                    ps.setInt(2, 2);//from  w  w  w .j  a v a2  s .c om
                    ps.setString(3, "Express");
                }
            });

}