Example usage for javax.sql.rowset WebRowSet setCommand

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

Introduction

In this page you can find the example usage for javax.sql.rowset WebRowSet 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 = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();/*from   w ww .  java  2s.  c om*/

    String query = "select * from survey where 1 = 0";

    WebRowSet webRS = new WebRowSetImpl();
    webRS.setCommand(query);
    webRS.execute(conn);

    // convert xml to a String object
    StringWriter sw = new StringWriter();
    webRS.writeXml(sw);
    System.out.println(sw.toString());

    st.close();
    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')");

    String sqlQuery = "SELECT * FROM survey";
    WebRowSet webRS = new WebRowSetImpl();
    webRS.setCommand(sqlQuery);
    webRS.execute(conn);/*w ww.j a v  a2s  .  com*/

    // create RowSetMetaData object
    RowSetMetaData rsMD = (RowSetMetaData) webRS.getMetaData();
    System.out.println("rsMD=" + rsMD);
    if (rsMD == null) {
        System.out.println("vendor does not support RowSetMetaData");
    } else {
        int columnCount = rsMD.getColumnCount();
        System.out.println("columnCount=" + columnCount);
    }
    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("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    Statement stmt = conn.createStatement();
    String sqlQuery = "SELECT * FROM survey WHERE id='1'";
    WebRowSet webRS = new WebRowSetImpl();
    webRS.setCommand(sqlQuery);
    webRS.execute(conn);/*from w ww.ja va 2 s .c  o m*/

    File file = new File("1.xml");
    FileWriter fw = new FileWriter(file);
    System.out.println("Writing db data to file " + file.getAbsolutePath());
    webRS.writeXml(fw);

    StringWriter sw = new StringWriter();
    webRS.writeXml(sw);
    System.out.println(sw.toString());
    fw.flush();
    fw.close();
    stmt.close();
    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("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')");

    WebRowSet webRS;
    ResultSet rs = null;/*  w  w  w  .  j av  a 2 s .  c  o  m*/
    Statement stmt = null;
    stmt = conn.createStatement();
    webRS = null;
    String sqlQuery = "SELECT * FROM survey WHERE id='1'";
    webRS = new WebRowSetImpl();
    webRS.setCommand(sqlQuery);
    webRS.execute(conn);

    FileWriter fw = null;

    File file = new File("1.xml");
    fw = new FileWriter(file);
    System.out.println("Writing db data to file " + file.getAbsolutePath());
    webRS.writeXml(fw);

    // convert xml to a String object
    StringWriter sw = new StringWriter();
    webRS.writeXml(sw);
    System.out.println("==============");
    System.out.println(sw.toString());
    System.out.println("==============");
    fw.flush();
    fw.close();
    rs.close();
    stmt.close();
    conn.close();
}

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

public void testWebRowSet() throws SQLException, IOException {
    FileReader fReader = null;/* ww w  . j  a va  2  s. com*/
    FileWriter fWriter = null;
    String priceListFileName = "pricelist.xml";
    int[] keyCols = { 1 };
    WebRowSet priceList = new WebRowSetImpl();

    priceList.setUsername(settings.userName);
    priceList.setPassword(settings.password);
    priceList.setUrl(settings.urlString);
    priceList.setCommand("select COF_NAME, PRICE from COFFEES");
    priceList.setKeyColumns(keyCols);

    // Populate the WebRowSet
    priceList.execute();
    System.out.println("Size of the WebRowSet is: " + priceList.size());

    // Insert a new row
    priceList.moveToInsertRow();
    priceList.updateString("COF_NAME", "Kona");
    priceList.updateFloat("PRICE", 8.99f);
    priceList.insertRow();
    priceList.moveToCurrentRow();
    System.out.println("New row inserted");
    System.out.println("Size of the WebRowSet is: " + priceList.size());

    //Delete the row with "Espresso"
    priceList.beforeFirst();
    while (priceList.next()) {
        if (priceList.getString(1).equals("Espresso")) {
            System.out.println("Deleting row with Espresso...");
            priceList.deleteRow();
            break;
        }
    }

    // Update price of Colombian
    priceList.beforeFirst();
    while (priceList.next()) {
        if (priceList.getString(1).equals("Colombian")) {
            System.out.println("Updating row with Colombian...");
            priceList.updateFloat(2, 6.99f);
            priceList.updateRow();
            break;
        }
    }

    int size1 = priceList.size();
    fWriter = new FileWriter(priceListFileName);
    priceList.writeXml(fWriter);
    fWriter.flush();
    fWriter.close();

    // Create the receiving WebRowSet object
    WebRowSet receiver = new WebRowSetImpl();
    receiver.setUrl(settings.urlString);
    receiver.setUsername(settings.userName);
    receiver.setPassword(settings.password);

    //Now read the XML file.
    fReader = new FileReader(priceListFileName);
    receiver.readXml(fReader);
    int size2 = receiver.size();
    if (size1 == size2) {
        System.out.println("WebRowSet serialized and " + "deserialiazed properly");
    } else {
        System.out.println("Error....serializing/deserializng the WebRowSet");
    }
}