Example usage for java.sql PreparedStatement executeUpdate

List of usage examples for java.sql PreparedStatement executeUpdate

Introduction

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

Prototype

int executeUpdate() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Usage

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 www  .j a  v a 2s .  com*/
        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);
    }
}

From source file:FacultyAdvisement.StudentRepository.java

public static void updatePassword(DataSource ds, String username, String password) throws SQLException {

    if (ds == null) {
        throw new SQLException("ds is null; Can't get data source");
    }//  www.j  ava  2  s.  c om

    Connection conn = ds.getConnection();

    if (conn == null) {
        throw new SQLException("conn is null; Can't get db connection");
    }

    String newPassword = SHA256Encrypt.encrypt(password);
    try {
        PreparedStatement ps = conn.prepareStatement("Update USERTABLE set PASSWORD=? where USERNAME=?");
        ps.setString(1, newPassword);
        ps.setString(2, username);

        ps.executeUpdate();

    } finally {
        conn.close();
    }
}

From source file:ca.qc.adinfo.rouge.leaderboard.db.LeaderboardDb.java

public static boolean createLeaderboard(DBManager dbManager, String key, String name) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;/* ww  w.  ja  v  a2 s . com*/
    String sql = null;

    sql = "INSERT INTO rouge_leaderboards (`key`, `name`) VALUES (?, ?);";

    try {
        connection = dbManager.getConnection();
        stmt = connection.prepareStatement(sql);

        stmt.setString(1, key);
        stmt.setString(2, name);

        int ret = stmt.executeUpdate();

        return (ret > 0);

    } catch (SQLException e) {
        log.error(stmt);
        log.error(e);
        return false;

    } finally {

        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(stmt);
        DbUtils.closeQuietly(connection);
    }
}

From source file:ch.newscron.registration.UserRegistration.java

public static void registerUserByShortURL(String name, String lastName, String email, long shortUrlId) {
    Connection connection = null;
    PreparedStatement query = null;
    ResultSet rs = null;/*from   w  ww. j  a  va 2  s .  c  o m*/
    try {
        connection = connect();
        query = connection
                .prepareStatement("INSERT INTO User (name, lastName, email, campaignId) VALUES (?, ?, ?, ?)");
        query.setString(1, name);
        query.setString(2, lastName);
        query.setString(3, email);
        query.setLong(4, shortUrlId);
        query.executeUpdate();
    } catch (Exception ex) {
        Logger.getLogger(UserRegistration.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        disconnect(connection, query, rs);
    }
}

From source file:las.DBConnector.java

public static void insertItemIntoTable(Item item) throws SQLException {
    String data = "INSERT INTO Items(title, author, type, amountleft)" + "Values (?,?,?,?)";
    PreparedStatement pt = conn.prepareStatement(data);
    pt.setString(1, item.getTitle());//from w w  w .j a  v  a  2  s  . c  o m
    pt.setString(2, item.getAuthor());
    pt.setString(3, item.getType());
    pt.setInt(4, item.getAmountLeft());
    pt.executeUpdate();
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.db.TopicItemDAOImplTest.java

@AfterClass
public static void cleanupDatabase() {
    final String unboundStr = "DELETE FROM ofPubsubItem where serviceID = ? AND nodeID = ?";
    Connection conn = null;/*from  w w w. ja v a 2s .c o  m*/
    PreparedStatement pstmt = null;
    try {
        conn = UnitTestDSProvider.getDataSource().getConnection();
        pstmt = conn.prepareStatement(unboundStr);
        pstmt.setString(1, SERVICE_ID);
        pstmt.setString(2, NODE_ID);
        pstmt.executeUpdate();
    } catch (SQLException e) {
        LOGGER.error("cleanupDatabase : caught exception cleaning ofPubsubItem");
    } finally {
        CloseUtil.close(LOGGER, pstmt, conn);
    }
}

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   w w  w  .  j  av a2s . co 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:net.codjo.dataprocess.server.treatmenthelper.TreatmentHelper.java

public static void initRepositoryUserAccess(Connection con, String userName,
        List<RepositoryDescriptor> repositoryDescList) throws SQLException {
    User user = new User();
    user.setUserName(userName);//from   w  ww.  j  a  v  a2  s  . co m
    for (RepositoryDescriptor repoDescriptor : repositoryDescList) {
        user.addRepository(new Repository(repoDescriptor.getRepositoryName()));
    }
    String xml = new UserXStreamImpl().toXml(user);
    PreparedStatement pStmt = con.prepareStatement("delete from PM_DP_USER where USER_NAME = ? "
            + " insert into PM_DP_USER (USER_NAME, USER_PARAM) values (?, ?)");
    try {
        pStmt.setString(1, userName);
        pStmt.setString(2, userName);
        pStmt.setString(3, xml);
        pStmt.executeUpdate();
        LOG.info(String.format("Droits d'accs accords  l'utilisateur '%s' pour tous les repositories.",
                userName));
    } finally {
        pStmt.close();
    }
}

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

public static void updateRaterRating(Connection con, RaterRatingPK pk, int note) throws SQLException {
    PreparedStatement prepStmt = con.prepareStatement(QUERY_UPDATE_RATER_RATING);
    try {/*from w  w  w.j  a  va 2 s.c  o m*/
        prepStmt.setInt(1, note);
        prepStmt.setString(2, pk.getInstanceId());
        prepStmt.setString(3, pk.getContributionId());
        prepStmt.setString(4, pk.getContributionType());
        prepStmt.setString(5, pk.getRater().getId());
        prepStmt.executeUpdate();
    } finally {
        DBUtil.close(prepStmt);
    }
}

From source file:dsd.dao.DAOProvider.java

/**
 * the insert row function done in a secure way
 * //from   w ww  . ja v a2s  .  co m
 * @param table
 * @param fields
 * @param con
 * @param valueArray
 * @return
 * @throws SQLException
 */
public static int InsertRowSecure(String table, String fields, Connection con, Object[] valueArray)
        throws SQLException {
    try {
        String values = "";
        if (valueArray.length > 0) {
            values = "?";
        }
        for (int i = 1; i < valueArray.length; i++) {
            values += ",?";
        }

        PreparedStatement command = con
                .prepareStatement(String.format("insert into %s (%s) values (%s)", table, fields, values));

        for (int i = 0; i < valueArray.length; i++) {
            SetParameter(command, valueArray[i], i + 1);
        }

        command.executeUpdate();

        command = con.prepareStatement(String.format("select Max(ID) from %s", table));
        ResultSet rs = command.executeQuery();
        rs.next();

        return rs.getInt(1);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return 0;
}