Example usage for io.vertx.sqlclient SqlConnection query

List of usage examples for io.vertx.sqlclient SqlConnection query

Introduction

In this page you can find the example usage for io.vertx.sqlclient SqlConnection query.

Prototype

@Override
    SqlConnection query(String sql, Handler<AsyncResult<RowSet<Row>>> handler);

Source Link

Usage

From source file:examples.MySQLClientExamples.java

public void connecting04(Vertx vertx) {

    // Connect options
    MySQLConnectOptions connectOptions = new MySQLConnectOptions().setPort(3306).setHost("the-host")
            .setDatabase("the-db").setUser("user").setPassword("secret");

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pooled client
    MySQLPool client = MySQLPool.pool(vertx, connectOptions, poolOptions);

    // Get a connection from the pool
    client.getConnection(ar1 -> {// w w w  .  ja  v a2 s .co m

        if (ar1.succeeded()) {

            System.out.println("Connected");

            // Obtain our connection
            SqlConnection conn = ar1.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 -> {
                        // Release the connection to the pool
                        conn.close();
                    });
                } else {
                    // Release the connection to the pool
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + ar1.cause().getMessage());
        }
    });
}

From source file:examples.PgClientExamples.java

License:Apache License

public void connecting04(Vertx vertx) {

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

    // Pool options
    PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

    // Create the pooled client
    PgPool client = PgPool.pool(vertx, connectOptions, poolOptions);

    // Get a connection from the pool
    client.getConnection(ar1 -> {/*from w  w w  .  j  a  va2  s.  c  o  m*/

        if (ar1.succeeded()) {

            System.out.println("Connected");

            // Obtain our connection
            SqlConnection conn = ar1.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 -> {
                        // Release the connection to the pool
                        conn.close();
                    });
                } else {
                    // Release the connection to the pool
                    conn.close();
                }
            });
        } else {
            System.out.println("Could not connect: " + ar1.cause().getMessage());
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingConnections01(Vertx vertx, Pool pool) {

    pool.getConnection(ar1 -> {//from w  ww .  ja  v  a2 s . c o m
        if (ar1.succeeded()) {
            SqlConnection connection = ar1.result();

            connection.query("SELECT * FROM users WHERE id='julien'", ar2 -> {
                if (ar1.succeeded()) {
                    connection.query("SELECT * FROM users WHERE id='paulo'", ar3 -> {
                        // Do something with rows and return the connection to the pool
                        connection.close();
                    });
                } else {
                    // Return the connection to the pool
                    connection.close();
                }
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void transaction01(Pool pool) {
    pool.getConnection(res -> {/*w w w.j a va 2s  .  c  o m*/
        if (res.succeeded()) {

            // Transaction must use a connection
            SqlConnection conn = res.result();

            // Begin the transaction
            Transaction tx = conn.begin();

            // Various statements
            conn.query("INSERT INTO Users (first_name,last_name) VALUES ('Julien','Viet')", ar1 -> {
                if (ar1.succeeded()) {
                    conn.query("INSERT INTO Users (first_name,last_name) VALUES ('Emad','Alblueshi')", ar2 -> {
                        if (ar2.succeeded()) {
                            // Commit the transaction
                            tx.commit(ar3 -> {
                                if (ar3.succeeded()) {
                                    System.out.println("Transaction succeeded");
                                } else {
                                    System.out.println("Transaction failed " + ar3.cause().getMessage());
                                }
                                // Return the connection to the pool
                                conn.close();
                            });
                        } else {
                            // Return the connection to the pool
                            conn.close();
                        }
                    });
                } else {
                    // Return the connection to the pool
                    conn.close();
                }
            });
        }
    });
}