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')", insert -> {
// query some data
connection.query("select * from test", rs -> {
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;
}
// 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
rollbackTx(conn.result(), rollbackTrans -> {
// 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 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));
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')", insert -> {
// query some data
connection.query("select * from test", rs -> {
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 Server method start.
@Override
public void start() throws Exception {
// quick load of test data, this is a *sync* helper not intended for
// real deployments...
setUpInitialData("jdbc:hsqldb:mem:test?shutdown=true");
// Create a JDBC client with a test database
JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver"));
// If you are planning NOT to build a fat jar, then use the BoneCP pool since it
// can handle loading the jdbc driver classes from outside vert.x lib directory
// JDBCClient client = JDBCClient.createShared(vertx, new JsonObject()
// .put("provider_class", "io.vertx.ext.jdbc.spi.impl.BoneCPDataSourceProvider")
// .put("jdbcUrl", "jdbc:hsqldb:mem:test?shutdown=true")
// .put("username", "sa")
// .put("password", ""));
Router router = Router.router(vertx);
// We need cookies, sessions and request bodies
router.route().handler(CookieHandler.create());
router.route().handler(BodyHandler.create());
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
// Simple auth service which uses a JDBC data source
AuthProvider authProvider = JDBCAuth.create(vertx, client);
// We need a user session handler too to make sure the user is stored in the session between requests
router.route().handler(UserSessionHandler.create(authProvider));
// Any requests to URI starting '/private/' require login
router.route("/private/*").handler(RedirectAuthHandler.create(authProvider, "/loginpage.html"));
// Serve the static private pages from directory 'private'
router.route("/private/*").handler(StaticHandler.create().setCachingEnabled(false).setWebRoot("private"));
// Handles the actual login
router.route("/loginhandler").handler(FormLoginHandler.create(authProvider));
// Implement logout
router.route("/logout").handler(context -> {
context.clearUser();
// Redirect back to the index page
context.response().putHeader("location", "/").setStatusCode(302).end();
});
// Serve the non private static pages
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
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;
}
Aggregations