> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> ClickHouseがJDBC経由で外部データベースに接続できるようにするテーブルエンジンです。

# JDBCテーブルエンジン

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            ClickHouse Cloud では利用できません
        </div>;
};

<Note>
  clickhouse-jdbc-bridge には実験的なコードが含まれており、現在はサポートされていません。信頼性の問題やセキュリティ脆弱性が存在する可能性があります。使用は自己責任でお願いします。
  ClickHouse では、アドホックなクエリには、より優れた代替手段となる ClickHouse 組み込みのテーブル関数 (Postgres、MySQL、MongoDB など) の使用を推奨しています。
</Note>

[JDBC](https://en.wikipedia.org/wiki/Java_Database_Connectivity) を介して、ClickHouse から外部データベースに接続できます。

JDBC 接続を実装するために、ClickHouse はデーモンとして実行する必要がある別プログラム [clickhouse-jdbc-bridge](https://github.com/ClickHouse/clickhouse-jdbc-bridge) を使用します。

このエンジンは [Nullable](/ja/reference/data-types/nullable) データ型をサポートしています。

<div id="creating-a-table">
  ## テーブルの作成
</div>

```sql theme={null}
CREATE TABLE [IF NOT EXISTS] [db.]table_name
(
    columns list...
)
ENGINE = JDBC(datasource, external_database, external_table)
```

**エンジンパラメータ**

* `datasource` — 外部DBMSのURIまたは名前。

  URIフォーマット: `jdbc:<driver_name>://<host_name>:<port>/?user=<username>&password=<password>`.
  MySQLの例: `jdbc:mysql://localhost:3306/?user=root&password=root`.

* `external_database` — 外部DBMS内のデータベース名、または明示的に定義されたテーブルスキーマ (例を参照) 。

* `external_table` — 外部データベース内のテーブル名、または `select * from table1 where column1=1` のような select クエリ。

* これらのパラメータは、[named collections](/ja/concepts/features/configuration/server-config/named-collections) を使って渡すこともできます。

<div id="usage-example">
  ## 使用例
</div>

MySQLサーバーのコンソールクライアントで直接接続して、テーブルを作成します。

```text theme={null}
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)
```

ClickHouse server でテーブルを作成し、そこからデータを取得します:

```sql theme={null}
CREATE TABLE jdbc_table
(
    `int_id` Int32,
    `int_nullable` Nullable(Int32),
    `float` Float32,
    `float_nullable` Nullable(Float32)
)
ENGINE JDBC('jdbc:mysql://localhost:3306/?user=root&password=root', 'test', 'test')
```

```sql theme={null}
SELECT *
FROM jdbc_table
```

```text theme={null}
┌─int_id─┬─int_nullable─┬─float─┬─float_nullable─┐
│      1 │         ᴺᵁᴸᴸ │     2 │           ᴺᵁᴸᴸ │
└────────┴──────────────┴───────┴────────────────┘
```

```sql theme={null}
INSERT INTO jdbc_table(`int_id`, `float`)
SELECT toInt32(number), toFloat32(number * 1.0)
FROM system.numbers
```

<div id="see-also">
  ## 関連項目
</div>

* [JDBC テーブル関数](/ja/reference/functions/table-functions/jdbc).
