Example usage for io.vertx.pgclient PgConnection query

List of usage examples for io.vertx.pgclient PgConnection query

Introduction

In this page you can find the example usage for io.vertx.pgclient PgConnection query.

Prototype

PgConnection query(String sql, Handler<AsyncResult<RowSet<Row>>> handler);

Source Link

Usage

From source file:examples.PgClientExamples.java

License:Apache License

public void connecting05(Vertx vertx) {

    // Pool options
    PgConnectOptions options = new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db")
            .setUser("user").setPassword("secret");

    // Connect to Postgres
    PgConnection.connect(vertx, options, res -> {
        if (res.succeeded()) {

            System.out.println("Connected");

            // Obtain our connection
            PgConnection conn = res.result();

            // All operations execute on the same connection
            conn.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
                if (ar2.succeeded()) {
                    conn.query("SELECT * FROM users WHERE id='emad'", ar3 -> {
                        // Close the connection
                        conn.close();//from  w w  w.  ja va2  s .  c  o  m
                    });
                } else {
                    // Close the connection
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + res.cause().getMessage());
        }
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void pubsub01(PgConnection connection) {

    connection.notificationHandler(notification -> {
        System.out//www. j ava2s .c  o  m
                .println("Received " + notification.getPayload() + " on channel " + notification.getChannel());
    });

    connection.query("LISTEN some-channel", ar -> {
        System.out.println("Subscribed to channel");
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void cancelRequest(PgConnection connection) {
    connection.query("SELECT pg_sleep(20)", ar -> {
        if (ar.succeeded()) {
            // imagine this is a long query and is still running
            System.out.println("Query success");
        } else {//from ww w .  j  a v a  2 s .  c om
            // the server will abort the current query after cancelling request
            System.out.println("Failed to query due to " + ar.cause().getMessage());
        }
    });
    connection.cancelRequest(ar -> {
        if (ar.succeeded()) {
            System.out.println("Cancelling request has been sent");
        } else {
            System.out.println("Failed to send cancelling request");
        }
    });
}