public void add(int a, int b, Handler<Integer> resultHandler) {
int r = a + b;
resultHandler.handle(r);
}
String sql = "SELECT * FROM Products"; ResultSet rs = stmt.executeQuery(sql);
connection.query("SELECT * FROM Products", result -> {
// do something with the result
});
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-jdbc-client</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>2.3.3</version> </dependency>
if (ar.failed()) {
System.err.println("The operation has failed...: "
+ ar.cause().getMessage());
} else {
// Use the result:
result = ar.result();
}
startBackend( (connection) -> createSomeData(connection, (nothing) -> startWebApp( (http) -> completeStartup(http, fut) ), fut ), fut);
private void startBackend(Handler<AsyncResult<SQLConnection>> next, Future<Void> fut) {
jdbc.getConnection(ar -> {
if (ar.failed()) {
fut.fail(ar.cause());
} else {
next.handle(Future.succeededFuture(ar.result()));
}
});
}
private void createSomeData(AsyncResult<SQLConnection> result,
Handler<AsyncResult<Void>> next, Future<Void> fut) {
if (result.failed()) {
fut.fail(result.cause());
} else {
SQLConnection connection = result.result();
connection.execute(
"CREATE TABLE IF NOT EXISTS Whisky (id INTEGER IDENTITY, name varchar(100), " +
"origin varchar(100))",
ar -> {
if (ar.failed()) {
fut.fail(ar.cause());
connection.close();
return;
}
connection.query("SELECT * FROM Whisky", select -> {
if (select.failed()) {
fut.fail(ar.cause());
connection.close();
return;
}
if (select.result().getNumRows() == 0) {
insert(
new Whisky("Bowmore 15 Years Laimrig", "Scotland, Islay"),
connection,
(v) -> insert(new Whisky("Talisker 57° North", "Scotland, Island"),
connection,
(r) -> {
next.handle(Future.<Void>succeededFuture());
connection.close();
}));
} else {
next.handle(Future.<Void>succeededFuture());
connection.close();
}
});
});
}
}
connection.execute( SQL statement, handler called when the statement has been executed )
private void insert(Whisky whisky, SQLConnection connection, Handler<AsyncResult<Whisky>> next) {
String sql = "INSERT INTO Whisky (name, origin) VALUES ?, ?";
connection.updateWithParams(sql,
new JsonArray().add(whisky.getName()).add(whisky.getOrigin()),
(ar) -> {
if (ar.failed()) {
next.handle(Future.failedFuture(ar.cause()));
return;
}
UpdateResult result = ar.result();
// Build a new whisky instance with the generated id.
Whisky w = new Whisky(result.getKeys().getInteger(0), whisky.getName(), whisky.getOrigin());
next.handle(Future.succeededFuture(w));
});
}
private void getAll(RoutingContext routingContext) {
jdbc.getConnection(ar -> {
SQLConnection connection = ar.result();
connection.query("SELECT * FROM Whisky", result -> {
List<Whisky> whiskies = result.result().getRows().stream().map(Whisky::new).collect(Collectors.toList());
routingContext.response()
.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(whiskies));
connection.close(); // Close the connection
});
});
}
DeploymentOptions options = new DeploymentOptions()
.setConfig(new JsonObject()
.put("http.port", port)
.put("url", "jdbc:hsqldb:mem:test?shutdown=true")
.put("driver_class", "org.hsqldb.jdbcDriver")
);
{
"http.port": ${http.port},
"url": "jdbc:hsqldb:mem:it-test?shutdown=true",
"driver_class": "org.hsqldb.jdbcDriver"
}
{
"http.port" : 8082,
"url": "jdbc:hsqldb:file:db/whiskies",
"driver_class": "org.hsqldb.jdbcDriver"
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有