Example usage for javax.sql RowSet beforeFirst

List of usage examples for javax.sql RowSet beforeFirst

Introduction

In this page you can find the example usage for javax.sql RowSet beforeFirst.

Prototype

void beforeFirst() throws SQLException;

Source Link

Document

Moves the cursor to the front of this ResultSet object, just before the first row.

Usage

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

private void outputRowSet(RowSet rs) throws SQLException {
    rs.beforeFirst();
    while (rs.next()) {
        String coffeeName = rs.getString(1);
        int supplierID = rs.getInt(2);
        float price = rs.getFloat(3);
        int sales = rs.getInt(4);
        int total = rs.getInt(5);
        System.out.println(coffeeName + ", " + supplierID + ", " + price + ", " + sales + ", " + total);

    }//from w  w  w  . j a  v  a  2  s . com
}

From source file:org.aludratest.cloud.web.report.SqlBean.java

public synchronized void execute() {
    resultColumns = null;//from w w  w . j  a va 2  s.  co  m
    resultRows = null;

    if (sql == null) {
        return;
    }

    sql = sql.trim();
    while (sql.endsWith(";")) {
        sql = sql.substring(0, sql.length() - 1);
    }

    // easy basic check
    if (!sql.toLowerCase(Locale.US).startsWith("select ")) {
        FacesContext.getCurrentInstance().addMessage(null,
                JSFUtil.createErrorMessage("Only SELECT statements are allowed"));
        return;
    }

    try {
        RowSet rs = CloudManagerApplicationHolder.getInstance().getDatabase().populateQuery(sql);

        resultColumns = new ArrayList<String>();
        ResultSetMetaData meta = rs.getMetaData();
        for (int i = 1; i <= meta.getColumnCount(); i++) {
            resultColumns.add(meta.getColumnName(i));
        }

        rs.beforeFirst();
        resultRows = new ArrayList<Map<String, String>>();

        while (rs.next()) {
            Map<String, String> row = new HashMap<String, String>();

            // TODO nicer formatting etc.
            for (String col : resultColumns) {
                row.put(col, rs.getString(col));
            }

            resultRows.add(row);
        }
    } catch (SQLException se) {
        FacesContext.getCurrentInstance().addMessage(null,
                JSFUtil.createErrorMessage("SQLException: " + se.getMessage()));
    }
}