forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdbc_example.groovy
39 lines (33 loc) · 1.02 KB
/
jdbc_example.groovy
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
import io.vertx.groovy.ext.jdbc.JDBCClient
def client = JDBCClient.createShared(vertx, [
url:"jdbc:hsqldb:mem:test?shutdown=true",
driver_class:"org.hsqldb.jdbcDriver",
max_pool_size:30
])
client.getConnection({ conn ->
if (conn.failed()) {
System.err.println(conn.cause().getMessage())
return
}
def connection = conn.result()
// create a test table
connection.execute("create table test(id int primary key, name varchar(255))", { create ->
// insert some test data
connection.execute("insert into test values (1, 'Hello'), (2, 'World')", { insert ->
// query some data with arguments
connection.queryWithParams("select * from test where id = ?", [
2
], { rs ->
rs.result().results.each { line ->
println(groovy.json.JsonOutput.toJson(line))
}
// and close the connection
connection.close({ done ->
if (done.failed()) {
throw new java.lang.RuntimeException(done.cause())
}
})
})
})
})
})