The connection to the JDBC Basics tutorial

Source: Internet
Author: User
Tags file system ftp odbc naming convention reserved resource file transfer protocol

The Connection object represents a connection to the database. The connection process includes the executed SQL statement and the results returned on that connection. An application can have one or more connections to a single database, or it can be connected to many databases. 2.1.1 The standard way to open a connection to a database is to invoke the Drivermanager.getconnection method. This method accepts a string containing a URL. The DriverManager class, known as the JDBC Management layer, will attempt to locate a driver that can connect to the database represented by that URL. The DriverManager class contains a list of registered Driver classes. When the method getconnection is invoked, it checks each driver in the manifest until it finds a driver that can connect to the database specified in the URL. Driver's method connect uses this URL to establish the actual connection.

Users can invoke the Driver method directly by bypassing the JDBC management layer. This is useful in exceptional cases where two drives can be connected to the database at the same time, and the user needs to explicitly select a particular drive. But in general, it would be simpler to let the DriverManager class handle open connections.

The following code shows how to open a connection to a database that is located in the URL "Jdbc:odbc:wombat". The user identifier used is "Oboy" and the password is "12Java":

String url = "jdbc:odbc:wombat";
Connection con = DriverManager.getConnection(url, "oboy", "12Java");

2.1.2 General usage URLs because URLs are often confusing, we'll start with a simple description of the generic URL before we discuss the JDBC URL.

The URL (Uniform Resource Locator) provides the information needed to locate resources on the Internet. It can be imagined as an address. The first part of the URL specifies the protocol used to access the information followed by a colon. Commonly used protocols are "FTP" (for "File Transfer Protocol") and "http" (for Hypertext Transfer Protocol). If the protocol is "file", it means that the resource is on a local file system rather than on the Internet (the following example is used to represent the part we describe; it is not part of the URL).

ftp://javasoft.com/docs/JDK-1_apidocs.zip
http://java.sun.com/products/jdk/CurrentRelease
file:/home/haroldw/docs/books/tutorial/summary.html

The remainder of the URL (after the colon) gives information about where the data resource is located. If the protocol is file, the remainder of the URL is the path to the files. For FTP and HTTP protocols, the remainder of the URL identifies the host and optionally gives a more detailed address path. For example, the following is the URL of the JavaSoft home page. The URL identifies only the host:

Http://java.sun.com from this homepage, you can go to many other pages, one of which is the JDBC home page. The JDBC home page URL is more specific, it looks similar: HTTP://JAVA.SUN.COM/PRODUCTS/JDBC

2.1.3 JDBC URL

The JDBC URL provides a way to identify the database and enable the appropriate driver to identify the database and establish a connection to it. In fact, the driver programmer will decide what JDBC URL to use to identify the specific driver. Users do not have to care about how to form a JDBC URL; they only have to use the URL provided with the driver they are using. The role of JDBC is to provide certain conventions that driver programmers should follow when constructing their JDBC URLs.

Because JDBC URLs are used with a variety of different drivers, these conventions should be flexible. First, they should allow different drivers to name the database using different scenarios. For example, the ODBC Sub-protocol allows (but does not require) a URL to contain property values. Second, the JDBC URL should allow driver programmers to incorporate all the information they need. This allows you to open the database connection to the applet that is going to talk to a given database without requiring the user to do any system administration work. Third, the JDBC URL should allow some degree of indirection. That is, the JDBC URL can point to the logical host or database name, which is dynamically converted to the actual name by the network naming system. This allows the system administrator to not have to declare a specific host as part of the JDBC name. There are a number of network naming services, such as DNS, NIS, and DCE, and there is no limit to which naming service to use. The standard syntax for the JDBC URL is shown below. It consists of three parts, separated by a colon:

jdbc:< Sub-Protocol >:< sub-name >

The three parts of the JDBC URL can be broken down as follows: jdbc─ protocol.

The protocol in the JDBC URL is always jdbc.

< sub-protocol >─ name of driver name or database connection mechanism (this mechanism can be supported by one or more drivers). A typical example of a child protocol name is "ODBC", which is reserved specifically for the URL that specifies the name of the ODBC-style data resource. For example, to access a database through the Jdbc-odbc bridge, you can use the URL shown below:

jdbc:odbc:fred

In this case, the Child protocol is "ODBC" and the child name "Fred" is a local ODBC data resource.

If you want to use the network naming Service (so that the database name in the JDBC URL does not have to be the actual name), the naming service can be a child protocol. For example, you can use the following url:jdbc:dcenaming:accounts-payable in this example, which specifies that the local DCE naming service should "accounts-payable" the database name Resolves to a more specific name that can be used to connect to the real database. < child name >─ a way to identify the database. A child name can vary according to a different child protocol. It can also have a child name that contains any internal syntax selected by the driver programmer. The purpose of using a child name is to provide sufficient information for locating the database. In the previous precedent, because ODBC will provide the rest of the information, it is sufficient to use "Fred". However, the database located on the remote server requires more information. For example, if the database is accessed over the Internet, the network address should be included as part of the child name in the JDBC URL, and must follow the standard URL naming convention as follows://Host Name: Port/Child protocol assuming that "dbnet" is used to connect a host to Protocol on the Internet, the JDBC URL is similar:

jdbc:dbnet://wombat:356/fred 2.1.4 "ODBC" sub Protocol ODBC is a special case. It is reserved for the URL that specifies the name of the ODBC-style data resource and has the following attributes: Allows you to specify any number of property values after a child name (data resource name). The complete syntax for the ODBC Child Protocol is: jdbc:odbc:< data Resource Name >[;< property name >=< attribute value >]*

Therefore, the following are the legal JDBC:ODBC names:

jdbc:odbc:qeor7jdbc:odbc:wombat
jdbc:odbc:wombat;CacheSize=20;ExtensionCase=LOWER
jdbc:odbc:qeora;UID=kgh;PWD=fooey

The 2.1.5 Registry driver programmer can retain a name to use as a child protocol name for the JDBC URL.

When the DriverManager class adds this name to the registered driver manifest, the driver that retains the name should recognize the name and establish a connection to the database it identifies. For example, ODBC is reserved for JDBC-ODBC bridges.

Example bis, assuming that there is a Miracle company, it may register "Miracle" as a child protocol attached to the JDBC driver on its Miracle DBMS, so that the name cannot be used by anyone else. JavaSoft is currently responsible for registering the JDBC Child protocol name as an unofficial agent. To register a child agreement name, send an e-mail message to the following address:

Jdbc@wombat.eng.sun.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.