Comprehensive analysis JDBC (2)
Comprehensive analysis JDBC (2)
How to set up a JDBC connection?
Connection object database connectivity and representatives. Linking process including the implementation of the SQL statement and the connection by the return of the results. An application with a single database can be one or more connections, or with many database connectivity.
1. Link opened
And the establishment of the database is the standard method for connecting calls DriverManager.getConnection methods. The accepted method of containing a URL string. DriverManager class (the so-called JDBC management) will attempt to find a URL that can be represented by the database connectivity drivers. DriverManager class have registered Driver category list. When getConnection call method, it will check the list of each driver until you find may be specified in the URL database connectivity drivers procedures. Driver connect using this method to establish the actual URL of the link.
Users can bypass JDBC Driver method called direct management. This special circumstances in the following would be useful: When two drives can be connected to the database, and users need to be clear and specific use of the drive. But under normal circumstances, let DriverManager class that handles such things open connections will be more simple.
The following code shows how to open a URL in the "jdbc: odbc: wombat" database connectivity. By the user identifier for the "freely" with the password "ec":
String url = "jdbc: odbc: wombat";
Connection con = DriverManager.getConnection (url, "freely", "ec");
2. General usage of the URL
The URL often cause confusion, we will first make a brief to the general URL that, and then discuss JDBCURL. URL (uniform resource locator address) to provide the necessary resources on the Internet positioning information. It can be imagined as an address. URL to the first part of the access to information specified by the agreement, always behind with colon. Common agreements "ftp" (on behalf of "File Transfer Protocol") and "http" (on behalf of "hypertext transfer protocol"). If the agreement is "file" and that resources are in a local file system, rather than on the Internet (for the cases that we are describing parts of it is not an integral part of the URL).
Ftp://Javasoft 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 (behind the colon) is the current location of the data resources of relevant information. If the agreement is the file, then the remaining part of the URL of the document path. For ftp and http protocol, URL and the remainder of the optional logo and the host is given the address of a more detailed path. For example, the following is JavaSoft Home URL. The only logo host URL: http://Java.sun.com. Home visit from the beginning, it can enter to many other pages, one of which is JDBC Home. JDBC home page URL to be more specific, it specifically said:
Http://Java.sun.com/products/jdbc
3. JDBC URL
JDBC URL database provides an identification method can make the corresponding driver can identify and establish the database connection. In fact, the driver programmers will decide what specific JDBC URL to identify the driver. Users do not have to concern how to form JDBC URL they use and only used by the driver to be provided by the URL. JDBC role is to provide some agreement, member-driven programming in their JDBC URL structure should follow these conventions.
As JDBC URL with a variety of drivers used together, these conventions should be very flexible. First, they should be allowed to use different drivers for different options for naming database. For example, odbc-agreement allows (but does not require) URL containing attribute values.
Secondly, JDBC URL should be allowed to be driven programming Rapporteur all necessary information into them. This allows the database to a given dialogue with the Applet open database connection, and do not require users of any system management.
Finally, JDBC URL should be allowed a certain degree of indirect. In other words, JDBC URL can point to mainframe or database of logic, and this logic or mainframe database from the network's naming system to dynamically switch to a name. This allows the system administrator need not be specific host name JDBC statement for a part. Network naming services (such as DNS, NIS and DCE) There are many, and for which the use of naming services and unlimited.
JDBC URL standard syntax is as follows. It consists of three parts, each part of separated by colons:
Jdbc: <Yao Jing-A HS? Said? Br> JDBC URL to the three parts of decomposition are as follows:
(1) jdbc agreement: the agreement JDBC URL always jdbc.
(2) <sub-agreement>: Driver or database connection mechanism (such mechanisms may be one or more driver support) name. Of the agreement were typical examples of "odbc", the name used for the designated style ODBC data resources devoted to retain the name of the URL. For example, in order to pass JDBC-ODBC Bridge to access a database, can be used as shown in the following URL: jdbc: odbc: book. In this example, the sub-agreement "odbc", the name of the "book" is a local ODBC data resources. If we want to network naming services (such JDBC URL names in the database is not the actual name), naming services can be as a sub-agreement. For example, can be shown in the following URL: jdbc: dcenaming: accounts. In this example, the URL specified in the local DCE naming names database services should be "accounts" for the more specific analysis of the database can be used to connect real name.
(3) <-name>: the method of identification database. - Names can be different according to the agreement of change. It could also have the name of the sub-sub-title (with driver programmers choice of any internal syntax). Use of the name of the location for the purpose of the database is to provide adequate information. Precedent, because ODBC will provide the remaining part of the information, "book" is sufficient. However, in a database on a remote server needs more information. For example, if the database is accessible through the Internet, should be in the JDBC URL in the name of the network address as a part of include, and must abide by the standard shown in the following URL naming conventions: / / hostname: port / of the agreement.
Assumptions, "dbnet" is used to connect to the Internet a host of the agreement, JDBC URL should read: jdbc: dbnet: / / wombat: 356/fred.
4. "Odbc" of the agreement
- Odbc agreement is a special case. It is designated for use ODBC-style data resources to retain the name of the URL, and with the following characteristics: allow the name of (name of data resources) behind specify any number of attribute values. Odbc agreement of the integrity of grammar:
Jdbc: odbc: <Data Resources Name> [; <property name attribute value> = <>], therefore, the following are legitimate jdbc: odbc title:
Jdbc: odbc: qeor7
Jdbc: odbc: wombat
Jdbc: odbc: wombat; CacheSize = 20; ExtensionCase = LOWER
Jdbc: odbc: qeora; UID = kgh; PWD = fooey
5. Registration of the agreement
Driver programmers may retain a name to be used as a JDBC URL of the agreement were. When DriverManager this category has been added to the name of the driver registration list, it retained the name of the driver should be able to identify the name and logo of its database and establish a connection. For example, odbc for JDBC-ODBC bridge reservations. There is a Miracle assumptions, it may be the "miracle" of their registration as a Miracle DBMS connected to the JDBC driver of the agreement, which will cause others can use this name.
JavaSoft currently registered as an informal agent responsible for JDBC name of the agreement. To register a name of the agreement, please send an e-mail to the following address: jdbc@wombat.eng.sun.com.
6. Send SQL statements
Once connected, it can be used to the database to send SQL statements. JDBC can be sent to the types of SQL statements without any restrictions. This provides a great deal of flexibility, which allows the use of a specific database sentences or even non-SQL statements. However, it requires the user is responsible for ensuring that the database can be dealt with by the SQL statement sent, or they will suffer. For example, if an application attempts to stored procedures do not support stored procedure calls sent DBMS, will fail and will throw an error. JDBC driver should be asked to provide at least ANSI SQL-2 Entry Level function can be in the TM standard JDBC. This means that users of this standard at least credible level of function.
JDBC provides three categories for the SQL statement sent to the database. Connection interface of the three methods can be used to create these kind of examples. These categories are listed below and create methods:
(1) Statement: createStatement created by the method. Statement object used to send simple SQL statement.
(2) PreparedStatement: prepareStatement created by the method. PreparedStatement with objects used to send one or more input parameters (IN parameter) of SQL statements. PreparedStatement ownership of a group method for setting IN parameter values. Executive statement, IN these parameters will be sent to the database. PreparedStatement expanded Statement example, they include a Statement of the method. PreparedStatement object Statement may object than more efficient, because it has been both on a pre-compiler that for future use.
(3) CallableStatement: prepareCall created by the method. CallableStatement targets for the implementation of SQL stored procedures ─ a group to be called by name (like function to be called as) the SQL statement. CallableStatement objects inherit from the PreparedStatement IN parameters for dealing with the method, but also to deal with increased OUT parameters and INOUT parameters.
But generally speaking createStatement method for simple SQL statement (without parameters), with prepareStatement method can be applied to one or more parameters of the SQL statement IN often been implemented or simple SQL statements, and prepareCall storage method has been used to call the process .
7. Service
Service by one or more such statements: the implementation of these statements has been completed and was submitted or reduction. When you call methods commit or rollback, on the current affairs will come to the end of another Affairs began. Default, the new link will be in a mode to Automatic. In other words, when the implementation of End sentence, that sentence will automatically call the commit method. Under such circumstances, as each statement is being submitted separately and, therefore, a Panel composed by only one statement. If submitted to disable the automatic mode, the Panel will have to wait until commit or rollback method is called when Explicit end, it will include a call on the method commit or rollback has been carried out all the sentences. For the second scenario, all the affairs of the statement would be submitted or as a group to restore.
Methods commit to the database SQL statements made any changes to become permanent, it will release all the locks Affairs holders. The method rollback will be abandoned to those changes. Sometimes in another user does not want the changes to take effect before the changes to take effect. This can be submitted through disable automatic updating and two in a portfolio to achieve affairs. If the two are successful updates, call commit, so that the two updated results become permanent if one or two have failed to update, call rollback ways to restore the value will be updated before the value.
Most JDBC driver support services. In fact, the JDBC driver must support services. DatabaseMetaData given information describing the services provided by the DBMS support level.
8. Isolation level
If DBMS support of business, it must have some way to manage the two affairs at the same time to operate on a database that may occur when a conflict. Users can be designated isolation level to specify how much should be spent on DBMS to resolve potential conflicts. For example, when a certain value changes Affairs and the second in the affairs of the changes has been submitted before or restore the value of the reader how Office.
The first assumption was Reduction Services, a firm second reading of the changes will be null and void, then it may allow this kind of conflict? JDBC users can be directed to the following code in the value of DBMS allows read before being submitted to the value ( "dirty read"), which is currently connected con:
Con.setTransactionIsolation (TRANSACTION_READ_UNCOMMITTED);
Isolation level higher, in order to avoid conflict also spent more and more energy. Connection interface defines five, the lowest level which is not designated a fundamental support services, and is designated as the highest level of services in a database operation, any other services that matters is not to read any of the data changes. Usually, the higher the isolation level, the application will be the speed of implementation of the slow (because of the resources spent to lock increase, but the concurrent operation between users reduced). In deciding what isolation level, developers must demand performance and data consistency between demand balance. However, the actual level that can be supported by the DBMS depends on the function.
When creating Connection object, and its isolation level depends on the driver, but usually when it involves a database of default values. Users can call setIsolationLevel approach to changes isolation level. The level of new connections will be in the process of the entry into force of the time remaining. Only change if a panel of the isolation level, the Panel must be set before the start, and the Panel conducted after reset. We do not advocate in the affairs of the middle of the isolation level to make changes, because it will immediately trigger commit method call, so that before any changes made to become permanent.
(To be continued)






