Search in sources :

Example 1 with Row

use of org.adbcj.Row in project adbcj by mheath.

the class PgTest method main.

/**
	 * @param args
	 * @throws InterruptedException
	 * @throws org.adbcj.DbException
	 */
public static void main(String[] args) throws DbException, InterruptedException {
    ConnectionManager cm = ConnectionManagerProvider.createConnectionManager("adbcj:postgresql-netty://localhost/adbcjtck", "adbcjtck", "adbcjtck");
    Connection connection = cm.connect().get();
    final DbFuture<ResultSet> future = connection.executeQuery("SELECT * FROM simple_values");
    final DbFuture<ResultSet> future2 = connection.executeQuery("SELECT * FROM large");
    ResultSet rs = future.get();
    ResultSet rs2 = future2.get();
    for (Row row : rs) {
        System.out.println(row.get(0) + " " + row.get(1));
    }
    for (Row row : rs2) {
        System.out.println(row.get(0) + " " + row.get(1));
    }
    connection.close(true).get();
    cm.close(true).get();
    System.out.println("Closed");
}
Also used : ConnectionManager(org.adbcj.ConnectionManager) Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) Row(org.adbcj.Row)

Example 2 with Row

use of org.adbcj.Row in project adbcj by mheath.

the class SelectTest method testSimpleSelect.

public void testSimpleSelect() throws DbException, InterruptedException {
    final boolean[] callbacks = { false };
    final CountDownLatch latch = new CountDownLatch(callbacks.length);
    Connection connection = connectionManager.connect().get();
    try {
        ResultSet resultSet = connection.executeQuery("SELECT int_val, str_val FROM simple_values ORDER BY int_val").addListener(new DbListener<ResultSet>() {

            public void onCompletion(DbFuture<ResultSet> future) throws Exception {
                System.out.println("In callback");
                future.get().size();
                callbacks[0] = true;
                latch.countDown();
                System.out.println("Finished callback");
            }
        }).get();
        Assert.assertEquals(6, resultSet.size());
        Iterator<Row> i = resultSet.iterator();
        Row nullRow = null;
        Row row = i.next();
        if (row.get(0).isNull()) {
            nullRow = row;
            row = i.next();
        }
        Assert.assertEquals(row.get(0).getInt(), 0);
        Assert.assertEquals(row.get(1).getValue(), "Zero");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 1);
        Assert.assertEquals(row.get(1).getValue(), "One");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 2);
        Assert.assertEquals(row.get(1).getValue(), "Two");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 3);
        Assert.assertEquals(row.get(1).getValue(), "Three");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 4);
        Assert.assertEquals(row.get(1).getValue(), "Four");
        if (i.hasNext() && nullRow == null) {
            nullRow = i.next();
        }
        Assert.assertEquals(nullRow.get(0).getValue(), null);
        Assert.assertEquals(nullRow.get(1).getValue(), null);
        Assert.assertTrue(!i.hasNext(), "There were too many rows in result set");
        latch.await();
        Assert.assertTrue(callbacks[0], "Result set callback was not invoked");
    } finally {
        connection.close(true);
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) DbListener(org.adbcj.DbListener) Row(org.adbcj.Row) CountDownLatch(java.util.concurrent.CountDownLatch) DbFuture(org.adbcj.DbFuture)

Aggregations

Connection (org.adbcj.Connection)2 ResultSet (org.adbcj.ResultSet)2 Row (org.adbcj.Row)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 ConnectionManager (org.adbcj.ConnectionManager)1 DbFuture (org.adbcj.DbFuture)1 DbListener (org.adbcj.DbListener)1