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:com.example.mydtapp.JdbcInputAppTest.java

public static void insertEventsInTable(int numEvents, int offset) {
    try {/*from  w ww. jav a 2 s.  c  o m*/
        Connection con = DriverManager.getConnection(URL);
        String insert = "insert into " + TABLE_NAME + " values (?,?,?)";
        PreparedStatement stmt = con.prepareStatement(insert);
        for (int i = 0; i < numEvents; i++, offset++) {
            stmt.setInt(1, offset);
            stmt.setString(2, "Account_Holder-" + offset);
            stmt.setInt(3, (offset * 1000));
            stmt.executeUpdate();
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.github.chrisbotcom.reminder.commands.Update.java

public static boolean execute(Reminder plugin, CommandSender sender, ReminderRecord reminder) throws Exception {
    if ((reminder.getId() == null) && (reminder.getPlayer() == null) && (reminder.getTag() == null)) {
        throw new ReminderException("Must supply either id or player and/or tag.");
    }/*from  ww w  .  j  ava2  s .c o  m*/

    if ((reminder.getId() != null) && ((reminder.getPlayer() != null) && (reminder.getTag() != null))) {
        throw new ReminderException("Must supply either id or player and/or tag.");
    }

    List<String> setClauses = new ArrayList<>();

    // "rate", "echo", 
    // TODO Consider making <player> and <tag> updatable.
    if (reminder.getId() != null) {
        if (reminder.getPlayer() != null) {
            setClauses.add(String.format("player = '%s'", reminder.getPlayer()));
        }
        if (reminder.getTag() != null) {
            setClauses.add(String.format("tag = '%s'", reminder.getTag()));
        }
    }

    if (reminder.getStart() != null) {
        setClauses.add(String.format("start = %s", reminder.getStart()));
    }

    if (reminder.getMessage() != null) {
        setClauses.add(String.format("message = '%s'", reminder.getMessage()));
    }

    if (reminder.getDelay() != null) {
        setClauses.add(String.format("delay = %s", reminder.getDelay()));
    }

    if (reminder.getRate() != null) {
        setClauses.add(String.format("rate = %s", reminder.getRate()));
    }

    if (reminder.getEcho() != null) {
        setClauses.add(String.format("echo = %s", reminder.getEcho()));
    }

    String sql = String.format(
            "UPDATE reminders SET %s WHERE (? IN(id, 0)) AND (? IN(player, 'EMPTY')) AND (? IN(tag, 'EMPTY'))",
            StringUtils.join(setClauses, ", "));
    PreparedStatement preparedStatement = plugin.db.prepareStatement(sql);
    preparedStatement.setLong(1, reminder.getId() == null ? 0 : reminder.getId());
    preparedStatement.setString(2,
            reminder.getId() != null || reminder.getPlayer() == null ? "EMPTY" : reminder.getPlayer());
    preparedStatement.setString(3,
            reminder.getId() != null || reminder.getTag() == null ? "EMPTY" : reminder.getTag());

    int rows = preparedStatement.executeUpdate();

    //sender.sendMessage(ChatColor.BLUE + String.format("%s", preparedStatement));
    sender.sendMessage(ChatColor.GREEN + String.format("%s record(s) updated.", rows));

    return true;
}

From source file:ch.newscron.encryption.ReferralManagerJUnitTest.java

@AfterClass
public static void tearDownClass() throws SQLException {
    // Delete inserted query
    con = ReferralManager.connect();//from w  w w .  j a  v  a2s  . co  m
    PreparedStatement query = null;
    query = con.prepareStatement("DELETE FROM ShortURL WHERE custId= ? AND shortUrl = ?");
    query.setLong(1, Long.parseLong((String) inviteData.get("custID")));
    query.setString(2, shortURL);
    query.execute();
    ReferralManager.disconnect(con, null);
}

From source file:com.splicemachine.derby.test.framework.SpliceRoleWatcher.java

public static void executeDrop(String roleName) {
    LOG.trace("ExecuteDrop");
    Connection connection = null;
    PreparedStatement statement = null;
    try {/*  w  w w.ja  va  2 s. c om*/
        connection = SpliceNetConnection.getConnection();
        statement = connection.prepareStatement("select roleid from sys.sysroles where roleid = ?");
        statement.setString(1, roleName);
        ResultSet rs = statement.executeQuery();
        if (rs.next())
            connection.createStatement().execute(String.format("drop role %s", roleName));
        connection.commit();
    } catch (Exception e) {
        LOG.error("error Dropping " + e.getMessage());
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        DbUtils.closeQuietly(statement);
        DbUtils.commitAndCloseQuietly(connection);
    }
}

From source file:com.silverpeas.notation.model.RatingDAO.java

public static boolean existRaterRating(Connection con, RaterRatingPK pk) throws SQLException {
    PreparedStatement prepStmt = con.prepareStatement(QUERY_EXISTS_RATER_RATING);
    prepStmt.setString(1, pk.getInstanceId());
    prepStmt.setString(2, pk.getContributionId());
    prepStmt.setString(3, pk.getContributionType());
    prepStmt.setString(4, pk.getRater().getId());
    ResultSet rs = null;//from ww w .j  a  v  a 2s.  c  o m

    try {
        rs = prepStmt.executeQuery();
        return (rs.next());
    } finally {
        DBUtil.close(rs, prepStmt);
    }
}

From source file:net.big_oh.common.jdbc.JdbcProxyExerciser.java

private static void exercisePreparedSelect(Connection con) throws SQLException {
    logger.info(StringUtils.center("exercise prepared select", 100, "-"));

    PreparedStatement preparedStmnt = null;
    ResultSet rs = null;/*from w w w.ja  v a  2 s.c om*/
    try {
        preparedStmnt = con.prepareStatement("SELECT * FROM TEST_TABLE WHERE TEST_COLUMN = ?");
        preparedStmnt.setString(1, "value1");
        rs = preparedStmnt.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getString("TEST_COLUMN"));
        }
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(preparedStmnt);
    }
}

From source file:com.silverpeas.notation.model.RatingDAO.java

public static long deleteRatings(Connection con, ContributionRatingPK pk) throws SQLException {
    PreparedStatement prepStmt = con.prepareStatement(QUERY_DELETE_RATING);
    try {// ww  w .  ja  va 2  s. com
        prepStmt.setString(1, pk.getInstanceId());
        prepStmt.setString(2, pk.getContributionId());
        prepStmt.setString(3, pk.getContributionType());
        return prepStmt.executeUpdate();
    } finally {
        DBUtil.close(prepStmt);
    }
}

From source file:com.silverpeas.notation.model.RatingDAO.java

public static long moveRatings(Connection con, ContributionRatingPK pk, final String componentInstanceId)
        throws SQLException {
    PreparedStatement prepStmt = con.prepareStatement(QUERY_MOVE_RATINGS);
    try {/*from  w  w  w .  ja va2 s. c o  m*/
        prepStmt.setString(1, componentInstanceId);
        prepStmt.setString(2, pk.getInstanceId());
        prepStmt.setString(3, pk.getContributionId());
        prepStmt.setString(4, pk.getContributionType());
        return prepStmt.executeUpdate();
    } finally {
        DBUtil.close(prepStmt);
    }
}

From source file:edu.jhu.pha.vospace.jobs.JobsProcessor.java

/**
 * Returns the JobDescription object serialized from the database record
 * @param jobId The identifier of a job/*from   w w w . j av  a2 s . co m*/
 * @return The job java object
 */
public static JobDescription getJob(final UUID jobId) {
    return DbPoolServlet.goSql("GetJob request", "select json_notation, note from jobs where id = ?",
            new SqlWorker<JobDescription>() {
                @Override
                public JobDescription go(Connection conn, PreparedStatement stmt) throws SQLException {
                    JobDescription returnJob = null;
                    stmt.setString(1, jobId.toString());
                    ResultSet rs = stmt.executeQuery();
                    if (rs.next()) {
                        byte[] jobJsonNotation = rs.getBytes(1);
                        try {
                            returnJob = (new ObjectMapper()).readValue(jobJsonNotation, 0,
                                    jobJsonNotation.length, JobDescription.class);
                            returnJob.setNote(rs.getString("note"));
                        } catch (JsonMappingException ex) { // Shouldn't happen
                            throw new InternalServerErrorException(ex.getMessage());
                        } catch (JsonParseException ex) {
                            throw new InternalServerErrorException(ex.getMessage());
                        } catch (IOException ex) {
                            throw new InternalServerErrorException(ex.getMessage());
                        }
                    }
                    return returnJob;
                }
            });
}

From source file:com.silverpeas.notation.model.RatingDAO.java

public static long deleteRaterRating(Connection con, RaterRatingPK pk) throws SQLException {
    PreparedStatement prepStmt = con.prepareStatement(QUERY_DELETE_RATER_RATING);
    try {/*from   ww w  .j a v  a  2 s .c o  m*/
        prepStmt.setString(1, pk.getInstanceId());
        prepStmt.setString(2, pk.getContributionId());
        prepStmt.setString(3, pk.getContributionType());
        prepStmt.setString(4, pk.getRater().getId());
        return prepStmt.executeUpdate();
    } finally {
        DBUtil.close(prepStmt);
    }
}