use of org.apache.pivot.sql.ResultList in project pivot by apache.
the class ResultListTest method main.
public static void main(String[] args) throws Exception {
// e.g. jdbc:mysql://localhost/test
String connectionURL = args[0];
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
ResultList resultList = null;
try {
connection = DriverManager.getConnection(connectionURL);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM result_list_test");
resultList = new ResultList(resultSet);
resultList.setFields(new ResultList.Field("i"), new ResultList.Field("f"), new ResultList.Field("s"), new ResultList.Field("b"));
out.println(JSONSerializer.toString(resultList));
resultSet = statement.executeQuery("SELECT * FROM result_list_test");
resultList = new ResultList(resultSet);
resultList.setFields(new ResultList.Field("i", "integer"), new ResultList.Field("f", "float"), new ResultList.Field("s", "string"), new ResultList.Field("b", "boolean"));
out.println(JSONSerializer.toString(resultList));
resultSet = statement.executeQuery("SELECT * FROM result_list_test");
resultList = new ResultList(resultSet);
resultList.setFields(new ResultList.Field("i", "integer", Integer.class), new ResultList.Field("f", "float", Float.class), new ResultList.Field("s", "string", String.class), new ResultList.Field("b", "boolean", Boolean.class));
out.println(JSONSerializer.toString(resultList));
// Test forward and backward iteration
resultSet = statement.executeQuery("SELECT * FROM result_list_test");
resultList = new ResultList(resultSet);
resultList.setFields(new ResultList.Field("i"), new ResultList.Field("f"), new ResultList.Field("s"), new ResultList.Field("b"));
Iterator<Map<String, Object>> iterator = resultList.iterator();
while (iterator.hasNext()) {
out.println(JSONSerializer.toString(iterator.next()));
}
} finally {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
Aggregations