private void startTx(SQLConnection conn, Handler<ResultSet> done) { conn.setAutoCommit(false, res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(null); }); }
private void execute(SQLConnection conn, String sql, Handler<Void> done) { conn.execute(sql, res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(null); }); }
private void rollbackTx(SQLConnection conn, Handler<ResultSet> done) { conn.rollback(res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(null); }); } }
@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); // Get a connection 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); } }
connection.execute("create table test(id int primary key, name varchar(255))", res -> { if (res.failed()) { resultHandler.handle(Future.failedFuture(res.cause())); connection.execute("insert into test values(1, 'Hello')", insert -> { connection.query("select * from test", rs -> { results.addAll(rs.result().getResults().stream().map(JsonArray::encode).collect(Collectors.toList())); connection.close(done -> { if (done.failed()) { resultHandler.handle(Future.failedFuture(done.cause()));
connection.execute("create table test(id int primary key, name varchar(255))", create -> { if (create.failed()) { System.err.println("Cannot create the table"); connection.execute("insert into test values (1, 'Hello'), (2, 'World')", insert -> { 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"); for (JsonArray line : rs.result().getResults()) { System.out.println(line.encode()); connection.close(done -> { if (done.failed()) { throw new RuntimeException(done.cause());
connection.execute("create table test(id int primary key, name varchar(255))", res -> { if (res.failed()) { throw new RuntimeException(res.cause()); connection.execute("insert into test values (1, 'Hello'), (2, 'Goodbye'), (3, 'Cya Later')", insert -> { connection.queryStream("select * from test", stream -> { if (stream.succeeded()) { SQLRowStream sqlRowStream = stream.result(); .handler(row -> { .endHandler(v -> { connection.close(done -> { if (done.failed()) { throw new RuntimeException(done.cause());
private void handleGetProduct(RoutingContext routingContext) { String productID = routingContext.request().getParam("productID"); HttpServerResponse response = routingContext.response(); if (productID == null) { sendError(400, response); } else { SQLConnection conn = routingContext.get("conn"); conn.queryWithParams("SELECT id, name, price, weight FROM products where id = ?", new JsonArray().add(Integer.parseInt(productID)), query -> { if (query.failed()) { sendError(500, response); } else { if (query.result().getNumRows() == 0) { sendError(404, response); } else { response.putHeader("content-type", "application/json").end(query.result().getRows().get(0).encode()); } } }); } }
for (JsonArray line : rs.getResults()) { System.out.println(line.encode()); conn.result().close(done -> { if (done.failed()) { throw new RuntimeException(done.cause());
private void handleListProducts(RoutingContext routingContext) { HttpServerResponse response = routingContext.response(); SQLConnection conn = routingContext.get("conn"); conn.query("SELECT id, name, price, weight FROM products", query -> { if (query.failed()) { sendError(500, response); } else { JsonArray arr = new JsonArray(); query.result().getRows().forEach(arr::add); routingContext.response().putHeader("content-type", "application/json").end(arr.encode()); } }); }
@Override 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); // Connect to the database jdbc.rxGetConnection().flatMap(conn -> { // Now chain some statements using flatmap composition Single<ResultSet> resa = conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))") .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")) .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")) .flatMap(result -> conn.rxQuery("SELECT * FROM test")); return resa.doAfterTerminate(conn::close); }).subscribe(resultSet -> { // Subscribe to the final result System.out.println("Results : " + resultSet.getRows()); }, err -> { System.out.println("Database problem"); err.printStackTrace(); }); } }
private void handleAddProduct(RoutingContext routingContext) { HttpServerResponse response = routingContext.response(); SQLConnection conn = routingContext.get("conn"); JsonObject product = routingContext.getBodyAsJson(); conn.updateWithParams("INSERT INTO products (name, price, weight) VALUES (?, ?, ?)", new JsonArray().add(product.getString("name")).add(product.getFloat("price")).add(product.getInteger("weight")), query -> { if (query.failed()) { sendError(500, response); } else { response.end(); } }); }
private void query(SQLConnection conn, String sql, Handler<ResultSet> done) { conn.query(sql, res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(res.result()); }); }
private void endTx(SQLConnection conn, Handler<ResultSet> done) { conn.commit(res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(null); }); } }
connection.execute("create table test(id int primary key, name varchar(255))", res -> { if (res.failed()) { throw new RuntimeException(res.cause()); connection.execute("insert into test values(1, 'Hello')", insert -> { connection.query("select * from test", rs -> { for (JsonArray line : rs.result().getResults()) { System.out.println(line.encode()); connection.close(done -> { if (done.failed()) { throw new RuntimeException(done.cause());
for (JsonArray line : rs.getResults()) { System.out.println(line.encode()); conn.result().close(done -> { if (done.failed()) { throw new RuntimeException(done.cause());
private void execute(SQLConnection conn, String sql, Handler<Void> done) { conn.execute(sql, res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(null); }); }
private void query(SQLConnection conn, String sql, Handler<ResultSet> done) { conn.query(sql, res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(res.result()); }); }
private void startTx(SQLConnection conn, Handler<ResultSet> done) { conn.setAutoCommit(false, res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } done.handle(null); }); }
private void setUpInitialData(Handler<Void> done) { client.getConnection(res -> { if (res.failed()) { throw new RuntimeException(res.cause()); } final SQLConnection conn = res.result(); conn.execute("CREATE TABLE IF NOT EXISTS products(id INT IDENTITY, name VARCHAR(255), price FLOAT, weight INT)", ddl -> { if (ddl.failed()) { throw new RuntimeException(ddl.cause()); } conn.execute("INSERT INTO products (name, price, weight) VALUES ('Egg Whisk', 3.99, 150), ('Tea Cosy', 5.99, 100), ('Spatula', 1.00, 80)", fixtures -> { if (fixtures.failed()) { throw new RuntimeException(fixtures.cause()); } done.handle(null); }); }); }); } }