Method of connecting database and creating program using JDBC API in Java programming

JDBC connection database

The programming involved in establishing a JDBC connection is quite simple. Here are four simple steps:

Import JDBC package: the import statement tells the java compiler where to find the class referenced in the code and place it at the beginning of your source code.

Use the standard JDBC package, which allows you to select, insert, update and delete data in SQL tables, and add the following imports to your source code:

Register JDBC Driver: before using it, you must register your driver. The registered driver is a process in which the class file of the Oracle driver is loaded into memory so that it can be used as the implementation of the JDBC interface.

You need to do this registration only once in your program. You can register a driver in one of two ways.

Method (I) - class. Forname(): the most commonly used method in registering a driver is to use Java class The forname () method dynamically loads the class file of the driver into memory, and it will automatically register it. This method is desirable because it allows the driver to be registered and configured for portability.

The following example uses class Forname() to register the Oracle driver:

You can use the getInstance () method to resolve incompatible JVMs, but write two additional exceptions as follows:

Method (2) - drivermanager. Registerdriver (): the second method that can be used to register a driver is to use staticdrivermanager Registerdriver() method.

Yes, if you are using an incompatible JDK JVM, for example, Microsoft provides a method using registerdriver ().

The following example uses registerdriver() to register the Oracle driver:

Database URL formulation: when loading the driver, you can build the program using drivermanager The connection of the getconnection () method. For ease of reference, let's list three overloaded drivermanagers Getconnection() method:

Here, each form requires a database URL. The URL of the database is the address pointing to the database.

Formulating a database URL is mostly used to establish a connection.

The following table lists the popular jdbc driver names and database URLs.

RDBMS jdbc driver name URL format

In the URL format, all highlighted parts are static, and only the remaining parts need to be changed according to the database settings.

Create a connection object: use the user name and password of the database URL: drivermanager in the following three forms Getconnection () method to create a connection object. The most common form of getconnection() requires passing a database URL, username and password:

The value of databasename in the URL database part: assuming that Oracle's thin driver is used, a host port needs to be specified.

Suppose there is a host with TCP / IP address 192.0 0.1 and the host name and Oracle listener are configured on port 1521, the database name is EMP, and then the complete database URL is:

Now, you must call the appropriate user name and password and the getconnection () method to get a connection object, as follows:

Use only one database URL: the second form, drivermanager The getconnection () method requires only one database URL:

However, in this case, the URL of the database, including user name and password, has the following general form:

Therefore, the above connection can be created as follows:

Use the URL of the database and a properties object: the third form, drivermanager The getconnection () method requires a database URL and a properties object:

DriverManager. getConnection(String url,Properties info); Properties object that holds a set of key value pairs. When it is used to call the getconnection () method, the driver properties are passed to the driver.

In order to make the same connection made in the previous example, use the following code:

Close JDBC connection: at the end of the JDBC program, it explicitly requires that all connections to the database be closed to end each database session. However, if you forget, the Java garbage collector will close the connection and it will clean up the stale objects.

Relying on garbage collection, especially in database programming, is a very poor programming habit. You should always be in the habit of closing the close () method associated with the connection object.

To ensure that the connection is closed, it can be executed in the finally block in the code. Finally blocks are executed regardless of whether they occur or not.

To close the connection opened above, you should call the close () method, as follows:

conn.close(); Explicitly closing the connection to the DBMS saves resources.

Create a JDBC application: there are six steps involved in building a JDBC application:

Example code: the example of this example can be used as a template to establish a JDBC application when necessary.

Based on the environment and database installation, this sample code has been written in the previous chapter.

Copy the following example firstexample Java, compile and run as follows:

Now compile the above example as follows:

When you run firstexample, it produces the following results:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>