Example usage for javax.sql.rowset FilteredRowSet setCommand

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

Introduction

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

Prototype

void setCommand(String cmd) throws SQLException;

Source Link

Document

Sets this RowSet object's command property to the given SQL query.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    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)");

    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  www  .  j  a  v  a 2  s . co  m
    while (frs.next()) {
        System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":"
                + frs.getInt("age"));
    }
    AgeFilter filter = new AgeFilter(7, 10, 3);
    frs.beforeFirst();
    frs.setFilter(filter);
    while (frs.next()) {
        System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":"
                + frs.getInt("age"));
    }
    frs.beforeFirst();
    while (frs.next()) {
        System.out.println(frs.getRow() + " - " + frs.getString("id") + ":" + frs.getString("name") + ":"
                + frs.getInt("age"));
    }
    frs.close();
    st.close();
    frs.close();
}

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   ww  w .  ja v  a 2 s .  c  o 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();
}

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

public void testFilteredRowSet() {
    FilteredRowSet frs = null;
    StateFilter myStateFilter = new StateFilter(10000, 10999, 1);
    String[] cityArray = { "SF", "LA" };

    CityFilter myCityFilter = new CityFilter(cityArray, 2);

    try {//from w w  w .j a  v  a  2 s. c o m
        frs = new FilteredRowSetImpl();

        frs.setCommand("SELECT * FROM COFFEE_HOUSES");
        frs.setUsername(settings.userName);
        frs.setPassword(settings.password);
        frs.setUrl(settings.urlString);
        frs.execute();

        System.out.println("\nBefore filter:");
        FilteredRowSetSample.viewTable(this.con);

        System.out.println("\nSetting state filter:");
        frs.beforeFirst();
        frs.setFilter(myStateFilter);
        this.viewFilteredRowSet(frs);

        System.out.println("\nSetting city filter:");
        frs.beforeFirst();
        frs.setFilter(myCityFilter);
        this.viewFilteredRowSet(frs);

    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    }
}