Selecting SQL Rows Through JDBC
This is how you fetch all rows from an SQL SELECT
query:
public class Main { public static void main(String[] args) throws SQLException { Collection<String> names = new JdbcSession(source) .sql("SELECT name FROM employee WHERE salary > 35000") .select( new Outcome<Collection<String>>() { @Override public Collection<String> handle(ResultSet rset) throws SQLException { final Collection<String> names = new LinkedList<String>(); while (rset.next()) { names.add(rset.getString(1)); } return names; } } ); } }
There is an off-the-shelf SingleOutcome
Outcome, which can be used if you interested in the first value of the first row of the SELECT
query result set:
public class Main { public static void main(String[] args) throws SQLException { String name = new JdbcSession(source) .sql("SELECT name FROM employee WHERE name = ?") .set("Jeff Bridges") .select(new SingleOutcome<String>(String.class)); } }