Search in sources :

Example 1 with JDBCClient

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());
                        }
                    });
                });
            });
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) SQLConnection(io.vertx.ext.sql.SQLConnection) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with JDBCClient

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());
                                }
                            });
                        });
                    });
                });
            });
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with JDBCClient

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());
                        }
                    });
                });
            });
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) SQLConnection(io.vertx.ext.sql.SQLConnection) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject)

Example 4 with JDBCClient

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);
}
Also used : JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.ext.web.Router) AuthProvider(io.vertx.ext.auth.AuthProvider)

Example 5 with JDBCClient

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;
}
Also used : JDBCClient(io.vertx.ext.jdbc.JDBCClient)

Aggregations

JDBCClient (io.vertx.ext.jdbc.JDBCClient)12 JsonObject (io.vertx.core.json.JsonObject)9 JsonArray (io.vertx.core.json.JsonArray)5 SQLConnection (io.vertx.ext.sql.SQLConnection)5 Vertx (io.vertx.core.Vertx)2 Suspendable (co.paralleluniverse.fibers.Suspendable)1 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Promise (io.vertx.core.Promise)1 Buffer (io.vertx.core.buffer.Buffer)1 Runner (io.vertx.example.util.Runner)1 AuthProvider (io.vertx.ext.auth.AuthProvider)1 JDBCAuth (io.vertx.ext.auth.jdbc.JDBCAuth)1 ResultSet (io.vertx.ext.sql.ResultSet)1 SQLRowStream (io.vertx.ext.sql.SQLRowStream)1 UpdateResult (io.vertx.ext.sql.UpdateResult)1