gpt4 book ai didi

java - JDBC 连接字符串语法和剖析

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:55:13 27 4
gpt4 key购买 nike

多年来,我使用 JDBC 连接到许多不同的关系系统:H2、HSQLDB、MySQL、Oracle、Postgres 等。在每种情况下,每个系统似乎都有自己的风格连接字符串语法。

我无法想象像 JDBC 这样长期存在的 API 不会具有用于连接字符串验证的定义的强制语法。例如:

一些有效的 HSQLDB 连接字符串:

jdbc:hsqldb:mem:mymemdb
jdbc:hsqldb:res:org.my.path.resdb
jdbc:hsqldb:file:/opt/db/testdb

MySQL:

jdbc:mysql://localhost/test

Postgres:

jdbc:postgresql://localhost/test

H2:

jdbc:h2:~/test
jdbc:h2:file:/data/sample
jdbc:h2:tcp://dbserv:8084/~/sample

从所有这些示例中,我收集了基本的通用语法:

jdbc:<vendor>:<vendor-specific-uri>

在哪里<vendor>是系统的名称( h2mysql 等),以及 <vendor-specific-uri>是确定数据库位置的路径或某种特定于供应商的方式。

我进行了大量的挖掘工作,但在我的一生中,我似乎无法找到 JDBC 在哪里定义了有效的连接字符串语法。具体来说:

  • 有效 JDBC 连接字符串的一般语法/定义是什么?
  • 连接字符串的每个标记/组件的不同名称是什么?例如,“jdbc:”是否称为“JDBC 协议(protocol)”?我的 <vendor> 的正确名称是什么?和 <vendor-specific-uri>分割市场?

最佳答案

URL 语法在 JDBC specification 中指定,特别是在第 9.4 节中:

The format of a JDBC URL is :

jdbc:<subprotocol>:<subname>

where subprotocol defines the kind of database connectivity mechanism that may be supported by one or more drivers. The contents and syntax of the subname will depend on the subprotocol.

Note – A JDBC URL is not required to fully adhere to the URI syntax as defined in RFC 3986, Uniform Resource Identifier (URI): Generic Syntax

没有比这更正式的了。子协议(protocol)通常是数据库或驱动程序的一些标识符。子名称是自由格式的,尽管驱动程序通常遵循类似 URI 的语法。

也不需要多拘谨。 DriverManager 将依次向每个已注册的 java.sql.Driver 提供 URL,第一个接受的用于连接。

关于java - JDBC 连接字符串语法和剖析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27380071/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com