Oracle Exams Exams of Oracle,Executing Statements,Oracle Certification Exam Using the Driver.connect() Method – JDBC Applications and Design Considerations

Using the Driver.connect() Method – JDBC Applications and Design Considerations



4.2.2.3   Using the Driver.connect() Method

The Driver.connect() method enables you to create an actual connection to the desired data-base and returns an associated Connection object. This method accepts the database URL string and a Properties object as its argument. A URL indicates the protocol and location of a data source,while the Properties object normally contains the user login information. One point to be noted is that the only time you can use the Driver.connect() method directly is when you have created a new instance of the Driver class.

A null will be returned if an exception occurs when the Driver.connect() method is exe-cuted, which means that something went wrong during the connection operation.

Comparing the DriverManager.getConnection() method with the Driver.connect()

method, the following conclusions can be obtained:

  • The DriverManager.getConnection() method can perform checking and testing for each driver in the driver list automatically for all loaded drivers. As soon as a matching-driver is found, it can be connected to the database directly by using the Driver.con-nect() method. This automatic process will greatly reduce the processing time.
  • The DriverManager.getConnection() method has looser requirements for the arguments passed. When applying the Driver.connect() method, you have to pass two arguments, the URL as a string and the login properties as a Properties object with strict syntax and grammar requirements. However, when using the DriverManager.get-Connection() method, you can define login properties as either a String, a Properties object or even a null string, since the DriverManager can handle converting these argu-ments to the appropriate Properties object when it is applied.

From this comparison, it can be seen that the DriverManager.getConnection() method is better than the Driver.connect() method; therefore, we will use this method to makeour database connection in all example projects in this book.

After a driver has been loaded and registered, the next step is to establish a database connection using a URL. Before we can continue with the database connection, we need to have a clear picture and understanding of JDBC connection URLs.

4.2.2.4   The JDBC Connection URL

The JDBC URL provides all the information for applications to access a special resource, such as a database. Generally, a URL contains three parts or segments: protocol name, sub-protocol and subname for the database to be connected. Each of these three segments has different function, and they work together to provide unique information for the target database. The syntax for a JDBC URL is:

protocol:sub-protocol:subname

The protocol name works as an identifier or indicator to show what kind of protocol should be adopted when connecting to the desired database. For a JDBC driver, the name of the protocol should be jdbc. The protocol name is used to indicate what kind of items shouldbe delivered or connected.

The sub-protocol is generally used to indicate the type of the database or data source to be con-nected, such as sqlserver or oracle.

The subname is used to indicate the address to which the item supposed to be delivered or the location of the database. Generally, a subname contains the following information for an address of a resource:

  • Network host name/IP address
  • The database server name
  • The port number
  • The name of the database

An example of a subname for our Oracle Server database is:

localhost:1521:XE

The network host name is localhost, the server name is XE and the port number the server uses is 1521. You need to use a double slash, either forward or back, to represent a normal slash in this URL string, since this is a DOS-style string.

By combining all three segments, we can get a full JDBC URL. An example URL that uses an Oracle 18c XE thin JDBC driver is:

jdbc:oracle:thin:@localhost:1521:XE

The database name works as an attribute of the connected database.

Now that we have a clear picture ofthe JDBC URL, next let’s connect our application to our desired database.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post