Example usage for javax.sql.rowset FilteredRowSet moveToCurrentRow

List of usage examples for javax.sql.rowset FilteredRowSet moveToCurrentRow

Introduction

In this page you can find the example usage for javax.sql.rowset FilteredRowSet moveToCurrentRow.

Prototype

void moveToCurrentRow() throws SQLException;

Source Link

Document

Moves the cursor to the remembered cursor position, usually the current row.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar, age int);");
    st.executeUpdate("insert into survey (id,name,age ) values (1,'nameValue', 10)");
    st.executeUpdate("insert into survey (id,name,age ) values (2,'anotherValue', 100)");

    FilteredRowSet frs = new FilteredRowSetImpl();
    frs.setUsername("sa");
    frs.setPassword("");
    frs.setUrl("jdbc:hsqldb:data/tutorial");
    frs.setCommand("SELECT id, name, age FROM survey");
    frs.execute();//from w  w w.j a va2 s. co  m

    System.out.println("--- Unfiltered RowSet: ---");
    while (frs.next()) {
        System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":"
                + frs.getInt("age"));
    }
    // create a filter that restricts entries in
    // the age column to be between 7 and 10
    AgeFilter filter = new AgeFilter(7, 10, 3);

    // set the filter.
    frs.beforeFirst();
    frs.setFilter(filter);

    // go to the beginning of the Rowset
    System.out.println("--- Filtered RowSet: ---");

    // show filtered data
    while (frs.next()) {
        System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":"
                + frs.getInt("age"));
    }

    System.out.println("--- Try to insert new records ---");

    // Try to add an employee with age = 90 (allowed by filter)
    frs.moveToInsertRow();
    frs.updateString(1, "999");
    frs.updateString(2, "Andre");
    frs.updateInt(3, 90);
    frs.insertRow();
    frs.moveToCurrentRow();
    frs.acceptChanges();

    // try to add an survey with age = 65 (not allowed by filter)
    frs.moveToInsertRow();
    frs.updateString(1, "123");
    frs.updateString(2, "Jeff");
    frs.updateInt(3, 65);
    frs.insertRow();
    frs.moveToCurrentRow();
    frs.acceptChanges();

    // scroll to first row of rowset
    frs.beforeFirst();
    // display rows in FilteredRowset
    System.out.println("FilteredRowSet after trying to insert Jeff (age 65) and Andre (age 90):");
    while (frs.next()) {
        System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":"
                + frs.getInt("age"));
    }

    frs.close();
    st.close();
    frs.close();
}