2019-05-16 07:12:04 +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 ).
2019-06-06 09:57:12 +00:00
To implement the JDBC connection, ClickHouse uses the separate program [clickhouse-jdbc-bridge ](https://github.com/alex-krash/clickhouse-jdbc-bridge ) that should run as a daemon.
2019-05-07 22:50:37 +00:00
This engine supports the [Nullable ](../../data_types/nullable.md ) data type.
## Creating a Table
2019-06-06 09:57:12 +00:00
```sql
CREATE TABLE [IF NOT EXISTS] [db.]table_name
ENGINE = JDBC(dbms_uri, external_database, external_table)
2019-05-07 22:50:37 +00:00
```
**Engine Parameters**
- `dbms_uri` — URI of an external DBMS.
Format: `jdbc:<driver_name>://<host_name>:<port>/?user=<username>&password=<password>` .
Example for MySQL: `jdbc:mysql://localhost:3306/?user=root&password=root` .
- `external_database` — Database in an external DBMS.
2019-06-06 09:57:12 +00:00
- `external_table` — Name of the table in `external_database` .
2019-05-07 22:50:37 +00:00
## Usage Example
2019-06-06 09:57:12 +00:00
Creating a table in MySQL server by connecting directly with it's console client:
2019-05-07 22:50:37 +00:00
2019-09-23 15:31:46 +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;
+--------+--------------+-------+----------------+
| int_id | int_nullable | float | float_nullable |
+--------+--------------+-------+----------------+
| 1 | NULL | 2 | NULL |
+--------+--------------+-------+----------------+
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
2019-09-23 15:31:46 +00:00
```sql
2019-05-07 22:50:37 +00:00
CREATE TABLE jdbc_table ENGINE JDBC('jdbc:mysql://localhost:3306/?user=root& password=root', 'test', 'test')
2019-09-23 15:31:46 +00:00
```
```sql
2019-05-07 22:50:37 +00:00
DESCRIBE TABLE jdbc_table
2019-09-23 15:31:46 +00:00
```
```text
2019-05-07 22:50:37 +00:00
┌─name───────────────┬─type───────────────┬─default_type─┬─default_expression─┐
│ int_id │ Int32 │ │ │
│ int_nullable │ Nullable(Int32) │ │ │
│ float │ Float32 │ │ │
│ float_nullable │ Nullable(Float32) │ │ │
└────────────────────┴────────────────────┴──────────────┴────────────────────┘
2019-09-23 15:31:46 +00:00
```
```sql
2019-05-07 22:50:37 +00:00
SELECT *
FROM jdbc_table
2019-09-23 15:31:46 +00:00
```
```text
2019-05-07 22:50:37 +00:00
┌─int_id─┬─int_nullable─┬─float─┬─float_nullable─┐
│ 1 │ ᴺᵁᴸᴸ │ 2 │ ᴺᵁᴸᴸ │
└────────┴──────────────┴───────┴────────────────┘
```
## See Also
- [JDBC table function ](../../query_language/table_functions/jdbc.md ).
2019-05-15 10:44:55 +00:00
[Original article ](https://clickhouse.yandex/docs/en/operations/table_engines/jdbc/ ) <!--hide-->