Example usage for java.sql ResultSet updateString

List of usage examples for java.sql ResultSet updateString

Introduction

In this page you can find the example usage for java.sql ResultSet updateString.

Prototype

void updateString(String columnLabel, String x) throws SQLException;

Source Link

Document

Updates the designated column with a String value.

Usage

From source file:Transaction.java

public void doWork() {
    try {/*from  w w w.ja  va2  s . c  o  m*/
        java.util.Date now = new java.util.Date();
        connection.setAutoCommit(false);
        Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = statement.executeQuery("SELECT * FROM acc_add WHERE acc_id = 1034055 and ts = 0");

        // set old row ts = current time
        rs.next();
        rs.updateTimestamp("ts", new Timestamp(now.getTime()));
        rs.updateRow();

        rs.moveToInsertRow();
        rs.updateInt("add_id", rs.getInt("add_id"));
        rs.updateInt("acc_id", rs.getInt("acc_id"));
        rs.updateString("name", rs.getString("name"));
        rs.updateString("address1", "555 East South Street");
        rs.updateString("address2", "");
        rs.updateString("address3", "");
        rs.updateString("city", rs.getString("city"));
        rs.updateString("state", rs.getString("state"));
        rs.updateString("zip", rs.getString("zip"));
        rs.updateTimestamp("ts", new Timestamp(0));
        rs.updateTimestamp("act_ts", new Timestamp(now.getTime()));
        rs.insertRow();
        connection.commit();

        rs.close();
        statement.close();
        connection.close();

    } catch (Exception e) {
        try {
            connection.rollback();
        } catch (SQLException error) {
        }
        e.printStackTrace();
    }
}

From source file:com.oracle.tutorial.jdbc.CoffeesTable.java

public void insertRow(String coffeeName, int supplierID, float price, int sales, int total)
        throws SQLException {
    Statement stmt = null;/*from  w  w w.j  a  v  a 2  s. co  m*/
    try {
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");

        uprs.moveToInsertRow();

        uprs.updateString("COF_NAME", coffeeName);
        uprs.updateInt("SUP_ID", supplierID);
        uprs.updateFloat("PRICE", price);
        uprs.updateInt("SALES", sales);
        uprs.updateInt("TOTAL", total);

        uprs.insertRow();
        uprs.beforeFirst();

    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:org.biblionum.ouvrage.modele.OuvrageTypeModele.java

/**
 * Java method that updates a row in the generated sql table
 *
 * @param con (open java.sql.Connection)
 * @param designation_typeou// w ww  .j av  a  2s. c o  m
 * @return boolean (true on success)
 * @throws SQLException
 */
public boolean updateOuvragetype(DataSource ds, int keyId, String designation_typeou) throws SQLException {
    con = ds.getConnection();
    String sql = "SELECT * FROM ouvragetype WHERE id = ?";
    PreparedStatement statement = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);
    statement.setInt(1, keyId);
    ResultSet entry = statement.executeQuery();

    entry.last();
    int rows = entry.getRow();
    entry.beforeFirst();
    if (rows == 0) {
        entry.close();
        statement.close();
        con.close();
        return false;
    }
    entry.next();

    if (designation_typeou != null) {
        entry.updateString("designation_typeou", designation_typeou);
    }

    entry.updateRow();
    entry.close();
    statement.close();
    con.close();
    return true;
}

From source file:net.solarnetwork.node.dao.jdbc.JdbcSettingDao.java

private void storeSettingInternal(final String key, final String ttype, final String value, final int flags) {
    final String type = (ttype == null ? "" : ttype);
    final Timestamp now = new Timestamp(System.currentTimeMillis());
    // to avoid bumping modified date column when values haven't changed, we are careful here
    // to compare before actually updating
    getJdbcTemplate().query(new PreparedStatementCreator() {

        @Override//from   w ww . j  a  v a2s  . co  m
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            PreparedStatement queryStmt = con.prepareStatement(sqlGet, ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
            queryStmt.setString(1, key);
            queryStmt.setString(2, type);
            return queryStmt;
        }
    }, new ResultSetExtractor<Object>() {

        @Override
        public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
            if (rs.next()) {
                String oldValue = rs.getString(1);
                if (!value.equals(oldValue)) {
                    rs.updateString(1, value);
                    rs.updateTimestamp(2, now);
                    rs.updateRow();
                }
            } else {
                rs.moveToInsertRow();
                rs.updateString(1, value);
                rs.updateTimestamp(2, now);
                rs.updateString(3, key);
                rs.updateString(4, type);
                rs.updateInt(5, flags);
                rs.insertRow();
            }
            return null;
        }
    });
}

From source file:org.biblionum.authentification.modele.UtilisateurModele.java

/**
 * Java method that updates a row in the generated sql table
 *
 * @param con (open java.sql.Connection)
 * @param nom/*  w w w .  j av a2s  . c  o  m*/
 * @param password
 * @param pseudo
 * @param prenom
 * @param utilisateur_type_id
 * @return boolean (true on success)
 * @throws SQLException
 */
public boolean updateUtilisateur(DataSource ds, int keyId, String nom, String password, String pseudo,
        String prenom) throws SQLException {
    con = ds.getConnection();
    String sql = "SELECT * FROM utilisateur WHERE id = ?";
    PreparedStatement statement = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);
    statement.setInt(1, keyId);
    ResultSet entry = statement.executeQuery();

    entry.last();
    int rows = entry.getRow();
    entry.beforeFirst();
    if (rows == 0) {
        entry.close();
        statement.close();
        con.close();
        return false;
    }
    entry.next();

    if (nom != null) {
        entry.updateString("nom", nom);
    }
    if (password != null) {
        entry.updateString("password", password);
    }
    if (pseudo != null) {
        entry.updateString("pseudo", pseudo);
    }
    if (prenom != null) {
        entry.updateString("prenom", prenom);
    }

    entry.updateRow();
    entry.close();
    statement.close();
    con.close();
    return true;
}

From source file:org.biblionum.ouvrage.modele.OuvrageModele.java

/**
 * Java method that updates a row in the generated sql table
 *
 * @param con (open java.sql.Connection)
 * @param auteur/*  ww w .  ja  va2 s  .c  o m*/
 * @param editeur
 * @param annee_edition
 * @param resume
 * @param nb_page
 * @param emplacement
 * @param couverture
 * @param ouvrageTipeid
 * @param categorieOuvrageid
 * @param niveauid_niveau
 * @param filiereid
 * @param titre
 * @return boolean (true on success)
 * @throws SQLException
 */
public boolean updateUtilisateur(DataSource ds, int keyId, String auteur, String editeur, int annee_edition,
        String resume, int nb_page, String emplacement, String couverture, int ouvrageTipeid,
        int categorieOuvrageid, int niveauid_niveau, int filiereid, String titre) throws SQLException {
    con = ds.getConnection();
    String sql = "SELECT * FROM ouvrage WHERE id = ?";
    PreparedStatement statement = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);
    statement.setInt(1, keyId);
    ResultSet entry = statement.executeQuery();

    entry.last();
    int rows = entry.getRow();
    entry.beforeFirst();
    if (rows == 0) {
        entry.close();
        statement.close();
        con.close();
        return false;
    }
    entry.next();

    if (auteur != null) {
        entry.updateString("auteur", auteur);
    }
    if (editeur != null) {
        entry.updateString("editeur", editeur);
    }
    entry.updateInt("annee_edition", annee_edition);
    if (resume != null) {
        entry.updateString("resume", resume);
    }
    entry.updateInt("nb_page", nb_page);
    if (emplacement != null) {
        entry.updateString("emplacement", emplacement);
    }
    if (couverture != null) {
        entry.updateString("couverture", couverture);
    }
    entry.updateInt("ouvrageTipeid", ouvrageTipeid);
    entry.updateInt("categorieOuvrageid", categorieOuvrageid);
    entry.updateInt("niveauid_niveau", niveauid_niveau);
    entry.updateInt("filiereid", filiereid);
    if (titre != null) {
        entry.updateString("titre", titre);
    }

    entry.updateRow();
    entry.close();
    statement.close();
    con.close();
    return true;
}

From source file:gov.nih.nci.migration.MigrationDriver.java

private void encryptDecryptApplicationInformation() throws EncryptionException, SQLException {

    Connection connection = getConnection();
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    ResultSet resultSet = null;
    if ("oracle".equals(DATABASE_TYPE)) {
        resultSet = stmt.executeQuery("SELECT CSM_APPLICATION.* FROM CSM_APPLICATION FOR UPDATE");
    } else {// ww w . java  2  s .  c om
        resultSet = stmt.executeQuery("SELECT * FROM CSM_APPLICATION");
    }

    String databasePassword = null;
    String encryptedDatabasePassword = null;

    while (resultSet.next()) {
        databasePassword = resultSet.getString("DATABASE_PASSWORD");

        if (!StringUtilities.isBlank(databasePassword)) {
            String orgPasswordStr = desEncryption.decrypt(databasePassword);
            encryptedDatabasePassword = aesEncryption.encrypt(orgPasswordStr);
            if (!StringUtilities.isBlank(encryptedDatabasePassword)) {
                resultSet.updateString("DATABASE_PASSWORD", encryptedDatabasePassword);
            }
        }
        System.out.println("Updating Application:" + resultSet.getString("APPLICATION_NAME"));
        resultSet.updateRow();
    }

}

From source file:org.springframework.jdbc.object.SqlQueryTests.java

public void testUpdateCustomers() throws SQLException {
    mockResultSet.next();// w  w  w  .  jav a2  s  .  c om
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.updateString(2, "Rod");
    ctrlResultSet.setVoidCallable();
    mockResultSet.updateRow();
    ctrlResultSet.setVoidCallable();
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(2);
    mockResultSet.updateString(2, "Thomas");
    ctrlResultSet.setVoidCallable();
    mockResultSet.updateRow();
    ctrlResultSet.setVoidCallable();
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(2), Types.NUMERIC);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_ID_FORENAME_WHERE_ID, ResultSet.TYPE_FORWARD_ONLY,
            ResultSet.CONCUR_UPDATABLE);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    class CustomerUpdateQuery extends UpdatableSqlQuery {

        public CustomerUpdateQuery(DataSource ds) {
            super(ds, SELECT_ID_FORENAME_WHERE_ID);
            declareParameter(new SqlParameter(Types.NUMERIC));
            compile();
        }

        protected Object updateRow(ResultSet rs, int rownum, Map context) throws SQLException {
            rs.updateString(2, "" + context.get(new Integer(rs.getInt(COLUMN_NAMES[0]))));
            return null;
        }
    }
    CustomerUpdateQuery query = new CustomerUpdateQuery(mockDataSource);
    Map values = new HashMap(2);
    values.put(new Integer(1), "Rod");
    values.put(new Integer(2), "Thomas");
    List customers = query.execute(2, values);
}

From source file:org.zanata.liquibase.custom.MigrateHTermCommentToString.java

@Override
public void execute(Database database) throws CustomChangeException {
    final JdbcConnection conn = (JdbcConnection) database.getConnection();
    try (Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {

        Map<Long, String> termCommentsMap = new HashMap<Long, String>();

        ResultSet rs1 = null;/*from w  w  w. ja  va 2  s . c o m*/
        ResultSet rs2 = null;

        try {
            String termCommentsSql = "select term.id, comment.comment from "
                    + "HGlossaryTerm as term, HTermComment as comment where comment.glossaryTermId = term.id";
            rs1 = stmt.executeQuery(termCommentsSql);

            while (rs1.next()) {
                long termId = rs1.getLong(1);
                String comment = rs1.getString(2);
                String newComment = null;

                if (termCommentsMap.containsKey(termId)) {
                    newComment = joinComment(termCommentsMap.get(termId), comment);
                } else {
                    newComment = joinComment(null, comment);
                }
                termCommentsMap.put(termId, newComment);
            }

            String termSql = "select term.id, term.comment from HGlossaryTerm term";
            rs2 = stmt.executeQuery(termSql);

            while (rs2.next()) {
                long termId = rs2.getLong(1);
                String comment = termCommentsMap.get(termId);
                rs2.updateString(2, comment);
                rs2.updateRow();
            }
        } finally {
            rs1.close();
            rs2.close();
        }
    } catch (SQLException | DatabaseException e) {
        throw new CustomChangeException(e);
    }
}

From source file:com.tascape.qa.th.db.H2Handler.java

@Override
public void updateSuiteExecutionResult(String execId) throws SQLException {
    LOG.info("Update test suite execution result with execution id {}", execId);
    int total = 0, fail = 0;

    try (Connection conn = this.getConnection();) {
        final String sql1 = "SELECT " + Test_Result.EXECUTION_RESULT.name() + " FROM " + TestResult.TABLE_NAME
                + " WHERE " + Test_Result.SUITE_RESULT.name() + " = ?;";
        try (PreparedStatement stmt = conn.prepareStatement(sql1)) {
            stmt.setString(1, execId);//from  w w w .java  2 s  . c o  m
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                total++;
                String result = rs.getString(Test_Result.EXECUTION_RESULT.name());
                if (!result.equals(ExecutionResult.PASS.name()) && !result.endsWith("/0")) {
                    fail++;
                }
            }
        }
    }

    try (Connection conn = this.getConnection();) {
        final String sql = "SELECT * FROM " + SuiteResult.TABLE_NAME + " WHERE " + SuiteResult.SUITE_RESULT_ID
                + " = ?;";
        try (PreparedStatement stmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE)) {
            stmt.setString(1, execId);
            ResultSet rs = stmt.executeQuery();
            if (rs.first()) {
                rs.updateInt(SuiteResult.NUMBER_OF_TESTS, total);
                rs.updateInt(SuiteResult.NUMBER_OF_FAILURE, fail);
                rs.updateString(SuiteResult.EXECUTION_RESULT, fail == 0 ? "PASS" : "FAIL");
                rs.updateLong(SuiteResult.STOP_TIME, System.currentTimeMillis());
                rs.updateRow();
            }
        }
    }
}