Example usage for java.sql PreparedStatement setString

List of usage examples for java.sql PreparedStatement setString

Introduction

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

Prototype

void setString(int parameterIndex, String x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java String value.

Usage

From source file:net.freechoice.model.orm.Map_Tag.java

@Override
public PreparedStatementCreator createInsert(final FC_Tag tag) {

    return new PreparedStatementCreator() {

        @Override/*w  ww  .  j  a  v  a2 s  . co  m*/
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement ps = con.prepareStatement("insert into FC_Tag(content)" + " values(?)", RET_ID);
            ps.setString(1, tag.content);
            return ps;
        }
    };
}

From source file:com.ewcms.component.online.dao.AdvisorDAO.java

public int add(final Advisor vo) {
    final String sql = "Insert Into plugin_online_advisory "
            + "(username,name,title,content,organ_id,matter_id,ip) Values " + "(?,?,?,?,?,?,?)";

    jdbcTemplate.update(new PreparedStatementCreator() {

        @Override/*from w ww  .  j a  v  a 2s. c om*/
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, vo.getUsername());
            ps.setString(2, vo.getName());
            ps.setString(3, vo.getTitle());
            ps.setString(4, vo.getContent());
            ps.setInt(5, vo.getOrganId());
            ps.setInt(6, vo.getMatterId());
            ps.setString(7, vo.getIp());

            return ps;
        }
    });
    return jdbcTemplate.queryForInt("select currval('seq_plugin_online_advisory_id')");
}

From source file:com.jernejerin.traffic.helper.TripOperations.java

/**
 * Insert a trip into database.//w  w  w  . ja v  a2 s  . c om
 *
 * @param trip trip to insert.
 * @param table table into which we need to insert trip
 */
public static void insertTrip(Trip trip, String table) {
    //        LOGGER.log(Level.INFO, "Started inserting trip into DB for trip = " +
    //                trip.toString() + " from thread = " + Thread.currentThread());
    PreparedStatement insertTrip = null;
    Connection conn = null;
    try {
        // first we need to get connection from connection pool
        conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:taxi");

        // setting up prepared statement
        insertTrip = conn.prepareStatement("insert into " + table
                + " (eventId, medallion, hack_license, pickup_datetime, "
                + "dropoff_datetime, trip_time, trip_distance, pickup_longitude, pickup_latitude, dropoff_longitude, "
                + "dropoff_latitude, payment_type, fare_amount, surcharge, mta_tax, tip_amount, tolls_amount, "
                + "total_amount, timestampReceived) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

        insertTrip.setInt(1, trip.getId());
        insertTrip.setString(2, trip.getMedallion());
        insertTrip.setString(3, trip.getHackLicense());
        insertTrip.setTimestamp(4,
                new Timestamp(trip.getPickupDatetime().toEpochSecond(ZoneOffset.UTC) * 1000));
        insertTrip.setTimestamp(5,
                new Timestamp(trip.getDropOffDatetime().toEpochSecond(ZoneOffset.UTC) * 1000));
        insertTrip.setInt(6, trip.getTripTime());
        insertTrip.setDouble(7, trip.getTripDistance());
        insertTrip.setDouble(8, trip.getPickupLongitude());
        insertTrip.setDouble(9, trip.getPickupLatitude());
        insertTrip.setDouble(10, trip.getDropOffLongitude());
        insertTrip.setDouble(11, trip.getDropOffLatitude());
        insertTrip.setString(12, trip.getPaymentType() != null ? trip.getPaymentType().name() : null);
        insertTrip.setDouble(13, trip.getFareAmount());
        insertTrip.setDouble(14, trip.getSurcharge());
        insertTrip.setDouble(15, trip.getMtaTax());
        insertTrip.setDouble(16, trip.getTipAmount());
        insertTrip.setDouble(17, trip.getTollsAmount());
        insertTrip.setDouble(18, trip.getTotalAmount());
        insertTrip.setLong(19, trip.getTimestampReceived());

        insertTrip.execute();
    } catch (SQLException e) {
        LOGGER.log(Level.SEVERE, "Problem when inserting ticket into DB for ticket = " + trip
                + " from thread = " + Thread.currentThread());
    } finally {
        try {
            if (insertTrip != null)
                insertTrip.close();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Problem with closing prepared statement for ticket = " + trip
                    + " from thread = " + Thread.currentThread());
        }
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "Problem with closing connection from thread = " + Thread.currentThread());
        }
        //            LOGGER.log(Level.INFO, "Finished inserting ticket into DB for for ticket = " +
        //                    trip + " from thread = " + Thread.currentThread());
    }
}

From source file:h2backup.H2BackupTestBase.java

protected void insertRecordIntoTestTable(String description) throws SQLException {
    try (Connection conn = getConnection()) {
        PreparedStatement ps = conn.prepareStatement("INSERT INTO test_tbl (description) VALUES (?)");
        ps.setString(1, description);
        ps.execute();//from   w  ww.j  a v a2s . c  om
    }
}

From source file:com.example.spring.jdbc.template.CustomerDao.java

public Boolean saveCustomereByPreparedStatement(final Customer e) {
    String query = "insert into customer values(?,?,?)";
    return jdbcTemplate.execute(query, new PreparedStatementCallback<Boolean>() {
        @Override//from  www .  ja v  a 2s.co m
        public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {

            ps.setString(1, e.getCutomerId());
            ps.setString(2, e.getName());
            ps.setString(3, e.getEmail());

            return ps.execute();

        }
    });

}

From source file:at.alladin.rmbt.db.dao.TestStatDao.java

@Override
public int update(TestStat entity) throws SQLException {
    final String sql = "UPDATE test_stat SET cpu_usage = ?::json, mem_usage = ?::json, WHERE test_uid = ?";
    final PreparedStatement ps = conn.prepareStatement(sql);
    ps.setString(1, entity.getCpuUsage().toString());
    ps.setString(2, entity.getMemUsage().toString());
    ps.setLong(3, entity.getTestUid());// www  . j  a v  a2  s .  c  o m
    return ps.executeUpdate();
}

From source file:org.perconsys.dao.implement.CommentDb.java

@Override
public Comment create(final Comment comm) {
    KeyHolder keyh = new GeneratedKeyHolder();
    final Long target = comm.getTargetId() == 0 ? null : comm.getTargetId();
    final String sql = "insert into blogs (`title`, `content`, `post_id`, `user_id`, `target_id`, `date`) "
            + "value (?, ?, ?, ?, ?, now()) ;";

    getJdbcTemplate().update(new PreparedStatementCreator() {

        @Override/*from w ww. java 2  s  .c  om*/
        public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
            PreparedStatement pstat = conn.prepareStatement(sql, new String[] { "id" });
            pstat.setString(1, comm.getTitle());
            pstat.setString(2, comm.getContent());
            pstat.setLong(3, comm.getPostId());
            pstat.setLong(4, comm.getUser().getId());
            pstat.setLong(5, target);
            return pstat;
        }
    }, keyh);
    Long id = (Long) keyh.getKey();
    comm.setId(id);
    return comm;
}

From source file:com.pactera.edg.am.metamanager.extractor.dao.helper.DeleteDependencyHelper.java

private void doInPreparedStatement(long sysTime, MMDDependency dependency, PreparedStatement ps)
        throws SQLException {
    // ?ID/*from   ww w  .  j a v a 2  s.  c  o m*/
    ps.setString(1, dependency.getOwnerMetadata().getId());
    // ?ID
    ps.setString(2, dependency.getValueMetadata().getId());

    ps.addBatch();
    ps.clearParameters();

    if (++super.count % super.batchSize == 0) {
        ps.executeBatch();
        ps.clearBatch();
    }
}

From source file:org.apache.lucene.store.jdbc.index.AbstractJdbcIndexOutput.java

public void close() throws IOException {
    super.close();
    final long length = length();
    doBeforeClose();//from   www  . jav  a  2s. c  o m
    jdbcDirectory.getJdbcTemplate().update(jdbcDirectory.getTable().sqlInsert(), new PreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            InputStream is = null;
            try {
                is = openInputStream();
                if (jdbcDirectory.getDialect().useInputStreamToInsertBlob()) {
                    ps.setBinaryStream(2, is, (int) length());
                } else {
                    ps.setBlob(2, new InputStreamBlob(is, length));
                }
                ps.setLong(3, length);
                ps.setBoolean(4, false);
            } catch (IOException e) {
                throw new SQLException(e);
            }
        }
    });
    doAfterClose();
}

From source file:edu.emory.cci.aiw.i2b2etl.ksb.QueryExecutor.java

public <E extends Object> E execute(final String bindArgument, ResultSetReader<E> resultSetReader)
        throws KnowledgeSourceReadException {
    return execute((PreparedStatement stmt, int j) -> {
        stmt.setString(j++, bindArgument);
        return j;
    }, resultSetReader);/*  w  w w.j a  v  a  2s .c  o m*/
}