Example usage for java.sql ResultSet TYPE_SCROLL_SENSITIVE

List of usage examples for java.sql ResultSet TYPE_SCROLL_SENSITIVE

Introduction

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

Prototype

int TYPE_SCROLL_SENSITIVE

To view the source code for java.sql ResultSet TYPE_SCROLL_SENSITIVE.

Click Source Link

Document

The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes to the data that underlies the ResultSet.

Usage

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_READ_ONLY);

    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 forward
    while (rs.next()) {
        // Get data at cursor
        String id = rs.getString(1);
        String name = rs.getString(2);
    }// w w  w  .  j a  v  a 2  s . c o  m

    // Move cursor backward
    while (rs.previous()) {
        // Get data at cursor
        String id = rs.getString(1);
        String name = rs.getString(2);
    }

    // Move cursor to the first row
    rs.first();

    // Move cursor to the last row
    rs.last();

    // Move cursor to the end, after the last row
    rs.afterLast();

    // Move cursor to the beginning, before the first row.
    // cursor position is 0.
    rs.beforeFirst();

    // Move cursor to the second row
    rs.absolute(2);

    // Move cursor to the last row
    rs.absolute(-1);

    // Move cursor to the second-to-last row
    rs.absolute(-2);

    // Move cursor down 5 rows from the current row. If this moves
    // cursor beyond the last row, cursor is put after the last row
    rs.relative(5);

    // Move cursor up 3 rows from the current row. If this moves
    // cursor beyond the first row, cursor is put before the first row
    rs.relative(-3);

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

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("org.apache.derby.jdbc.ClientDriver");
    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/testDb", "name", "pass");
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    String insertEmp1 = "insert into emp values(10,'A','trainee')";
    String insertEmp2 = "insert into emp values(11,'B','trainee')";
    String insertEmp3 = "insert into emp values(12,'C','trainee')";
    con.setAutoCommit(false);// w  w  w .j a  v a  2  s.c  om
    stmt.addBatch(insertEmp1);
    stmt.addBatch(insertEmp2);
    stmt.addBatch(insertEmp3);
    ResultSet rs = stmt.executeQuery("select * from emp");
    rs.last();
    System.out.println("rows before batch execution= " + rs.getRow());
    stmt.executeBatch();
    con.commit();
    System.out.println("Batch executed");
    rs = stmt.executeQuery("select * from emp");
    rs.last();
    System.out.println("rows after batch execution= " + rs.getRow());
}

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.  ja  v  a 2 s.  co  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:Main.java

public static void main(String[] args) throws SQLException {
    // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@yourDB", "scott", "tiger");

    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    stmt.setFetchSize(1);/*from  w w w .j  a v a2 s  . c o  m*/
    ResultSet rset = stmt.executeQuery("select EMPNO, ENAME, SAL from EMP");
    showProperty(rset);

    while (rset.next()) {
        System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3));
    }
    doSomeChanges(conn);
    // Place the cursor before the first row
    rset.beforeFirst();

    while (rset.next()) {
        System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3));
    }
    rset.close();
    stmt.close();
    cleanup(conn);
    conn.close();
}

From source file:SqlWarning.java

public static void main(String[] args) {
    try {//from  w w  w  .j av  a 2 s.  com
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
        Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");

        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        SQLWarning sw = null;

        ResultSet rs = stmt.executeQuery("Select * from employees");
        sw = stmt.getWarnings();
        System.out.println(sw.getMessage());

        while (rs.next()) {
            System.out.println("Employee name: " + rs.getString(2));
        }
        rs.previous();
        rs.updateString("name", "Jon");

    } catch (SQLException se) {
        System.out.println("SQLException occurred: " + se.getMessage());

    } catch (Exception e) {
        e.printStackTrace();

    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";

    Connection con;// w ww.jav  a2 s .c o m
    Statement stmt;
    ResultSet uprs;

    try {
        Class.forName(driver);
        con = DriverManager.getConnection("jdbc:odbc:RainForestDSN", "student", "student");
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        uprs = stmt.executeQuery("SELECT * FROM Records");

        // Check the column count
        ResultSetMetaData md = uprs.getMetaData();
        System.out.println("Resultset has " + md.getColumnCount() + " cols.");

        int rowNum = uprs.getRow();
        System.out.println("row1 " + rowNum);
        uprs.absolute(1);
        rowNum = uprs.getRow();
        System.out.println("row2 " + rowNum);
        uprs.next();
        uprs.moveToInsertRow();
        uprs.updateInt(1, 150);
        uprs.updateString(2, "Madonna");
        uprs.updateString(3, "Dummy");
        uprs.updateString(4, "Jazz");
        uprs.updateString(5, "Image");
        uprs.updateInt(6, 5);
        uprs.updateDouble(7, 5);
        uprs.updateInt(8, 15);
        uprs.insertRow();
        uprs.close();
        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from www.j av  a  2  s  .  co  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_READ_ONLY);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from   w ww  .  j  av a  2s. 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");

    // Retrieve the current values of the row from the database
    resultSet.refreshRow();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from   w  ww  .  jav a2s.  com*/

    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[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/* w w w  . j av a2s .  c om*/

    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");

    // Move cursor to the row to update
    resultSet.first();

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

    // Discard the update to the row
    resultSet.cancelRowUpdates();

}