use of io.vertx.reactivex.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class Streaming method start.
@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);
jdbc.rxGetConnection().flatMapPublisher(conn -> {
// With the connection...
return // ...create test table
conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))").flatMap(// ...insert a row
result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")).flatMap(// ...another one
result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")).flatMap(// ...get values stream
result -> conn.rxQueryStream("SELECT * FROM test")).flatMapPublisher(sqlRowStream -> {
return // Transform the stream into a Flowable...
sqlRowStream.toFlowable().doOnTerminate(() -> {
// ...and close the connection when the stream is fully read or an
// error occurs
conn.close();
System.out.println("Connection closed");
});
});
}).subscribe(row -> System.out.println("Row : " + row.encode()));
}
use of io.vertx.reactivex.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class Transaction method start.
@Override
public void start() throws Exception {
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
String sql = "CREATE TABLE colors (" + "id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, " + "name VARCHAR(255), " + "datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)";
JDBCClient client = JDBCClient.createShared(vertx, config);
// Connect to the database
client.rxGetConnection().flatMap(conn -> conn.rxSetAutoCommit(false).toSingleDefault(false).flatMap(autoCommit -> conn.rxExecute(sql).toSingleDefault(true)).flatMap(executed -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE"))).flatMap(updateResult -> conn.rxCommit().toSingleDefault(true).map(commit -> updateResult)).onErrorResumeNext(ex -> conn.rxRollback().toSingleDefault(true).onErrorResumeNext(ex2 -> Single.error(new CompositeException(ex, ex2))).flatMap(ignore -> Single.error(ex))).flatMap(updateResult -> conn.rxQuery("SELECT * FROM colors")).doAfterTerminate(conn::close)).subscribe(resultSet -> {
// Subscribe to get the final result
System.out.println("Results : " + resultSet.getRows());
}, Throwable::printStackTrace);
}
use of io.vertx.reactivex.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class Client method start.
@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();
});
}
Aggregations