Search in sources :

Example 1 with ResultList

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();
        }
    }
}
Also used : ResultList(org.apache.pivot.sql.ResultList) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Map(org.apache.pivot.collections.Map)

Aggregations

Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Map (org.apache.pivot.collections.Map)1 ResultList (org.apache.pivot.sql.ResultList)1