Example usage for javax.sql.rowset CachedRowSet setCommand

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

Introduction

In this page you can find the example usage for javax.sql.rowset CachedRowSet 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 {
    CachedRowSet rs;
    String ROWSET_IMPL_CLASS = "com.sun.rowset.CachedRowSetImpl";

    Class c = Class.forName(ROWSET_IMPL_CLASS);
    rs = (CachedRowSet) c.newInstance();

    rs.setUrl("jdbc:postgresql:dbname");
    rs.setUsername("username");
    rs.setPassword("password");

    rs.setCommand("select * from members where name like ?");
    rs.setString(1, "I%");

    rs.execute();/*from www.  ja v a  2  s .c  o  m*/

    while (rs.next()) {
        if (rs.getInt("id") == 42) {
            rs.setString(1, "newString");
            rs.updateRow(); // Normal JDBC

            rs.acceptChanges();
        }
    }
    rs.close();
}

From source file:CachedRS.java

public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(CRS_FILE_LOC);
    ObjectInputStream in = new ObjectInputStream(fis);
    CachedRowSet crs = (CachedRowSet) in.readObject();
    fis.close();/*from w w  w  . j a v  a  2  s . c  o m*/
    in.close();

    Class.forName("oracle.jdbc.driver.OracleDriver");
    crs.setUrl("jdbc:oracle:thin:@localhost:1521:ORCL");
    crs.setUsername("yourName");
    crs.setPassword("mypwd");
    String sql = "SELECT SSN, Name, Salary, Hiredate FROM Employees WHERE SSN=?";
    crs.setCommand(sql);
    crs.setInt(1, 111111111);
    crs.execute();

    FileOutputStream fos = new FileOutputStream(CRS_FILE_LOC);
    ObjectOutputStream out = new ObjectOutputStream(fos);
    out.writeObject(crs);
    out.close();
    crs.close();

    fis = new FileInputStream(CRS_FILE_LOC);
    in = new ObjectInputStream(fis);
    crs = (CachedRowSet) in.readObject();
    fis.close();
    in.close();

    while (crs.next()) {
        System.out.print("SSN: " + crs.getInt("ssn"));
        System.out.print(", Name: " + crs.getString("name"));
        System.out.print(", Salary: $" + crs.getDouble("salary"));
        System.out.print(", HireDate: " + crs.getDate("hiredate"));
    }
    crs.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    CachedRowSet rs;

    // Create the class with class.forName to avoid importing
    // from the unsupported com.sun packages.
    Class c = Class.forName(ROWSET_IMPL_CLASS);
    rs = (CachedRowSet) c.newInstance();

    rs.setUrl("jdbc:postgresql:tmclub");
    rs.setUsername("ian");
    rs.setPassword("secret");

    rs.setCommand("select * from members where name like ?");
    rs.setString(1, "I%");

    // This will cause the RowSet to connect, fetch its data, and
    // disconnect
    rs.execute();/*from  w ww.jav  a2  s  .  c o  m*/

    // Some time later, the client tries to do something.

    // Suppose we want to update data:
    while (rs.next()) {
        if (rs.getInt("id") == 42) {
            rs.setString(1, "Marvin");
            rs.updateRow(); // Normal JDBC

            // This additional call tells the CachedRowSet to connect
            // to its database and send the updated data back.
            rs.acceptChanges();
        }
    }

    // If we're all done...
    rs.close();
}

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

public void testJoinRowSet(String supplierName) throws SQLException {

    CachedRowSet coffees = null;
    CachedRowSet suppliers = null;
    JoinRowSet jrs = null;/*from  w w w.  jav  a  2s.  c  o  m*/

    try {
        coffees = new CachedRowSetImpl();
        coffees.setCommand("SELECT * FROM COFFEES");
        coffees.setUsername(settings.userName);
        coffees.setPassword(settings.password);
        coffees.setUrl(settings.urlString);
        coffees.execute();

        suppliers = new CachedRowSetImpl();
        suppliers.setCommand("SELECT * FROM SUPPLIERS");
        suppliers.setUsername(settings.userName);
        suppliers.setPassword(settings.password);
        suppliers.setUrl(settings.urlString);
        suppliers.execute();

        jrs = new JoinRowSetImpl();
        jrs.addRowSet(coffees, "SUP_ID");
        jrs.addRowSet(suppliers, "SUP_ID");

        System.out.println("Coffees bought from " + supplierName + ": ");
        while (jrs.next()) {
            if (jrs.getString("SUP_NAME").equals(supplierName)) {
                String coffeeName = jrs.getString(1);
                System.out.println("     " + coffeeName);
            }
        }
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (jrs != null) {
            jrs.close();
        }
        if (suppliers != null) {
            suppliers.close();
        }
        if (coffees != null) {
            coffees.close();
        }
    }
}

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

}

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

public CachedRowSet getContentsOfCoffeesTable() throws SQLException {
    CachedRowSet crs = null;
    try {//from www. j av  a 2  s . co m
        connection = settings.getConnection();
        crs = new CachedRowSetImpl();
        crs.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
        crs.setConcurrency(ResultSet.CONCUR_UPDATABLE);
        crs.setUsername(settings.userName);
        crs.setPassword(settings.password);

        // In MySQL, to disable auto-commit, set the property relaxAutoCommit to
        // true in the connection URL.

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

        // Regardless of the query, fetch the contents of COFFEES

        crs.setCommand("select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES");
        crs.execute();

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

From source file:com.egt.core.db.xdp.RecursoCachedRowSetDataProvider.java

private void setComandoCachedRowSet(String comando, boolean refresh, long version) throws ExcepcionAplicacion {
    Bitacora.trace(this.getClass(), "setComandoCachedRowSet", comando, "refresh=" + refresh);
    if (StringUtils.isNotBlank(comando)) {
        CachedRowSet crs = this.getCachedRowSet();
        Bitacora.trace(this.getClass(), "setComandoCachedRowSet",
                crs == null ? "CachedRowSet=?" : "CachedRowSet=" + crs);
        if (crs != null) {
            try {
                InterpreteSql interprete = TLC.getInterpreteSql();
                if (interprete != null) {
                    String command = crs.getCommand();
                    String comandoSelect = interprete.getComandoSelect(comando,
                            this.getLimiteFilasFuncionSelect());
                    if (comandoSelect != null && !comandoSelect.equals(command)) {
                        crs.setCommand(comandoSelect); /* throws SQLException */
                        this.setComandoSelectAnsi(comando);
                        this.incrementaVersionComandoSelect();
                        this.setVersionComandoInsert(version);
                    }//from   www.  j a va2 s .  c  om
                    if (refresh) {
                        this.refresh(); /* throws DataProviderException */
                    }
                }
            } catch (SQLException | DataProviderException ex) {
                TLC.getBitacora().fatal(ex);
                throw new ExcepcionAplicacion(ex);
            }
        }
    }
}

From source file:ips1ap101.lib.core.db.xdp.RecursoCachedRowSetDataProvider.java

private void setComandoCachedRowSet(String comando, boolean refresh, long version) throws ExcepcionAplicacion {
    Bitacora.trace(getClass(), "setComandoCachedRowSet", comando, "refresh=" + refresh);
    if (StringUtils.isNotBlank(comando)) {
        CachedRowSet crs = getCachedRowSet();
        Bitacora.trace(getClass(), "setComandoCachedRowSet",
                crs == null ? "CachedRowSet=?" : "CachedRowSet=" + crs);
        if (crs != null) {
            try {
                InterpreteSql interprete = TLC.getInterpreteSql();
                if (interprete != null) {
                    String command = crs.getCommand();
                    String comandoSelect = interprete.getComandoSelect(comando, getLimiteFilasFuncionSelect());
                    if (comandoSelect != null && !comandoSelect.equals(command)) {
                        crs.setCommand(comandoSelect); /* throws SQLException */
                        setComandoSelectAnsi(comando);
                        incrementaVersionComandoSelect();
                        setVersionComandoInsert(version);
                    }/* www.  java2 s.com*/
                    if (refresh) {
                        refresh(); /* throws DataProviderException */
                    }
                }
            } catch (SQLException | DataProviderException ex) {
                TLC.getBitacora().fatal(ex);
                throw new ExcepcionAplicacion(ex);
            }
        }
    }
}