java - FilteredRowSet is not showing results -
i working on example using filteredrowset, trying run query, filter results using predicate object.
here code:
import javax.sql.rowset.filteredrowset; import oracle.jdbc.rowset.oraclefilteredrowset; public class example { public static void main(string[] args) throws sqlexception, ioexception { try (filteredrowset rs = new oraclefilteredrowset();) { rs.seturl("jdbc:oracle:thin:@localhost:1521:xe"); rs.setusername("dbuser"); rs.setpassword("dbpassword"); rs.setcommand("select * employees"); rs.execute(); string name[] = {"user1", "user2"}; rs.setfilter(new userfilter("lastname", name)); while(rs.next()){ string lname= rs.getstring("lastname"); system.out.println(lname); } } } }
here predicate class:
import javax.sql.rowset; import javax.sql.rowset.predicate;
public class userfilter implements predicate { private string[] names; private string colname = null; public userfilter(string colname, string[] names) { this.names = names; this.colname = colname; } @override public boolean evaluate(rowset arg0) { return false; } @override public boolean evaluate(object arg0, int arg1) throws sqlexception { return false; } @override public boolean evaluate(object valuearg, string colnamearg) throws sqlexception { if (colnamearg.equalsignorecase(this.colname)) { (int = 0; < this.names.length; i++) { if (this.names[i].equalsignorecase((string) valuearg)) { return true; } } } return false; } }
in database table employees
have records lastname
values user1
, user2
when apply filter shown in question, not getting output. mean code not entering while loop.
can please tell me how apply filter? expecting output contains records lastname
contains user1
or user2
you have implement logic of
public boolean evaluate(rowset arg0);
as best practice, filter should implement 3 evaluate()
methods.
in order avoid code redundancy in these 3 methods, can create private helper method contains actual logic of comparing col/row value ones filter, , have 3 evaluate()
methods call it. example at filter, not factorise code logic tough.
Comments
Post a Comment