Example usage for io.vertx.sqlclient SqlConnection prepare

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

Introduction

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

Prototype

@Fluent
SqlConnection prepare(String sql, Handler<AsyncResult<PreparedQuery>> handler);

Source Link

Document

Create a prepared query.

Usage

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingConnections02(SqlConnection connection) {
    connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery pq = ar1.result();
            pq.execute(Tuple.of("julien"), ar2 -> {
                if (ar2.succeeded()) {
                    // All rows
                    RowSet<Row> rows = ar2.result();
                }/* www  .j av  a2 s  .  c om*/
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingConnections03(SqlConnection connection) {
    connection.prepare("INSERT INTO USERS (id, name) VALUES ($1, $2)", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery prepared = ar1.result();

            // Create a query : bind parameters
            List<Tuple> batch = new ArrayList();

            // Add commands to the createBatch
            batch.add(Tuple.of("julien", "Julien Viet"));
            batch.add(Tuple.of("emad", "Emad Alblueshi"));

            prepared.batch(batch, res -> {
                if (res.succeeded()) {

                    // Process rows
                    RowSet<Row> rows = res.result();
                } else {
                    System.out.println("Batch failed " + res.cause());
                }//  www  .  j ava2 s.  c  o m
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingCursors01(SqlConnection connection) {
    connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery pq = ar1.result();

            // Cursors require to run within a transaction
            Transaction tx = connection.begin();

            // Create a cursor
            Cursor cursor = pq.cursor(Tuple.of("julien"));

            // Read 50 rows
            cursor.read(50, ar2 -> {/*from   www  . j  a  v a 2s  .  c om*/
                if (ar2.succeeded()) {
                    RowSet<Row> rows = ar2.result();

                    // Check for more ?
                    if (cursor.hasMore()) {
                        // Repeat the process...
                    } else {
                        // No more rows - commit the transaction
                        tx.commit();
                    }
                }
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingCursors03(SqlConnection connection) {
    connection.prepare("SELECT * FROM users WHERE first_name LIKE $1", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery pq = ar1.result();

            // Streams require to run within a transaction
            Transaction tx = connection.begin();

            // Fetch 50 rows at a time
            RowStream<Row> stream = pq.createStream(50, Tuple.of("julien"));

            // Use the stream
            stream.exceptionHandler(err -> {
                System.out.println("Error: " + err.getMessage());
            });/* w  w  w . j  a v  a2 s. co m*/
            stream.endHandler(v -> {
                tx.commit();
                System.out.println("End of stream");
            });
            stream.handler(row -> {
                System.out.println("User: " + row.getString("last_name"));
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingConnections02(SqlConnection connection) {
    connection.prepare("SELECT * FROM users WHERE first_name LIKE ?", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery pq = ar1.result();
            pq.execute(Tuple.of("julien"), ar2 -> {
                if (ar2.succeeded()) {
                    // All rows
                    RowSet<Row> rows = ar2.result();
                }/*from   w ww .java2s . c  o  m*/
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingConnections03(SqlConnection connection) {
    connection.prepare("INSERT INTO USERS (id, name) VALUES (?, ?)", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery prepared = ar1.result();

            // Create a query : bind parameters
            List<Tuple> batch = new ArrayList();

            // Add commands to the createBatch
            batch.add(Tuple.of("julien", "Julien Viet"));
            batch.add(Tuple.of("emad", "Emad Alblueshi"));

            prepared.batch(batch, res -> {
                if (res.succeeded()) {

                    // Process rows
                    RowSet<Row> rows = res.result();
                } else {
                    System.out.println("Batch failed " + res.cause());
                }/*from w  ww .  jav  a 2s .c  o m*/
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingCursors01(SqlConnection connection) {
    connection.prepare("SELECT * FROM users WHERE age > ?", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery pq = ar1.result();

            // Create a cursor
            Cursor cursor = pq.cursor(Tuple.of(18));

            // Read 50 rows
            cursor.read(50, ar2 -> {/*  w w  w .j  av a2  s.c  o  m*/
                if (ar2.succeeded()) {
                    RowSet<Row> rows = ar2.result();

                    // Check for more ?
                    if (cursor.hasMore()) {
                        // Repeat the process...
                    } else {
                        // No more rows - close the cursor
                        cursor.close();
                    }
                }
            });
        }
    });
}

From source file:examples.SqlClientExamples.java

License:Apache License

public void usingCursors03(SqlConnection connection) {
    connection.prepare("SELECT * FROM users WHERE age > ?", ar1 -> {
        if (ar1.succeeded()) {
            PreparedQuery pq = ar1.result();

            // Fetch 50 rows at a time
            RowStream<Row> stream = pq.createStream(50, Tuple.of(18));

            // Use the stream
            stream.exceptionHandler(err -> {
                System.out.println("Error: " + err.getMessage());
            });/*w w w  .j a  va2  s.  com*/
            stream.endHandler(v -> {
                System.out.println("End of stream");
            });
            stream.handler(row -> {
                System.out.println("User: " + row.getString("last_name"));
            });
        }
    });
}