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

public static long writeJavaObject(Connection conn, Object object) throws Exception {
    String className = object.getClass().getName();
    PreparedStatement pstmt = conn.prepareStatement(WRITE_OBJECT_SQL);

    // set input parameters
    pstmt.setString(1, className);
    pstmt.setObject(2, object);//from w w  w.  j ava  2s  .c o  m
    pstmt.executeUpdate();

    // get the generated key for the id
    ResultSet rs = pstmt.getGeneratedKeys();
    int id = -1;
    if (rs.next()) {
        id = rs.getInt(1);
    }

    rs.close();
    pstmt.close();
    System.out.println("writeJavaObject: done serializing: " + className);
    return id;
}

From source file:Main.java

/**
 * Sets the optional string.//from ww  w.  ja v  a2 s  .c  o m
 *
 * @param statement the statement
 * @param string the string
 * @param optionIndex the option index
 * @return the prepared statement
 * @throws SQLException the sQL exception
 */
static PreparedStatement setOptionalString(PreparedStatement statement, String string, int optionIndex)
        throws SQLException {
    if (string != null)
        statement.setString(optionIndex, string);
    else
        statement.setNull(optionIndex, Types.VARCHAR);

    return statement;
}

From source file:jp.co.opentone.bsol.linkbinder.CorresponBodyUpdater.java

static void execute(PreparedStatement stmt) throws Exception {
    String dir = "100506-Body???/??Body";

    stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6564LOG.txt")));
    stmt.setLong(2, 1093);// w  ww  .ja  va  2  s. co m
    stmt.executeUpdate();

    stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6565LOG.txt")));
    stmt.setLong(2, 1094);
    stmt.executeUpdate();

    stmt.setString(1, FileUtils.readFileToString(new File(dir, "ID6661LOG.txt")));
    stmt.setLong(2, 1095);
    stmt.executeUpdate();
}

From source file:wiki.doc.DocResource.java

public static void insertAll(final List<Doc> docs, DbConnector dbc) {
    BatchPreparedStatementSetter bpss = new BatchPreparedStatementSetter() {
        @Override//from   ww  w .  j a  va  2s . co  m
        public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
            preparedStatement.setString(1, docs.get(i).title);
            preparedStatement.setLong(2, docs.get(i).id);
        }

        @Override
        public int getBatchSize() {
            return docs.size();
        }
    };

    dbc.jdbcTemplate.batchUpdate("INSERT INTO pages(title, id) values(?, ?)", bpss);
}

From source file:Main.java

private static void saveAll(Connection conn, List<Person> people) throws SQLException {
    PreparedStatement prep = conn.prepareStatement("insert into people values (?, ?);");
    for (Person person : people) {
        prep.setString(1, person.getName());
        prep.setString(2, person.getOccupation());
        prep.addBatch();//from w w  w .  ja va 2 s  .  co m
    }
    conn.setAutoCommit(false);
    prep.executeBatch();
    conn.setAutoCommit(true);
    close(prep);
}

From source file:com.sapienter.jbilling.tools.ConvertToBinHexa.java

private static void updateUserRow(int id, String password) throws SQLException {
    PreparedStatement stmt = connection.prepareStatement("UPDATE base_user set password = ? where ID = ?");
    stmt.setString(1, password);
    stmt.setInt(2, id);/*from  www.ja v a2s.c  o  m*/
    stmt.executeUpdate();
}

From source file:edu.pitt.sis.infsci2730.finalProject.dao.AddressDao.java

/**
 * add new AddressDBModel/*from w ww . j  ava2s. com*/
 *
 * @param para
 * @return
 */
//    public static int addAddress(final String[] para) throws SQLException {
//        String sql = "insert into Address (city,street,state_,zipCode) values (?,?,?,?)";
//        return jdbcTemplate.update(sql,
//                para,
//                new int[]{java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR, java.sql.Types.VARCHAR});
//    }

public static Long addAddress(final String[] para) throws SQLException {

    KeyHolder holder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {

        String sql = "insert into Address (city,street,state_,zipCode) values (?,?,?,?)";

        @Override
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, para[0]);
            ps.setString(2, para[1]);
            ps.setString(3, para[2]);
            ps.setString(4, para[3]);
            return ps;
        }
    }, holder);

    Long newPersonId = holder.getKey().longValue();

    return newPersonId;
}

From source file:edu.pitt.sis.infsci2730.finalProject.dao.TransactionDao.java

public static TransactionDBModel InsertTransactionByID(final String[] para) throws SQLException {

    KeyHolder holder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() {

        String sql = "insert into Transactions (transaction_date, customer_id) "
                + "values (CURRENT_TIMESTAMP,?)";

        @Override//from   ww w  . j a va 2s. c  om
        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
            PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
            ps.setString(1, para[0]);

            return ps;
        }
    }, holder);

    int newId = holder.getKey().intValue();

    return jdbcTemplate.queryForObject("select * from Transactions where TRANSACTION_ID=" + newId,
            new TransactionRowMapper());
}

From source file:com.l2jfree.gameserver.datatables.PetNameTable.java

public static boolean doesPetNameExist(String name, int petNpcId) {
    boolean result = true;
    Connection con = null;// w w  w  .java 2s  . com

    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement(
                "SELECT name FROM pets p, items i WHERE p.item_obj_id = i.object_id AND name=? AND i.item_id=?");
        statement.setString(1, name);
        statement.setString(2, Integer.toString(PetDataTable.getItemIdByPetId(petNpcId)));
        ResultSet rset = statement.executeQuery();
        result = rset.next();
        rset.close();
        statement.close();
    } catch (SQLException e) {
        _log.warn("could not check existing petname:" + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }

    return result;
}

From source file:com.sapienter.jbilling.tools.ConvertToBinHexa.java

private static void updateCCRow(int id, String number, String name) throws SQLException {
    PreparedStatement stmt = connection
            .prepareStatement("UPDATE credit_card set cc_number = ?, name = ? where ID = ?");
    stmt.setString(1, number);
    stmt.setString(2, name);//www. ja  v a  2 s .c  om
    stmt.setInt(3, id);
    stmt.executeUpdate();
}