0% found this document useful (0 votes)
222 views2 pages

Sample JDBI With hikariCP GitHub

This document provides a sample code for using JDBI with HikariCP for database connectivity in Scala. It shows how to configure HikariCP as the connection pool, initialize JDBI with the Hikari data source, execute a query to select all rows from a table, print the results, and close the connection handle and data source. The sample code connects to a MySQL database, executes a simple select query, and maps the results to objects using the DefaultMapper.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views2 pages

Sample JDBI With hikariCP GitHub

This document provides a sample code for using JDBI with HikariCP for database connectivity in Scala. It shows how to configure HikariCP as the connection pool, initialize JDBI with the Hikari data source, execute a query to select all rows from a table, print the results, and close the connection handle and data source. The sample code connects to a MySQL database, executes a simple select query, and maps the results to objects using the DefaultMapper.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

3/9/2021 sample JDBI with hikariCP · GitHub

Instantly share code, notes, and snippets.

maji-KY / JDBI.scala
Created 7 years ago

Star

Code Revisions 1 Stars 4

sample JDBI with hikariCP

JDBI.scala

1 package com.neco_labo.db
2
3 import com.zaxxer.hikari.{HikariDataSource, HikariConfig}
4 import org.skife.jdbi.v2.{DefaultMapper, DBI}
5
6 import scala.collection.JavaConverters._
7
8 object JDBI extends App {
9
10 val config = new HikariConfig()
11 config.setJdbcUrl("jdbc:mysql://localhost:3306/db?zeroDateTimeBehavior=round")
12 config.setUsername("user")
13 config.setPassword("pass")
14 config.addDataSourceProperty("dataSourceClassName", "com.mysql.jdbc.jdbc2.optional.Mysql
15 config.addDataSourceProperty("autoCommit", "false")
16 config.addDataSourceProperty("useServerPrepStmts", "true")
17 config.addDataSourceProperty("cachePrepStmts", "true")
18
19 val ds = new HikariDataSource(config)
20
21 val dbi = new DBI(ds)
22
23 val h = dbi.open()
24
25 val result = h.createQuery("select * from table")
26 .map(new DefaultMapper)
27 .iterator().asScala
28
29 result.foreach(println)

30
31 h.close()

https://gist.github.com/maji-KY/646f202cacac855cd8da 1/2
3/9/2021 sample JDBI with hikariCP · GitHub
32
33 ds.close()
34
35 }

https://gist.github.com/maji-KY/646f202cacac855cd8da 2/2

You might also like