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.predic8.membrane.core.interceptor.statistics.StatisticsJDBCInterceptor.java

private void saveExchange(Connection con, Exchange exc) throws Exception {
    if (ignoreGetMethod(exc))
        return;/*from   ww  w .ja  v  a 2s  .c  o m*/
    if (ignoreNotSoap(exc))
        return;
    PreparedStatement stat = con.prepareStatement(statString);
    JDBCUtil.setData(exc, stat, idGenerated);
    stat.executeUpdate();
}

From source file:dk.netarkivet.common.utils.DBUtils.java

/** Update a database by executing all the statements in
 *  the updates String array./*  w w  w . j  a  v  a 2  s .c o m*/
 *  NOTE: this must NOT be used for tables under version control
 *  It must only be used in connection with temporary tables e.g. used
 *  for backup.
 *
 *  NB: the method does not close the provided connection.
 *
 * @param connection connection to the database.
 * @param updates The SQL statements that makes the necessary
 * updates.
 * @throws IOFailure in case of problems in interacting with the database
 */
public static void executeSQL(Connection connection, final String... updates) {
    ArgumentNotValid.checkNotNull(updates, "String... updates");
    PreparedStatement st = null;
    String s = "";

    try {
        connection.setAutoCommit(false);
        for (String update : updates) {
            s = update;
            log.debug("Executing SQL-statement: " + update);
            st = prepareStatement(connection, update);
            st.executeUpdate();
            st.close();
        }
        connection.setAutoCommit(true);
        log.debug("Updated database using updates '" + StringUtils.conjoin(";", updates) + "'.");
    } catch (SQLException e) {
        String msg = "SQL error updating database with sql: " + s + "\n"
                + ExceptionUtils.getSQLExceptionCause(e);
        log.warn(msg, e);
        throw new IOFailure(msg, e);
    } finally {
        rollbackIfNeeded(connection, "updating table with SQL: ", StringUtils.conjoin(";", updates) + "'.");
    }
}

From source file:dao.UserTrafficAddQuery.java

/**
 * This method is not called by spring./*  w  w w.j  a v a2  s. c  o  m*/
 *
 * @param conn the connection passed to this.
 * @param memberid the memberid 
 * @param visitorid the visitorid 
 * @exception BaseDaoException
 */
//public void run(Connection conn, String memberid, String visitorid) throws BaseDaoException {
public void run(Connection conn, String[] params) throws BaseDaoException {

    String memberid = params[0];
    String visitorid = params[1];

    PreparedStatement query = null;
    String stmt = null;
    try {
        stmt = "insert delayed into usertraffic values(" + memberid + ", " + visitorid
                + ", CURRENT_TIMESTAMP())";
        query = conn.prepareStatement(stmt);
        query.executeUpdate();
    } catch (Exception e) {
        throw new BaseDaoException("Error occured, executing usertraffic insert, query = " + stmt, e);
    }
}

From source file:ece356.UserDBAO.java

public static void writeReview(ReviewData review) throws ClassNotFoundException, SQLException, NamingException {
    Connection con = null;/*ww  w  .  j a va2  s .  c  o m*/
    PreparedStatement pstmt = null;
    try {
        con = getConnection();
        pstmt = con.prepareStatement(
                "INSERT INTO review (doc_username, patient_username, date, rating, comment) VALUES (?, ?, NOW(), ?, ?);");
        pstmt.setString(1, review.getDoctorUsername());
        pstmt.setString(2, review.getPatientUsername());
        pstmt.setInt(3, review.getRating());
        pstmt.setString(4, review.getComment());
        pstmt.executeUpdate();
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
        if (con != null) {
            con.close();
        }
    }
}

From source file:dao.CarryonIncrementHitsQuery.java

/**
 * This method is not called by spring./*from  w w  w  . j  av  a  2 s .  c  o  m*/
 * This method is used by Carryon in incrementHits() method.
 * @param conn the connection passed to this.
 * @param entryid the entryid 
 * @param loginid the loginid 
 * @exception BaseDaoException
 */
public void run(Connection conn, String loginid, String entryid) throws BaseDaoException {
    PreparedStatement query = null;
    String stmt = null;
    try {
        stmt = "update low_priority carryon set hits=hits+1 where loginid=" + loginid + " and entryid="
                + entryid + " limit 1";
        query = conn.prepareStatement(stmt);
        query.executeUpdate();
    } catch (Exception e) {
        throw new BaseDaoException("Error occured, executing carryon updating hits, query = " + stmt, e);
    }
}

From source file:com.bosscs.spark.jdbc.writer.JdbcWriter.java

/**
 * Saves data.//from www  . j av a  2 s.  c  o m
 * @param row Data structure representing a row as a Map of column_name:column_value
 * @throws SQLException
 */
public void save(Map<String, Object> row) throws Exception {
    Tuple2<List<String>, String> data = sqlFromRow(row);
    PreparedStatement statement = conn.prepareStatement(data._2());
    int i = 1;
    for (String columnName : data._1()) {
        statement.setObject(i, row.get(columnName));
        i++;
    }
    statement.executeUpdate();
}

From source file:com.imagelake.control.KeyWordsDAOImp.java

@Override
public boolean addKeyWords(int images_images_id, String keyword) {
    System.out.println("Add key words:" + images_images_id);
    boolean ok = false;
    try {//from   w  w w.  j av a 2  s . co m
        String sql = "INSERT INTO key_words(key_word,images_images_id)VALUES(?,?)";
        Connection con = DBFactory.getConnection();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, keyword);
        ps.setInt(2, images_images_id);
        int i = ps.executeUpdate();
        if (i > 0) {
            ok = true;
        } else {
            ok = false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("addKeyWords:" + ok);
    return ok;
}

From source file:dhbw.clippinggorilla.objects.user.UserUtils.java

/**
 * Removes a user//ww  w .j a v  a  2 s.co m
 *
 * @param u The User who should be deleted
 */
public static void removeUser(User u) {
    String sql = "DELETE FROM " + Tables.USER + " WHERE " + Columns.ID + " = ?";
    try {
        PreparedStatement statement = Database.getConnection().prepareStatement(sql);
        statement.setInt(1, u.getId());
        statement.executeUpdate();
    } catch (SQLException ex) {
        Log.warning("Sql failed: removeUser", ex);

    }
    USERS.values().remove(u);
    USERS_PER_EMAIL.values().remove(u);
    USERS_PER_ID.values().remove(u);
    USERS_PER_USERNAME.values().remove(u);
}

From source file:com.redip.dao.impl.QualityNonconformitiesDocDAO.java

@Override
public String deleteNonconformityDoc(int idQualityNonconformityDoc) {
    String statusmessage = "";
    final String sql = "Delete from qualitynonconformitiesdoc " + " WHERE Idqualitynonconformitiesdoc = '"
            + idQualityNonconformityDoc + "'";

    Logger.log(QualityNonconformitiesImpactDAOImpl.class.getName(), Level.INFO,
            "Connecting to jdbc/qualityfollowup from deleteNonconformitiesDoc");
    Connection connection = this.databaseSpring.connect();
    try {//from w  ww .j  a  v a2  s. c  o m
        PreparedStatement preStat = connection.prepareStatement(sql.toString());
        try {
            int resultSet = preStat.executeUpdate();
            try {
                if (resultSet > 0) {
                    statusmessage = "[Success] IDNCI:" + idQualityNonconformityDoc
                            + " has successfully been deleted";
                } else {
                    statusmessage = "[Error] IDNCI:" + idQualityNonconformityDoc + " has not been deleted";
                }
            } catch (Exception exception) {
                Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.ERROR, exception.toString());
            }

        } catch (SQLException exception) {
            Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.ERROR, exception.toString());
        } finally {
            preStat.close();
        }

    } catch (SQLException exception) {
        Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.ERROR, exception.toString());
    } finally {
        try {
            if (connection != null) {
                Logger.log(QualityNonconformitiesImpactDAOImpl.class.getName(), Level.INFO,
                        "Disconnecting to jdbc/qualityfollowup from deleteNonconformitiesImpact");
                connection.close();
            }
        } catch (SQLException e) {
            Logger.log(QualityNonconformitiesDAOImpl.class.getName(), Level.WARN, e.toString());
        }
    }

    return statusmessage;
}

From source file:mupomat.controller.ObradaOperater.java

@Override
public void obrisiPostojeci(Operater entitet) throws SQLException {
    try {//w  ww.j a  v a 2 s  .c  om
        Connection veza = MySqlBazaPodataka.getConnection();
        PreparedStatement izraz = veza.prepareStatement("delete from  operater  where sifra=? ");
        izraz.setInt(1, entitet.getSifra());
        izraz.executeUpdate();
        izraz.close();
        veza.close();
    } catch (ClassNotFoundException | IOException e) {
        //  System.out.println(e.getMessage());
        e.printStackTrace();
        return;
    }
}