Example usage for java.sql ResultSet updateRow

List of usage examples for java.sql ResultSet updateRow

Introduction

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

Prototype

void updateRow() throws SQLException;

Source Link

Document

Updates the underlying database with the new contents of the current row of this ResultSet object.

Usage

From source file:org.apache.ambari.server.orm.DBAccessorImpl.java

@Override
public void updateTable(String tableName, DBColumnInfo columnNameFrom, DBColumnInfo columnNameTo)
        throws SQLException {
    LOG.info("Executing query: UPDATE TABLE " + tableName + " SET " + columnNameTo.getName() + "="
            + columnNameFrom.getName());

    String statement = "SELECT * FROM " + tableName;
    int typeFrom = getColumnType(tableName, columnNameFrom.getName());
    int typeTo = getColumnType(tableName, columnNameTo.getName());
    ResultSet rs = executeSelect(statement, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    while (rs.next()) {
        convertUpdateData(rs, columnNameFrom, typeFrom, columnNameTo, typeTo);
        rs.updateRow();
    }/*from   w  w  w .j a  v  a 2  s  .c o m*/
    rs.close();
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

public void updateBlob(Select sel, JDBCStore store, InputStream is) throws SQLException {
    SQLBuffer sql = sel.toSelect(true, store.getFetchConfiguration());
    ResultSet res = null;
    Connection conn = store.getConnection();
    PreparedStatement stmnt = null;
    try {//from www .j  av  a2  s  .c o m
        stmnt = sql.prepareStatement(conn, store.getFetchConfiguration(), ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        setTimeouts(stmnt, store.getFetchConfiguration(), true);
        res = stmnt.executeQuery();
        if (!res.next()) {
            throw new InternalException(_loc.get("stream-exception"));
        }
        Blob blob = res.getBlob(1);
        OutputStream os = blob.setBinaryStream(1);
        copy(is, os);
        os.close();
        res.updateBlob(1, blob);
        res.updateRow();

    } catch (IOException ioe) {
        throw new StoreException(ioe);
    } finally {
        if (res != null)
            try {
                res.close();
            } catch (SQLException e) {
            }
        if (stmnt != null)
            try {
                stmnt.close();
            } catch (SQLException e) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
            }
    }
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

public void updateClob(Select sel, JDBCStore store, Reader reader) throws SQLException {
    SQLBuffer sql = sel.toSelect(true, store.getFetchConfiguration());
    ResultSet res = null;
    Connection conn = store.getConnection();
    PreparedStatement stmnt = null;
    try {//from  ww w  .ja v  a  2s.co m
        stmnt = sql.prepareStatement(conn, store.getFetchConfiguration(), ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        setTimeouts(stmnt, store.getFetchConfiguration(), true);
        res = stmnt.executeQuery();
        if (!res.next()) {
            throw new InternalException(_loc.get("stream-exception"));
        }
        Clob clob = res.getClob(1);
        if (clob != null) {
            Writer writer = clob.setCharacterStream(1);
            copy(reader, writer);
            writer.close();
            res.updateClob(1, clob);
            res.updateRow();
        }

    } catch (IOException ioe) {
        throw new StoreException(ioe);
    } finally {
        if (res != null)
            try {
                res.close();
            } catch (SQLException e) {
            }
        if (stmnt != null)
            try {
                stmnt.close();
            } catch (SQLException e) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
            }
    }
}

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  ww.j a va 2 s  .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.commentaire.modele.CommentaireOuvragesModele.java

public static boolean updateOuvragetype(DataSource ds, int keyId, String contenu_commentaire,
        String date_commentaire, int utilisateurid, int ouvrageid) throws SQLException {

    Connection 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);/*from  w  w w . j  a  v  a 2 s.c  o m*/
    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 (contenu_commentaire != null) {
        entry.updateString("contenu_commentaire", contenu_commentaire);
    }
    if (date_commentaire != null) {
        entry.updateString("date_commentaire", date_commentaire);
    }
    entry.updateInt("utilisateurid", utilisateurid);
    entry.updateInt("ouvrageid", ouvrageid);

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

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

/**
 * Java method that updates a row in the generated sql table
 *
 * @param con (open java.sql.Connection)
 * @param designation_categorie/*from w ww  . jav  a  2 s .  c  o m*/
 * @return boolean (true on success)
 * @throws SQLException
 */
public static boolean updateCategorieouvrage(DataSource ds, int keyId, String designation_categorie)
        throws SQLException {
    con = ds.getConnection();
    String sql = "SELECT * FROM categorieouvrage 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_categorie != null) {
        entry.updateString("designation_categorie", designation_categorie);
    }

    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//from  w ww  .ja  v a2 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: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/*from ww w .  j  a v  a2  s . 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:org.opennms.model.utils.AssetsUpdater.java

protected static void parseCsv2(final File csv) throws ClassNotFoundException, SQLException, IOException {

    String sql = m_dbQuery;//from ww  w .j  ava2s . c  o m

    Connection con = createConnection(false);
    PreparedStatement ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(csv)));
    CSVReader csvReader = new CSVReader(br);
    String[] line;
    int lineCnt = 0;
    while ((line = csvReader.readNext()) != null) {

        System.out.println("Processing csv line: " + String.valueOf(++lineCnt));

        if (line.length != m_fieldMap.size() + 1) {
            continue;
        }

        String foreignSource = m_formatter.formatForeignSource(StringUtils.isBlank(line[0]) ? null : line[0]);

        System.out.println("Running query for foreignSource: " + foreignSource + " ...");
        ps.setString(1, foreignSource);
        ResultSet rs = ps.executeQuery();
        rs.last();
        int rows = rs.getRow();
        if (rows < 1) {
            rs.close();
            System.out.println("No results found for foreignsource: " + foreignSource
                    + "; continuing to next foreignsource...");
            continue;
        }
        System.out.println("Found " + rows + " rows.");

        rs.beforeFirst();

        while (rs.next()) {
            System.out.println("Updating node: " + rs.getInt("nodeid"));

            Set<Entry<Integer, String>> entrySet = m_fieldMap.entrySet();
            for (Entry<Integer, String> entry : entrySet) {
                int csvField = entry.getKey() - 1;
                String columnName = entry.getValue();
                System.out.println(
                        "\t" + "updating column: " + columnName + " with csv field: " + line[csvField]);
                rs.updateString(columnName, line[csvField]);
            }

            rs.updateRow();
        }
        rs.close();
    }

    try {
        con.commit();
    } catch (SQLException e) {
        e.printStackTrace();
        con.rollback();
    }

    csvReader.close();
    ps.close();
    con.close();
}

From source file:org.opennms.provisiond.utils.CsvRequisitionParser.java

private static void migrateDbNodes() throws SQLException, UnknownHostException, ClassNotFoundException {

    String distinctNodesQueryStr = "  " + "SELECT nodeId AS \"nodeid\"," + "       nodeLabel AS \"nodelabel\","
            + "       foreignSource AS \"foreignsource\"," + "       foreignId AS \"foreignid\" "
            + "  FROM node " + " WHERE nodeid in (" + "  SELECT " + "DISTINCT nodeid " + "    FROM ipinterface "
            + "   WHERE iplike(ipaddr, '" + m_iplikeQuery + "')) " + "ORDER BY nodeid";

    if (m_addOnly) {
        distinctNodesQueryStr = "  " + "SELECT nodeId AS \"nodeid\"," + "       nodeLabel AS \"nodelabel\","
                + "       foreignSource AS \"foreignsource\"," + "       foreignId AS \"foreignid\" "
                + "  FROM node " + " WHERE nodeid in (" + "  SELECT " + "DISTINCT nodeid "
                + "    FROM ipinterface " + "   WHERE iplike(ipaddr, '" + m_iplikeQuery + "')) "
                + "  AND foreignsource is NULL " + "ORDER BY nodeid";
    }/*  w  ww  . jav  a 2  s.c  o  m*/

    Connection connection = null;
    Statement distinctNodesStatement = null;
    PoolingConnection pool = null;
    connection = createConnection();
    connection.setAutoCommit(false);
    pool = new PoolingConnection(connection);
    distinctNodesStatement = pool.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    ResultSet distinctNodesResultSet = null;
    int rowsFound = 0;
    distinctNodesResultSet = distinctNodesStatement.executeQuery(distinctNodesQueryStr);
    distinctNodesResultSet.last();
    rowsFound = distinctNodesResultSet.getRow();
    distinctNodesResultSet.beforeFirst();

    System.out.println(rowsFound + " nodes found.");

    int nodesMigrated = 0;
    while (distinctNodesResultSet.next()) {
        System.out.println("Processing row: " + distinctNodesResultSet.getRow() + "...");

        int nodeId = distinctNodesResultSet.getInt("nodeid");
        String queryStr = "" + "  SELECT ipaddr " + "    FROM ipinterface " + "   WHERE nodeid = " + nodeId
                + " " + "     AND issnmpprimary = 'P' " + "ORDER BY inet(ipaddr)" + "   LIMIT 1";

        Statement findPrimaryStatement = pool.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_READ_ONLY);

        System.out.println("Querying DB for SNMP Primary interface for node: " + nodeId + "...");
        ResultSet findPrimaryResultSet = findPrimaryStatement.executeQuery(queryStr);

        String primaryIp = null;

        if (findPrimaryResultSet.next()) {
            primaryIp = findPrimaryResultSet.getString("ipaddr");
            System.out.println("SNMP Primary found: " + primaryIp);
        }

        findPrimaryResultSet.close();
        findPrimaryStatement.close();

        if (primaryIp == null) {
            System.out.println("SNMP Primary not found.  Determining lowest numbered IP to set as Primary...");
            queryStr = "" + "  SELECT ipaddr " + "    FROM ipinterface " + "   WHERE nodeid = " + nodeId + " "
                    + "ORDER BY inet(ipaddr)" + "   LIMIT 1";
            findPrimaryStatement = pool.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            findPrimaryResultSet = findPrimaryStatement.executeQuery(queryStr);
        }

        if (primaryIp == null && findPrimaryResultSet.next()) {
            primaryIp = findPrimaryResultSet.getString("ipaddr");
            System.out.println("SNMP Primary found: " + primaryIp);
        }

        findPrimaryResultSet.close();
        findPrimaryStatement.close();

        if (primaryIp == null) {
            System.out.println(
                    "SNMP Primary not found.  Skipping node.  (This should never happen since it is the iplike query that finds the distinct nodes :( )");
            continue;
        }

        String foreignId = null;
        if (m_useNodeId) {
            foreignId = String.valueOf(nodeId);
        } else {
            foreignId = String.valueOf(System.currentTimeMillis());
        }

        String label = distinctNodesResultSet.getString("nodelabel");
        distinctNodesResultSet.updateString("foreignsource", m_foreignSource);
        distinctNodesResultSet.updateString("foreignId", foreignId);

        System.out.println("Updating node (" + nodeId + ":" + label + ") with foreignsource:" + m_foreignSource
                + " and foreignId:" + foreignId);
        distinctNodesResultSet.updateRow();
        System.out.println("Node updated.");

        RequisitionData rd = new RequisitionData(label, primaryIp, m_foreignSource, foreignId);

        if (m_categoryAddExisting) {
            String categoriesQueryString = "" + "SELECT c.categoryname as \"categoryname\" "
                    + "  FROM categories c " + "  JOIN category_node cn "
                    + "    ON cn.categoryid = c.categoryid " + "  JOIN node n on n.nodeid = cn.nodeid "
                    + " WHERE n.nodeid = " + nodeId;
            Statement categoriesStatement = pool.createStatement();

            ResultSet crs = categoriesStatement.executeQuery(categoriesQueryString);

            Set<String> categories = new LinkedHashSet<String>();
            while (crs.next()) {
                categories.add(crs.getString("categoryname"));
            }

            crs.close();
            categoriesStatement.close();
            rd.setCategories(categories);
        }

        System.out.println("Updating requistion...");
        createOrUpdateRequistion(rd);
        System.out.println("Requistion updated!  Next...\n");
        nodesMigrated++;
    }

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

    distinctNodesResultSet.close();
    distinctNodesStatement.close();
    pool.close();
    connection.close();

    System.out.println(nodesMigrated + " Nodes migrated to foreign source " + m_foreignSource);

}