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();
});
});
});
});
}
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();
});
});
});
});
}
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);
}
});
});
});
});
});
}
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();
});
});
});
});
}
Aggregations