Example usage for javax.sql.rowset CachedRowSet moveToCurrentRow

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

Introduction

In this page you can find the example usage for javax.sql.rowset CachedRowSet 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);");
    st.executeUpdate("create view surveyView as (select * from survey);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    CachedRowSet crs = null;
    RowSetMetaData rsMD = new RowSetMetaDataImpl();
    rsMD.setColumnCount(2);/* w w  w  .j  av  a  2 s .c  o  m*/
    rsMD.setColumnName(1, "id");
    rsMD.setColumnType(1, Types.VARCHAR);
    rsMD.setColumnName(2, "name");
    rsMD.setColumnType(2, Types.VARCHAR);
    // sets the designated column's table name, if any, to the given String.
    rsMD.setTableName(1, "survey");
    rsMD.setTableName(2, "survey");

    // use a custom made RowSetMetaData object for CachedRowSet object
    crs = new CachedRowSetImpl();
    crs.setMetaData(rsMD);

    crs.moveToInsertRow();
    crs.updateString(1, "1111");
    crs.updateString(2, "alex");
    crs.insertRow();

    crs.moveToInsertRow();
    crs.updateString(1, "2222");
    crs.updateString(2, "jane");
    crs.insertRow();
    crs.moveToCurrentRow();

    crs.acceptChanges(conn);
    conn.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);");
    st.executeUpdate("create view surveyView as (select * from survey);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    CachedRowSet crs = null;
    RowSetMetaData rsMD = new RowSetMetaDataImpl();
    rsMD.setColumnCount(2);/*from   w ww . java 2  s  .  c  om*/
    rsMD.setColumnName(1, "id");
    rsMD.setColumnType(1, Types.VARCHAR);
    rsMD.setColumnName(2, "name");
    rsMD.setColumnType(2, Types.VARCHAR);
    // sets the designated column's table name, if any, to the given String.
    rsMD.setTableName(1, "survey");
    rsMD.setTableName(2, "survey");

    // use a custom made RowSetMetaData object for CachedRowSet object
    crs = new CachedRowSetImpl();
    crs.setMetaData(rsMD);

    crs.moveToInsertRow();
    crs.updateString(1, "1111");
    crs.updateString(2, "alex");
    crs.insertRow();

    crs.moveToInsertRow();
    crs.updateString(1, "2222");
    crs.updateString(2, "jane");
    crs.insertRow();

    // if you want to commit changes from a CachedRowSet
    // object to your desired datasource, then you must
    // create a Connection object.
    //
    //conn = getHSQLConnection();

    // moves the cursor to the remembered cursor position, usually
    // the current row. This method has no effect if the cursor is
    // not on the insert row.
    crs.moveToCurrentRow();

    // when the method acceptChanges() is executed, the CachedRowSet
    // object's writer, a RowSetWriterImpl object, is called behind the
    // scenes to write the changes made to the rowset to the underlying
    // data source. The writer is implemented to make a connection to
    // the data source and write updates to it.
    crs.acceptChanges(conn);
    conn.close();
}

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

public void rowChanged(RowSetEvent event) {

    CachedRowSet currentRowSet = this.myCoffeesTableModel.coffeesRowSet;

    try {//from   w  w w. j  av a2 s.com
        currentRowSet.moveToCurrentRow();
        myCoffeesTableModel = new CoffeesTableModel(myCoffeesTableModel.getCoffeesRowSet());
        table.setModel(myCoffeesTableModel);

    } catch (SQLException ex) {

        JDBCTutorialUtilities.printSQLException(ex);

        // Display the error in a dialog box.

        JOptionPane.showMessageDialog(CoffeesFrame.this, new String[] { // Display a 2-line message
                ex.getClass().getName() + ": ", ex.getMessage() });
    }
}

From source file:Main.java

  public void readData(RowSetInternal caller) throws SQLException {
  System.out.println("--- CustomRowSetReader: begin. ---");
  if (caller == null) {
    System.out.println("CustomRowSetReader: caller is null.");
    return;/*from w  w w  . ja va  2 s .  co m*/
  }

  CachedRowSet crs = (CachedRowSet) caller;
  // CachedRowSet crs = (CachedRowSet) caller.getOriginal();

  RowSetMetaData rsmd = new RowSetMetaDataImpl();

  rsmd.setColumnCount(3);

  rsmd.setColumnType(1, Types.VARCHAR);
  rsmd.setColumnType(2, Types.INTEGER);
  rsmd.setColumnType(3, Types.VARCHAR);

  rsmd.setColumnName(1, "col1");
  rsmd.setColumnName(2, "col2");
  rsmd.setColumnName(3, "col3");

  crs.setMetaData(rsmd);
  System.out.println("CustomRowSetReader: crs.setMetaData( rsmd );");

  crs.moveToInsertRow();

  crs.updateString(1, "StringCol11");
  crs.updateInt(2, 1);
  crs.updateString(3, "StringCol31");
  crs.insertRow();
  System.out.println("CustomRowSetReader: crs.insertRow() 1");

  crs.updateString(1, "StringCol12");
  crs.updateInt(2, 2);
  crs.updateString(3, "StringCol32");
  crs.insertRow();
  System.out.println("CustomRowSetReader: crs.insertRow() 2");

  crs.moveToCurrentRow();
  crs.beforeFirst();
  displayRowSet(crs);
  crs.beforeFirst();
  // crs.acceptChanges();
  System.out.println("CustomRowSetReader: end.");
}

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

public void testPaging() throws SQLException, MalformedURLException {

    CachedRowSet crs = null;
    this.con.setAutoCommit(false);

    try {/*from  w  w w .j a va2  s  .  co  m*/

        crs = new CachedRowSetImpl();
        crs.setUsername(settings.userName);
        crs.setPassword(settings.password);

        if (this.dbms.equals("mysql")) {
            crs.setUrl(settings.urlString + "?relaxAutoCommit=true");
        } else {
            crs.setUrl(settings.urlString);
        }
        crs.setCommand("select * from MERCH_INVENTORY");

        // Setting the page size to 4, such that we
        // get the data in chunks of 4 rows @ a time.
        crs.setPageSize(100);

        // Now get the first set of data
        crs.execute();

        crs.addRowSetListener(new ExampleRowSetListener());

        // Keep on getting data in chunks until done.

        int i = 1;
        do {
            System.out.println("Page number: " + i);
            while (crs.next()) {
                System.out.println("Found item " + crs.getInt("ITEM_ID") + ": " + crs.getString("ITEM_NAME"));
                if (crs.getInt("ITEM_ID") == 1235) {
                    int currentQuantity = crs.getInt("QUAN") + 1;
                    System.out.println("Updating quantity to " + currentQuantity);
                    crs.updateInt("QUAN", currentQuantity + 1);
                    crs.updateRow();
                    // Syncing the row back to the DB
                    crs.acceptChanges(con);
                }

            } // End of inner while
            i++;
        } while (crs.nextPage());
        // End of outer while

        // Inserting a new row
        // Doing a previous page to come back to the last page
        // as we ll be after the last page.

        int newItemId = 123456;

        if (this.doesItemIdExist(newItemId)) {
            System.out.println("Item ID " + newItemId + " already exists");
        } else {
            crs.previousPage();
            crs.moveToInsertRow();
            crs.updateInt("ITEM_ID", newItemId);
            crs.updateString("ITEM_NAME", "TableCloth");
            crs.updateInt("SUP_ID", 927);
            crs.updateInt("QUAN", 14);
            Calendar timeStamp;
            timeStamp = new GregorianCalendar();
            timeStamp.set(2006, 4, 1);
            crs.updateTimestamp("DATE_VAL", new Timestamp(timeStamp.getTimeInMillis()));
            crs.insertRow();
            crs.moveToCurrentRow();

            // Syncing the new row back to the database.
            System.out.println("About to add a new row...");
            crs.acceptChanges(con);
            System.out.println("Added a row...");
            this.viewTable(con);
        }
    } catch (SyncProviderException spe) {

        SyncResolver resolver = spe.getSyncResolver();

        Object crsValue; // value in the RowSet object
        Object resolverValue; // value in the SyncResolver object
        Object resolvedValue; // value to be persisted

        while (resolver.nextConflict()) {

            if (resolver.getStatus() == SyncResolver.INSERT_ROW_CONFLICT) {
                int row = resolver.getRow();
                crs.absolute(row);

                int colCount = crs.getMetaData().getColumnCount();
                for (int j = 1; j <= colCount; j++) {
                    if (resolver.getConflictValue(j) != null) {
                        crsValue = crs.getObject(j);
                        resolverValue = resolver.getConflictValue(j);

                        // Compare crsValue and resolverValue to determine
                        // which should be the resolved value (the value to persist)
                        //
                        // This example choses the value in the RowSet object,
                        // crsValue, to persist.,

                        resolvedValue = crsValue;

                        resolver.setResolvedValue(j, resolvedValue);
                    }
                }
            }
        }
    } catch (SQLException sqle) {
        JDBCTutorialUtilities.printSQLException(sqle);
    } finally {
        if (crs != null)
            crs.close();
        this.con.setAutoCommit(true);
    }

}