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:gridool.util.jdbc.JDBCUtils.java

/**
 * Execute an SQL INSERT, UPDATE, or DELETE query.
 * /*w  w  w.  j  av a  2 s.  c  om*/
 * @param conn The connection to use to run the query.
 * @param sql The SQL to execute.
 * @param params The query replacement parameters.
 * @return The number of rows updated.
 */
public static int update(Connection conn, String sql, Object[] params) throws SQLException {
    PreparedStatement stmt = null;
    int rows = 0;
    try {
        stmt = conn.prepareStatement(sql);
        fillStatement(stmt, params);
        verboseQuery(sql, params);
        rows = stmt.executeUpdate();
    } catch (SQLException e) {
        rethrow(e, sql, params);
    } finally {
        close(stmt);
    }
    return rows;
}

From source file:ca.qc.adinfo.rouge.achievement.db.AchievementDb.java

public static boolean createAchievement(DBManager dbManager, String key, String name, int pointValue,
        double total) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;// w w w  .  j  a  v a2  s.c  o m
    String sql = null;

    sql = "INSERT INTO rouge_achievements (`key`, `name`, `point_value`, `total`) " + " VALUES (?, ?, ?, ?);";

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

        stmt.setString(1, key);
        stmt.setString(2, name);
        stmt.setInt(3, pointValue);
        stmt.setDouble(4, total);

        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:com.trackplus.ddl.DataWriter.java

private static void insertBlobData(Connection con, String line) throws DDLException {
    String sql = "INSERT INTO TBLOB(OBJECTID, BLOBVALUE, TPUUID) VALUES(?,?,?)";
    StringTokenizer st = new StringTokenizer(line, ",");
    Integer objectID = Integer.valueOf(st.nextToken());
    String base64Str = st.nextToken();
    byte[] bytes = Base64.decodeBase64(base64Str);
    String tpuid = null;//from   ww  w .j  a v  a 2 s  . com
    if (st.hasMoreTokens()) {
        tpuid = st.nextToken();
    }

    try {
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, objectID);
        preparedStatement.setBinaryStream(2, new ByteArrayInputStream(bytes), bytes.length);
        preparedStatement.setString(3, tpuid);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        throw new DDLException(e.getMessage(), e);
    }
}

From source file:ca.qc.adinfo.rouge.social.db.SocialDb.java

public static boolean deleteFriend(DBManager dbManager, long userId, long friendId) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;//from  w w w  .  j  av a2s  .  c o m
    String sql = null;

    sql = "DELETE FROM rouge_social_friends " + "WHERE (`user_id` = ? AND `friend_user_id` = ?) "
            + "OR (`user_id` = ? AND `friend_user_id` = ?)";

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

        stmt.setLong(1, userId);
        stmt.setLong(2, friendId);
        stmt.setLong(3, friendId);
        stmt.setLong(4, userId);

        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:com.useekm.indexing.postgis.IndexedStatement.java

/**
 * Creates and stores an indexed version of the provided statement. Only use
 * this function if it is safe to assume that the statement is not yet in
 * the database. This method is faster than
 * {@link #addIfNew(Connection, Resource, URI, Value, String)}, but does not
 * check for duplicates./* ww w .  ja  va 2  s  .  c o m*/
 * 
 * @param subject
 *            The subject, BNode subjects are not allowed.
 * @param predicate
 *            The predicate
 * @param object
 *            The object/value
 * 
 * @return The IndexedStatement to add to the index.
 * 
 * @throws IllegalArgumentException
 *             If one of the arguments equals null or if one of the
 *             arguments is a {@link BNode}.
 * @throws IndexException
 * @Throws SQLException
 */
static void add(Connection conn, String table, IndexedStatement indexedStatement) throws SQLException {
    PreparedStatement stat = addPrepare(conn, table);
    try {
        addNewBatch(stat, indexedStatement);
        int count = stat.executeUpdate();
        Validate.isTrue(count == 1);
    } finally {
        stat.close();
    }
}

From source file:dsd.dao.DAOProvider.java

public static int InsertRowsSecure(String table, String fields, Connection con, Object[][] valueArray)
        throws SQLException {
    try {/*from  ww w  .  j a  v a 2s .  c  om*/
        String values = "(";
        if (valueArray[0].length > 0) {
            values += "?";
        }
        for (int i = 1; i < valueArray[0].length; i++) {
            values += ",?";
        }
        values += ")";
        String rows = "";
        for (int j = 0; j < valueArray.length; j++) {
            rows += " " + values;
            if (j != valueArray.length - 1)
                rows += " , ";
        }

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

        for (int i = 0; i < valueArray.length; i++) {
            for (int j = 0; j < valueArray[i].length; j++) {
                SetParameter(command, valueArray[i][j], i * valueArray[i].length + j + 1);
            }
        }
        command.executeUpdate();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return 0;
}

From source file:com.useekm.indexing.postgis.IndexedStatement.java

/**
 * Deletes matching indexed statements.//from  w  w  w .  j  a  v a 2  s  . c o  m
 * 
 * @param subject
 *            The subject, BNode subjects are ignored.
 * @param predicate
 *            The predicate
 * @param object
 *            The object/value
 * 
 * @return The number of matching (hence deleted) statements.
 * 
 * @throws IndexException
 * @Throws SQLException
 */
static int delete(Connection conn, String table, Resource subject, URI predicate, Value object)
        throws SQLException {
    if (subject instanceof BNode || object instanceof BNode)
        return 0; // // BNodes are not indexed, so there is nothing to
                  // delete
    String sql = createQuery(DELETE_FROM_STS + table, subject, predicate, object);
    PreparedStatement stat = conn.prepareStatement(sql);
    try {
        addBindings(stat, subject, predicate, object);
        return stat.executeUpdate();
    } finally {
        stat.close();
    }
}

From source file:edu.lafayette.metadb.model.userman.UserManDAO.java

/**
 * Delete a user/* w w  w  .j a  v a  2  s . c  o  m*/
 * 
 * @param userName The username of the user to delete.
 * @return true if the user was deleted successfully, false otherwise
 */
public static boolean deleteUser(String userName) {
    Connection conn = Conn.initialize(); // Establish connection
    if (conn != null) {
        try {
            PreparedStatement deleteUser = conn.prepareStatement(DELETE_USER);

            deleteUser.setString(1, userName); // Set parameters
            deleteUser.executeUpdate();

            deleteUser.close();
            conn.close(); // Close statement and connection

            return true;
        }

        catch (Exception e) {
            MetaDbHelper.logEvent(e);
        }
    }
    return false;
}

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

public static boolean submitScore(DBManager dbManager, String key, long userId, long score) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;//from   w w w.j  a v a2s .  c  om
    String sql = null;

    sql = "INSERT INTO rouge_leaderboard_score (`leaderboard_key`, `user_id`, `score`) "
            + "VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE score = GREATEST(?, score);";

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

        stmt.setString(1, key);
        stmt.setLong(2, userId);
        stmt.setLong(3, score);
        stmt.setLong(4, score);

        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:com.sql.SECExceptions.java

/**
 * Inserts an exception to the database//from ww  w  . j av  a2s.  c om
 *
 * @param item SECExceptionsModel
 * @return boolean
 */
public static boolean insertException(SECExceptionsModel item) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "INSERT INTO SECExceptions (" + "className, " + "methodName, " + "exceptionType, "
                + "exceptionDescrption, " + "timeOccurred " + ") VALUES (" + "?, " + "?, " + "?, " + "?, "
                + "GETDATE())";
        ps = conn.prepareStatement(sql);
        ps.setString(1, item.getClassName());
        ps.setString(2, item.getMethodName());
        ps.setString(3, item.getExceptionType());
        ps.setString(4, item.getExceptionDescription());
        ps.executeUpdate();
    } catch (SQLException ex) {
        System.out.println(ex.toString());
        return true;
    } finally {
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(conn);
    }
    return false;
}