2020-04-03 13:23:32 +00:00
---
2022-04-09 13:29:05 +00:00
sidebar_position: 3
sidebar_label: JDBC
2020-04-03 13:23:32 +00:00
---
2020-03-21 04:11:51 +00:00
# JDBC {#table-engine-jdbc}
2019-05-07 22:50:37 +00:00
Allows ClickHouse to connect to external databases via [JDBC ](https://en.wikipedia.org/wiki/Java_Database_Connectivity ).
2021-06-11 09:37:38 +00:00
To implement the JDBC connection, ClickHouse uses the separate program [clickhouse-jdbc-bridge ](https://github.com/ClickHouse/clickhouse-jdbc-bridge ) that should run as a daemon.
2019-05-07 22:50:37 +00:00
2020-04-30 18:19:18 +00:00
This engine supports the [Nullable ](../../../sql-reference/data-types/nullable.md ) data type.
2019-05-07 22:50:37 +00:00
2020-03-20 10:10:48 +00:00
## Creating a Table {#creating-a-table}
2019-05-07 22:50:37 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-06-06 09:57:12 +00:00
CREATE TABLE [IF NOT EXISTS] [db.]table_name
2019-12-29 13:57:26 +00:00
(
columns list...
)
2021-06-11 09:37:38 +00:00
ENGINE = JDBC(datasource_uri, external_database, external_table)
2019-05-07 22:50:37 +00:00
```
**Engine Parameters**
2021-06-11 09:37:38 +00:00
- `datasource_uri` — URI or name of an external DBMS.
URI Format: `jdbc:<driver_name>://<host_name>:<port>/?user=<username>&password=<password>` .
2020-03-21 04:11:51 +00:00
Example for MySQL: `jdbc:mysql://localhost:3306/?user=root&password=root` .
2019-05-07 22:50:37 +00:00
2020-03-21 04:11:51 +00:00
- `external_database` — Database in an external DBMS.
2020-03-20 10:10:48 +00:00
2021-06-11 09:37:38 +00:00
- `external_table` — Name of the table in `external_database` or a select query like `select * from table1 where column1=1` .
2019-05-07 22:50:37 +00:00
2020-03-20 10:10:48 +00:00
## Usage Example {#usage-example}
2019-05-07 22:50:37 +00:00
2020-03-20 10:10:48 +00:00
Creating a table in MySQL server by connecting directly with it’ s console client:
2019-05-07 22:50:37 +00:00
2020-03-20 10:10:48 +00:00
``` text
2019-05-07 22:50:37 +00:00
mysql> CREATE TABLE `test` .`test` (
-> `int_id` INT NOT NULL AUTO_INCREMENT,
-> `int_nullable` INT NULL DEFAULT NULL,
-> `float` FLOAT NOT NULL,
-> `float_nullable` FLOAT NULL DEFAULT NULL,
-> PRIMARY KEY (`int_id`));
Query OK, 0 rows affected (0,09 sec)
mysql> insert into test (`int_id`, `float` ) VALUES (1,2);
Query OK, 1 row affected (0,00 sec)
mysql> select * from test;
2020-04-03 13:23:32 +00:00
+------+----------+-----+----------+
2019-05-07 22:50:37 +00:00
| int_id | int_nullable | float | float_nullable |
2020-04-03 13:23:32 +00:00
+------+----------+-----+----------+
2019-05-07 22:50:37 +00:00
| 1 | NULL | 2 | NULL |
2020-04-03 13:23:32 +00:00
+------+----------+-----+----------+
2019-05-07 22:50:37 +00:00
1 row in set (0,00 sec)
```
2019-06-06 09:57:12 +00:00
Creating a table in ClickHouse server and selecting data from it:
2019-05-07 22:50:37 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2020-02-02 03:13:08 +00:00
CREATE TABLE jdbc_table
(
2020-01-16 16:53:56 +00:00
`int_id` Int32,
`int_nullable` Nullable(Int32),
`float` Float32,
`float_nullable` Nullable(Float32)
2020-02-02 03:13:08 +00:00
)
ENGINE JDBC('jdbc:mysql://localhost:3306/?user=root& password=root', 'test', 'test')
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` sql
2019-05-07 22:50:37 +00:00
SELECT *
FROM jdbc_table
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-05-07 22:50:37 +00:00
┌─int_id─┬─int_nullable─┬─float─┬─float_nullable─┐
│ 1 │ ᴺᵁᴸᴸ │ 2 │ ᴺᵁᴸᴸ │
└────────┴──────────────┴───────┴────────────────┘
```
2021-06-11 09:37:38 +00:00
``` sql
INSERT INTO jdbc_table(`int_id`, `float` )
SELECT toInt32(number), toFloat32(number * 1.0)
FROM system.numbers
```
2020-03-20 10:10:48 +00:00
## See Also {#see-also}
2019-05-07 22:50:37 +00:00
2020-04-30 18:19:18 +00:00
- [JDBC table function ](../../../sql-reference/table-functions/jdbc.md ).
2019-05-15 10:44:55 +00:00
2021-09-19 20:05:54 +00:00
[Original article ](https://clickhouse.com/docs/en/engines/table-engines/integrations/jdbc/ ) <!--hide-->