use of io.vertx.ext.jdbc.JDBCClient in project vertx-auth by vert-x3.
the class JDBCAuthOptions method createProvider.
@Override
public JDBCAuth createProvider(Vertx vertx) {
JDBCClient client;
if (shared) {
if (datasourceName != null) {
client = JDBCClient.createShared(vertx, config, datasourceName);
} else {
client = JDBCClient.createShared(vertx, config);
}
} else {
client = JDBCClient.createNonShared(vertx, config);
}
JDBCAuth auth = JDBCAuth.create(vertx, client);
if (authenticationQuery != null) {
auth.setAuthenticationQuery(authenticationQuery);
}
if (rolesQuery != null) {
auth.setRolesQuery(rolesQuery);
}
if (permissionsQuery != null) {
auth.setPermissionsQuery(permissionsQuery);
}
if (rolesPrefix != null) {
auth.setRolePrefix(rolesPrefix);
}
return auth;
}
use of io.vertx.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class JDBCExample method start.
@Override
public void start() throws Exception {
final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
client.getConnection(conn -> {
if (conn.failed()) {
System.err.println(conn.cause().getMessage());
return;
}
final SQLConnection connection = conn.result();
// create a test table
connection.execute("create table test(id int primary key, name varchar(255))", create -> {
if (create.failed()) {
System.err.println("Cannot create the table");
create.cause().printStackTrace();
return;
}
// insert some test data
connection.execute("insert into test values (1, 'Hello'), (2, 'World')", insert -> {
// query some data with arguments
connection.queryWithParams("select * from test where id = ?", new JsonArray().add(2), rs -> {
if (rs.failed()) {
System.err.println("Cannot retrieve the data from the database");
rs.cause().printStackTrace();
return;
}
for (JsonArray line : rs.result().getResults()) {
System.out.println(line.encode());
}
// and close the connection
connection.close(done -> {
if (done.failed()) {
throw new RuntimeException(done.cause());
}
});
});
});
});
});
}
use of io.vertx.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class JDBCExample method start.
@Override
public void start() throws Exception {
final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
client.getConnection(conn -> {
if (conn.failed()) {
System.err.println(conn.cause().getMessage());
return;
}
final SQLConnection connection = conn.result();
connection.execute("create table test(id int primary key, name varchar(255))", res -> {
if (res.failed()) {
throw new RuntimeException(res.cause());
}
// insert some test data
connection.execute("insert into test values (1, 'Hello'), (2, 'Goodbye'), (3, 'Cya Later')", insert -> {
// query some data
connection.queryStream("select * from test", stream -> {
if (stream.succeeded()) {
SQLRowStream sqlRowStream = stream.result();
sqlRowStream.handler(row -> {
// do something with the row...
System.out.println(row.encode());
}).endHandler(v -> {
// no more data available, close the connection
connection.close(done -> {
if (done.failed()) {
throw new RuntimeException(done.cause());
}
});
});
}
});
});
});
});
}
use of io.vertx.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class JDBCExample method start.
@Override
public void start() throws Exception {
final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
client.getConnection(conn -> {
if (conn.failed()) {
System.err.println(conn.cause().getMessage());
return;
}
// create a test table
execute(conn.result(), "create table test(id int primary key, name varchar(255))", create -> {
// start a transaction
startTx(conn.result(), beginTrans -> {
// insert some test data
execute(conn.result(), "insert into test values(1, 'Hello')", insert -> {
// commit data
endTx(conn.result(), commitTrans -> {
// query some data
query(conn.result(), "select count(*) from test", rs -> {
for (JsonArray line : rs.getResults()) {
System.out.println(line.encode());
}
// and close the connection
conn.result().close(done -> {
if (done.failed()) {
throw new RuntimeException(done.cause());
}
});
});
});
});
});
});
});
}
use of io.vertx.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class Client method start.
@Override
@Suspendable
public void start() throws Exception {
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
JDBCClient jdbc = JDBCClient.createShared(vertx, config);
try (SQLConnection conn = awaitResult(jdbc::getConnection)) {
// Create a table
Void v = awaitResult(h -> conn.execute("CREATE TABLE test(col VARCHAR(20))", h));
// Insert some stuff
for (int i = 0; i < 10; i++) {
int ii = i;
UpdateResult res = awaitResult(h -> conn.update("INSERT INTO test (col) VALUES ('val" + ii + "')", h));
System.out.println("Rows updated: " + res.getUpdated());
}
// Select the results
ResultSet res = awaitResult(h -> conn.query("SELECT * FROM test", h));
System.out.println("Selected " + res.getNumRows() + " results");
res.getResults().forEach(System.out::println);
}
}
Aggregations