Example usage for javax.sql.rowset CachedRowSet setMetaData

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

Introduction

In this page you can find the example usage for javax.sql.rowset CachedRowSet setMetaData.

Prototype

public void setMetaData(RowSetMetaData md) throws SQLException;

Source Link

Document

Sets the metadata for this CachedRowSet object with the given RowSetMetaData object.

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);//from   www. 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  .jav  a 2  s.com*/
    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: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 ww  . j  a v a  2 s.  c  om
  }

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