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:Main.java

public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    String sqlQuery = "SELECT uid, name, duration from EVENTS";

    ResultSet rs = stmt.executeQuery(sqlQuery);

    while (rs.next()) {
        rs.updateString("Name", "new Name");

        rs.updateRow();
    }/* w  w w .  j a  va2  s  .  com*/

    rs.first();
    while (rs.next()) {
        String name = rs.getString(2);
        Timestamp hireDate = rs.getTimestamp(5);
        System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }

    rs.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:mysql://192.168.1.3/zzzTest?" + //
            "useUnicode=yes&characterEncoding=UTF-8" + //
            "&user=root&password=whatever");
    String newName = "G";
    String newEmail = "g@example.com";
    String newMobile = "444-555-2222";
    String sql = "SELECT " + //
            "id, " + //
            "name, " + //
            "email, " + //
            "mobile " + //
            "FROM registerSmsUsers " + //
            "WHERE mobile = ? " + //
            "FOR UPDATE";
    PreparedStatement pst = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    pst.setString(1, newMobile);// ww  w . j  a v  a  2  s  .c o  m
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        rs.moveToCurrentRow();
        rs.updateString("name", newName);
        rs.updateString("email", newEmail);
        rs.updateRow();
        System.out.println("Existing row updated.");
    } else {
        rs.moveToInsertRow();
        rs.updateString("name", newName);
        rs.updateString("email", newEmail);
        rs.updateString("mobile", newMobile);
        rs.insertRow();
        System.out.println("New row inserted.");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);/*from   w w w .ja v  a  2s .  c  o  m*/

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble(3) * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:UpdateableRs.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);/*from  ww w . j a  va  2  s.com*/

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble("salary") * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);// w w  w.  j  av a  2  s. c o m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    resultSet.first();

    // Update the value of column col_string on that row
    resultSet.updateString("col_string", "new data");

    // Update the row; if auto-commit is enabled, update is committed
    resultSet.updateRow();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,'Tom')");

    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // Move cursor to the row to update
    rs.first();//from www  . ja va 2s  .  c  om

    // Update the value of column column_1 on that row
    rs.updateString(2, "new data");

    // Update the row; if autocommit is enabled,
    // update is committed
    rs.updateRow();

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,'Tom')");

    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // Move cursor to the row to update
    rs.first();/* w w w  .jav a2s. co  m*/

    // Update the value of column column_1 on that row
    rs.updateString("name", "new data");

    // Update the row; if autocommit is enabled,
    // update is committed
    rs.updateRow();

    rs.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
            ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT id, code, name, quantity, price FROM products";
    ResultSet uprs = statement.executeQuery(query);

    while (uprs.next()) {
        System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":" + uprs.getString("name")
                + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price"));
    }/*from  w  w w .j  a  v  a2 s . c o m*/
    uprs.first();
    uprs.updateString("name", "Java");
    uprs.updateRow();
    uprs.next();
    uprs.deleteRow();

    uprs.moveToInsertRow();
    uprs.updateString("code", "1");
    uprs.updateString("name", "Data Structures");
    uprs.updateInt("quantity", 1);
    uprs.updateDouble("price", 5.99);
    uprs.insertRow();

    uprs.beforeFirst();
    while (uprs.next()) {
        System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t" + uprs.getString("name")
                + "\t" + uprs.getInt("quantity") + "\t" + uprs.getDouble("price"));
    }
    connection.close();
}

From source file:ResultSetUpdate.java

public static void main(String args[]) {

    String url;//from   w  ww.  j a  v a  2  s.  co  m
    url = "jdbc:odbc:UserDB";

    String user, pass;
    user = "ian";
    pass = "stjklsq";

    Connection con;
    Statement stmt;
    ResultSet rs;

    try {
        con = DriverManager.getConnection(url, user, pass);
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        rs = stmt.executeQuery("SELECT * FROM Users where nick=\"ian\"");

        // Get the resultset ready, update the passwd field, commit
        rs.first();
        rs.updateString("password", "unguessable");
        rs.updateRow();

        rs.close();
        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:PrintResultSet.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc: Contacts");
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT Name,Description,Qty,Cost FROM Stock");
    ResultSetMetaData md = rs.getMetaData();

    if (rs.getConcurrency() == ResultSet.CONCUR_UPDATABLE)
        System.out.println("UPDATABLE");
    else//from   w  w w.  j ava 2 s . com
        System.out.println("READ_ONLY");

    int nColumns = md.getColumnCount();
    for (int i = 1; i <= nColumns; i++) {
        System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t"));
    }
    while (rs.next()) {
        rs.updateString("Street", "123 Main");
        rs.updateRow();
        for (int i = 1; i <= nColumns; i++) {
            System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t"));
        }
    }
}