shorten title
[yosql.git] / yosql-website / content / persistence / jdbc / _index.md
blob9d170dd690bc811693bc46263e8c00f27512810b
1 ---
2 title: JDBC
3 date: 2019-09-27T18:51:08+02:00
4 menu:
5   main:
6     parent: Persistence APIs
7 categories:
8   - Persistence
9 tags:
10   - JDBC
11 ---
13 The `javax.sql` based implementation of `YoSQL` to access your database. It does not require any dependencies outside from standard JDK classes exposed by the [JDBC](https://en.wikipedia.org/wiki/Java_Database_Connectivity) API. The available configuration is listed split into multiple pages which are listed at the bottom of this page.
15 ## Tooling
17 In order to use the JDBC API in generated code, set the `daoApi` option to `JDBC`. Further configuration can be performed using the dedicated `jdbc` section.
19 ### Maven
21 In order to use `YoSQL` together with [Maven](https://maven.apache.org/), take a look at the tooling [documentation
22 for Maven](/tooling/maven/).
24 {{< maven/persistence/jdbc/index >}}
26 ### Gradle
28 In order to use `YoSQL` together with [Gradle](https://gradle.org/), take a look at the tooling [documentation for Gradle](/tooling/gradle/).
30 ```groovy
31 plugins {
32   id("wtf.metio.yosql")
35 yosql {
36   apis {
37     daoApi = JDBC
38   }
39   jdbc {
40     ... jdbc configuration
41   }
43 ```
45 ### Bazel
47 In order to use `YoSQL` together with [Bazel](https://bazel.build/), take a look at the tooling [documentation for
48 Bazel](/tooling/bazel/).
50 ### CLI
52 In order to use YoSQL on the command line, take a look at the tooling [documentation for CLI](/tooling/cli/).
54 ```shell
55 $ yosql --apis-dao-api=JDBC
56 ```
58 The shorter form is available as well:
60 ```shell
61 $ yosql --dao-api=JDBC
62 ```
64 ## Manual Converters
66 The JDBC API offers no built-in object mapping mechanism. In order to use high level types of your domain, use a converter to map results to your types.
68 ```sql
69 -- results:
70 --   converter: my.own.UserConverter
71 SELECT  *
72 FROM    users
73 ```
75 You can either specify the fully-qualified name of the converter or use its alias. The result type is read from the converter configuration as well. This in turn changes the generated code into this:
77 ```java
78 // uses "User" as type argument for all return types
79 List<User> findUsers()
80 Stream<User> findUsersStreamEager()
81 Stream<User> findUsersStreamLazy()
82 Flowable<User> findUsersFlow()
83 ```
85 `my.own.UserConverter` could look like this:
87 ```java
88 package my.own;
90 import java.sql.ResultSet;
91 import java.sql.SQLException;
93 import my.own.User;
94 import my.own.persistence.util.ResultState;
96 public class UserConverter {
98     public final User asUserType(final ResultState result) throws SQLException {
99         final ResultSet resultSet = result.getResultSet();
100         final User pojo = new User();
101         pojo.setId(resultSet.getInt("id"));
102         pojo.setName(resultSet.getString("name"));
103         return pojo;
104     }