Example usage for javax.sql RowSetMetaData setTableName

List of usage examples for javax.sql RowSetMetaData setTableName

Introduction

In this page you can find the example usage for javax.sql RowSetMetaData setTableName.

Prototype

void setTableName(int columnIndex, String tableName) throws SQLException;

Source Link

Document

Sets the designated column's table name, if any, to the given String.

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;//from  w w w  .  j a  v a2  s . co m
    RowSetMetaData rsMD = new RowSetMetaDataImpl();
    rsMD.setColumnCount(2);
    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;/*from   w w  w  . ja  v  a  2s .co  m*/
    RowSetMetaData rsMD = new RowSetMetaDataImpl();
    rsMD.setColumnCount(2);
    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();
}