Search in sources :

Example 1 with Pool

use of io.vertx.sqlclient.Pool in project vertx-examples by vert-x3.

the class SqlClientExample method start.

@Override
public void start() {
    Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
    // Uncomment for MySQL
    // Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
    // .setPort(5432)
    // .setHost("the-host")
    // .setDatabase("the-db")
    // .setUser("user")
    // .setPassword("secret"), new PoolOptions().setMaxSize(4));
    pool.getConnection(res1 -> {
        if (res1.failed()) {
            System.err.println(res1.cause().getMessage());
            return;
        }
        SqlConnection connection = res1.result();
        // create a test table
        connection.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
            if (res2.failed()) {
                connection.close();
                System.err.println("Cannot create the table");
                res2.cause().printStackTrace();
                return;
            }
            // insert some test data
            connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
                // query some data with arguments
                connection.preparedQuery("select * from test where id = ?").execute(Tuple.of(2), rs -> {
                    if (rs.failed()) {
                        System.err.println("Cannot retrieve the data from the database");
                        rs.cause().printStackTrace();
                        return;
                    }
                    for (Row line : rs.result()) {
                        System.out.println("" + line);
                    }
                    // and close the connection
                    connection.close();
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) SqlConnection(io.vertx.sqlclient.SqlConnection) Row(io.vertx.sqlclient.Row)

Example 2 with Pool

use of io.vertx.sqlclient.Pool in project vertx-examples by vert-x3.

the class SqlClientExample method start.

@Override
public void start() throws Exception {
    Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
    // Uncomment for MySQL
    // Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
    // .setPort(5432)
    // .setHost("the-host")
    // .setDatabase("the-db")
    // .setUser("user")
    // .setPassword("secret"), new PoolOptions().setMaxSize(4));
    pool.getConnection(res1 -> {
        if (res1.failed()) {
            System.err.println(res1.cause().getMessage());
            return;
        }
        SqlConnection connection = res1.result();
        // create a test table
        connection.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
            if (res2.failed()) {
                connection.close();
                System.err.println("Cannot create the table");
                res2.cause().printStackTrace();
                return;
            }
            // insert some test data
            connection.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
                // query some data with arguments
                connection.query("select * from test").execute(rs -> {
                    if (rs.failed()) {
                        System.err.println("Cannot retrieve the data from the database");
                        rs.cause().printStackTrace();
                        return;
                    }
                    for (Row line : rs.result()) {
                        System.out.println("" + line);
                    }
                    // and close the connection
                    connection.close();
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) SqlConnection(io.vertx.sqlclient.SqlConnection) Row(io.vertx.sqlclient.Row)

Example 3 with Pool

use of io.vertx.sqlclient.Pool in project vertx-examples by vert-x3.

the class SqlClientExample method start.

@Override
public void start() {
    Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
    // Uncomment for MySQL
    // Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
    // .setPort(5432)
    // .setHost("the-host")
    // .setDatabase("the-db")
    // .setUser("user")
    // .setPassword("secret"), new PoolOptions().setMaxSize(4));
    pool.begin(res1 -> {
        if (res1.failed()) {
            System.err.println(res1.cause().getMessage());
            return;
        }
        Transaction tx = res1.result();
        // create a test table
        tx.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
            if (res2.failed()) {
                tx.close();
                System.err.println("Cannot create the table");
                res2.cause().printStackTrace();
                return;
            }
            // insert some test data
            tx.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
                // rollback transaction
                tx.rollback(res4 -> {
                    // query some data with arguments
                    pool.query("select * from test").execute(rs -> {
                        if (rs.failed()) {
                            System.err.println("Cannot retrieve the data from the database");
                            rs.cause().printStackTrace();
                            return;
                        }
                        for (Row line : rs.result()) {
                            System.out.println("" + line);
                        }
                    });
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) Transaction(io.vertx.sqlclient.Transaction) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) Row(io.vertx.sqlclient.Row)

Example 4 with Pool

use of io.vertx.sqlclient.Pool in project vertx-examples by vert-x3.

the class SqlClientExample method start.

@Override
public void start() {
    Pool pool = PgPool.pool(vertx, new PgConnectOptions().setPort(5432).setHost("the-host").setDatabase("the-db").setUser("user").setPassword("secret"), new PoolOptions().setMaxSize(4));
    // Uncomment for MySQL
    // Pool pool = MySQLPool.pool(vertx, new MySQLConnectOptions()
    // .setPort(5432)
    // .setHost("the-host")
    // .setDatabase("the-db")
    // .setUser("user")
    // .setPassword("secret"), new PoolOptions().setMaxSize(4));
    pool.begin(res1 -> {
        if (res1.failed()) {
            System.err.println(res1.cause().getMessage());
            return;
        }
        Transaction tx = res1.result();
        // create a test table
        tx.query("create table test(id int primary key, name varchar(255))").execute(res2 -> {
            if (res2.failed()) {
                tx.close();
                System.err.println("Cannot create the table");
                res2.cause().printStackTrace();
                return;
            }
            // insert some test data
            tx.query("insert into test values (1, 'Hello'), (2, 'World')").execute(res3 -> {
                // query some data with arguments
                tx.query("select * from test").execute(rs -> {
                    if (rs.failed()) {
                        System.err.println("Cannot retrieve the data from the database");
                        rs.cause().printStackTrace();
                        return;
                    }
                    for (Row line : rs.result()) {
                        System.out.println("" + line);
                    }
                    // and close the connection
                    tx.commit();
                });
            });
        });
    });
}
Also used : PgConnectOptions(io.vertx.pgclient.PgConnectOptions) Transaction(io.vertx.sqlclient.Transaction) PoolOptions(io.vertx.sqlclient.PoolOptions) PgPool(io.vertx.pgclient.PgPool) Pool(io.vertx.sqlclient.Pool) Row(io.vertx.sqlclient.Row)

Aggregations

PgConnectOptions (io.vertx.pgclient.PgConnectOptions)4 PgPool (io.vertx.pgclient.PgPool)4 Pool (io.vertx.sqlclient.Pool)4 PoolOptions (io.vertx.sqlclient.PoolOptions)4 Row (io.vertx.sqlclient.Row)4 SqlConnection (io.vertx.sqlclient.SqlConnection)2 Transaction (io.vertx.sqlclient.Transaction)2