Java examples for JDBC:FilteredRowSet
Use a FilteredRowSet to query the database and store the contents.
import com.sun.rowset.FilteredRowSetImpl; import java.sql.Connection; import java.sql.SQLException; import javax.sql.RowSet; import javax.sql.rowset.FilteredRowSet; import javax.sql.rowset.Predicate; public class Main { public static Connection conn = null; public static FilteredRowSet frs = null; public static void main(String[] args) throws Exception{ String[] authorArray = {"DEA", "JUNEAU"}; EmpFilter authorFilter = new EmpFilter(authorArray, 2); try {/*from ww w .ja va 2s. c o m*/ frs = new FilteredRowSetImpl(); frs.setCommand("SELECT TITLE, LASTNAME " + "FROM Emp BA, " + " Project AW, " + " BOOK B " + "WHERE AW.AUTHOR_ID = BA.ID " + "AND B.ID = AW.BOOK_ID"); frs.execute(conn); System.out.println("Prior to adding filter:"); viewRowSet(frs); System.out.println("Adding author filter:"); frs.beforeFirst(); frs.setFilter(authorFilter); viewRowSet(frs); } catch (SQLException e) { e.printStackTrace(); } } public static void viewRowSet(RowSet rs) { try { while (rs.next()) { System.out.println(rs.getString(1) + " - " + rs.getString(2)); } } catch (SQLException ex) { ex.printStackTrace(); } } } class EmpFilter implements Predicate { private String[] authors; private String colName = null; private int colNumber = -1; public EmpFilter(String[] authors, int colNumber) { this.authors = authors; this.colNumber = colNumber; this.colName = null; } @Override public boolean evaluate(Object value, String colName) { if (colName.equalsIgnoreCase(this.colName)) { for (String author : this.authors) { if (author.equalsIgnoreCase((String)value)) { return true; } } } return false; } @Override public boolean evaluate(Object value, int colNumber) { if (colNumber == this.colNumber) { for (String author : this.authors) { if (author.equalsIgnoreCase((String)value)) { return true; } } } return false; } @Override public boolean evaluate(RowSet rs) { if (rs == null) return false; try { for (int i = 0; i < this.authors.length; i++) { String v = null; if (this.colNumber > 0) { v = (String)rs.getObject(this.colNumber); } else { return false; } if (v.equalsIgnoreCase(authors[i])) { return true; } } } catch (SQLException e) { return false; } return false; } }