简体   繁体   English

JDBC使用URL进行连接

[英]JDBC use urls to connect

When you setup a URL in your jdbc properties like: 在jdbc属性中设置URL时,例如:

jdbc:derby://localhost:1527/vehicle;create=true

Does this mean all the data is being sent via HTTP on a specific port? 这是否意味着所有数据都通过HTTP在特定端口上发送? (it seems so obviously) (似乎很明显)

It has nothing to do with HTTP, no. 它与HTTP无关,不。 The protocol used is specific to the database and JDBC driver, as is JDBC URL format. 使用的协议特定于数据库和JDBC驱动程序,JDBC URL格式也是如此。 The only thing common to all JDBC URLs is the jdbc: prefix. 所有JDBC URL唯一的共同点是jdbc:前缀。

In this specific case, I'd say you have a Apache Derby JDBC URL, connecting to a server on localhost port 1527, database (or maybe user) name "vehicle", creating it if necessary. 在这种特定情况下,我想说您有一个Apache Derby JDBC URL,它连接到本地主机端口1527上的服务器,数据库(或用户)名称为“ vehicle”,并在必要时创建它。

Another example would be an Oracle JDBC URL format: 另一个示例是Oracle JDBC URL格式:

jdbc:oracle:<drivertype>:<username/password>@<database>

Very different to the Derby one, but specifying the same sort of information. 与Derby完全不同,但是指定了相同的信息。

Quoting the chapter 6 Database connections of the specification for the JDBC API (this is an old version but I find it more clear than more recent versions about JDBC URLs): 引用第6JDBC API规范的 数据库连接 (这是一个旧版本,但是我发现它比有关JDBC URL的最新版本更清楚):

6.3 URLs 6.3网址

6.3.1 Goals for JDBC database naming 6.3.1 JDBC数据库命名的目标

We need to provide a way of naming databases so that application writers can specify which database they wish to connect to. 我们需要提供一种命名数据库的方法,以便应用程序编写者可以指定他们希望连接到的数据库。

We would like this JDBC naming mechanism to have the following properties: 我们希望此JDBC命名机制具有以下属性:

  • Different drivers can use different schemes for naming databases. 不同的驱动程序可以使用不同的方案来命名数据库。 For example, a JDBC-ODBC bridge driver may support simple ODBC style data source names, whereas a network protocol driver may need to know additional information so it can discover which hostname and port to connect to. 例如,JDBC-ODBC桥驱动程序可能支持简单的ODBC样式数据源名称,而网络协议驱动程序可能需要知道其他信息,以便它可以发现要连接的主机名和端口。
  • If a user downloads an applet that wants to talk to a given database then we would like to be able to open a database connection without requiring the user to do any system administration chores. 如果用户下载了要与给定数据库对话的小程序,那么我们希望能够打开数据库连接而无需用户执行任何系统管理工作。 Thus for example, we want to avoid requiring an analogue of the human-administered ODBC data source tables on the client machines. 因此,例如,我们希望避免在客户端计算机上要求类似人为管理的ODBC数据源表。 This implies that it should be possible to encode any necessary connection information in the JDBC name. 这意味着应该可以在JDBC名称中编码任何必要的连接信息。
  • We would like to allow a level of indirection in the JDBC name, so that the initial name may be resolved via some network naming system in order to locate the database. 我们希望在JDBC名称中允许一定程度的间接访问,以便可以通过某些网络命名系统解析初始名称,以便定位数据库。 This will allow system administrators to avoid specifying particular hosts as part of the JDBC name. 这将使系统管理员可以避免将特定主机指定为JDBC名称的一部分。 However, since there are a number of different network name services (such as NIS, DCE, etc.) we do not wish to mandate that any particular network nameserver is used. 但是,由于存在许多不同的网络名称服务(例如NIS,DCE等),因此我们不希望使用任何特定的网络名称服务器。

6.3.2 URL syntax 6.3.2 URL语法

Fortunately the World Wide Web has already standardized on a naming system that supports all of these properties. 幸运的是,万维网已经在支持所有这些属性的命名系统上实现了标准化。 This is the Uniform Resource Locator (URL) mechanism. 这是统一资源定位器(URL)机制。 So we propose to use URLs for JDBC naming, and merely recommend some conventions for structuring JDBC URLs. 因此,我们建议使用URL进行JDBC命名,而仅建议一些用于构造JDBC URL的约定。

We recommend that JDBC URL's be structured as: 我们建议将JDBC URL的结构如下:

 jdbc:<subprotocol>:<subname> 

where a subprotocol names a particular 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. 子名称的内容和语法将取决于子协议。

If you are specifying a network address as part of your subname, we recommend following the standard URL naming convention of "//hostname:port/subsubname" for the subname. 如果您要指定网络地址作为子名称的一部分,我们建议遵循标准URL命名约定“ // hostname:port / subsubname”作为子名称。 The subsubname can have arbitrary internal syntax. 子子名称可以具有任意内部语法。

To summarize: 总结一下:

  • The only thing in common between a JDBC URL and an HTTP URL is that both use the same standardized naming system: the Uniform Resource Locator (URL) mechanism (this is only a recommendation for JDBC 1 ). JDBC URL和HTTP URL之间唯一的共同点是两者都使用相同的标准化命名系统: 统一资源定位器 (URL)机制(这仅是JDBC 1的建议)。

  • A JDBC URL is specific to a JDBC driver (different drivers for a same database will use different schemes). JDBC URL特定于JDBC驱动程序(同一数据库的不同驱动程序将使用不同的方案)。 Refer to the documentation of the JDBC driver for the details (for example, when using Derby Network Server, have a look at Derby network client URL Format ). 有关详细信息,请参阅JDBC驱动程序的文档(例如,在使用Derby Network Server时,请查看Derby网络客户端URL格式 )。

  • And no, the protocol used by a JDBC client to talk to a database server is not HTTP (it his a database specific protocol). 不能,JDBC客户端用来与数据库服务器通信的协议不是HTTP(它是数据库特定的协议)。


1 Actually, a JDBC URL may not be a real URI as mentioned in this note of the brand new JSR 221: JDBCTM 4.0 API Specification : 1实际上,如全新的JSR 221:JDBCTM 4.0 API规范的本说明中所提到的,JDBC URL可能不是真实的URI:

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. -不需要完全遵循RFC 3986(统一资源标识符(URI):通用语法)中定义的URI语法的JDBC URL。

The short answer is no. 最简洁的答案是不。 To understand why you need to understand the syntax of a URL. 要了解为什么需要了解URL的语法。 The basic syntax of a URI is URI的基本语法是

<url> ::= <scheme>:<scheme-specific-part>

where the is an identifier that says how to interpret the stuff after the colon. 其中的标识符表示在冒号后如何解释内容。 Most web URLs use the 'http' scheme; 大多数Web URL使用'http'方案; eg 例如

http://www.example.com/somepage.html

though web browsers also understand other schemes such 'ftp', 'mailto' and so on. 尽管网络浏览器还可以理解其他方案,例如“ ftp”,“ mailto”等。

But as you can see, a JDBC URL uses the 'jdbc' scheme; 但是正如您所看到的,JDBC URL使用'jdbc'方案。 for example 例如

jdbc:derby://localhost:1527/vehicle;create=true

In some cases (eg 'http' and 'ftp') the scheme has a correspond application protocol (eg HTTP or FTP respectively) that may be used for accessing content. 在某些情况下(例如“ http”和“ ftp”),该方案具有可用于访问内容的相应的应用协议(例如分别为HTTP或FTP)。 (This assumes the URL actually resolves to something that is fetchable). (这假设URL实际上解析为可提取的内容)。

In other cases including the 'jdbc' scheme, the URL does not denote content, and does not necessarily even specify a protocol. 在其他情况下,包括“ jdbc”方案,URL并不表示内容,甚至不一定指定协议。 In the JDBC case, the URL is actually specifying an end point location (and other details) for accessing a database. 在JDBC情况下,URL实际上是指定用于访问数据库的端点位置(和其他详细信息)。 The stuff after the second colon is JDBC driver / RDBMS specific, both in syntax and in what it means. 第二个冒号之后的内容在语法和含义上都是JDBC驱动程序/ RDBMS特定的。 But it is highly unlikely that HTTP is involved, or that a web browser would know what to do with a JDBC URL. 但是,涉及HTTP的可能性很小,或者Web浏览器不太可能知道如何处理JDBC URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM