3.2. Use the JDBC 4.1 RowSetProvider, RowSetFactory, and new RowSet interfaces - Source Code

Code:

package c3.s2;

import java.sql.SQLException;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

/**
 * @author Mikalai Zaikin
 */
public class RowSetTest {

    public static final String URL = "jdbc:derby://localhost/d:/workspace/ocpjp7-upgrade-samples/sample";

    private void doIt() throws SQLException {

        RowSetFactory myRowSetFactory = RowSetProvider.newFactory();
        try (JdbcRowSet jdbcRs = myRowSetFactory.createJdbcRowSet();) {

            jdbcRs.setUrl(URL);
            jdbcRs.setCommand("SELECT * FROM PERSON");
            jdbcRs.execute();

            while (jdbcRs.next()) {
                int id = jdbcRs.getInt("ID");
                String name = jdbcRs.getString("NAME");
                System.out.println("ID: " + id);
                System.out.println("NAME: " + name);
                System.out.println();
            }
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) throws Exception {
        RowSetTest test = new RowSetTest();
        test.doIt();
    }
}
					

output:

ID: 1
NAME: Mikalai Zaikin

ID: 2
NAME: Volha Zaikina

ID: 3
NAME: Anastasia Zaikina

ID: 4
NAME: Daria Zaikina

ID: 5
NAME: Ivan Zaikin
					

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Free SCDJWS 5.0 Guide