-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
SqlClientExample.java
50 lines (43 loc) · 1.63 KB
/
SqlClientExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package io.vertx.example.virtualthreads;
import io.vertx.core.*;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.sqlclient.*;
import org.testcontainers.containers.PostgreSQLContainer;
import static io.vertx.core.Future.await;
public class SqlClientExample extends AbstractVerticle {
public static void main(String[] args) throws Exception {
var vertx = Vertx.vertx();
try (var container = new PostgreSQLContainer<>()) {
container.start();
vertx.deployVerticle(() -> {
PgConnectOptions connectOptions = new PgConnectOptions()
.setPort(container.getMappedPort(5432))
.setHost(container.getContainerIpAddress())
.setDatabase(container.getDatabaseName())
.setUser(container.getUsername())
.setPassword(container.getPassword());
return new SqlClientExample(Pool.pool(vertx, connectOptions, new PoolOptions().setMaxSize(4)));
}, new DeploymentOptions()
.setThreadingModel(ThreadingModel.VIRTUAL_THREAD))
.toCompletionStage()
.toCompletableFuture()
.get();
}
}
private final Pool pool;
public SqlClientExample(Pool pool) {
this.pool = pool;
}
@Override
public void start() {
// create a test table
await(pool.query("create table test(id int primary key, name varchar(255))").execute());
// insert some test data
await(pool.query("insert into test values (1, 'Hello'), (2, 'World')").execute());
// query some data
var rows = await(pool.query("select * from test").execute());
for (Row row : rows) {
System.out.println("row = " + row.toJson());
}
}
}