Several steps of JDBC linking database

This article lists the four steps of JDBC linking database for your reference:

JDBC: Java database access solution.

Several steps: 1 Load driver class;

     2. Establish connection with database;

   3. Execute SQL statement

     4. Processing result set

     5. Close connection

1. Step 1: load driver class:

Note: different databases have different reference strings. The Oracle connection is class forName("oracle.jdbc.driver.OracleDriver"); After this step is executed, the program may throw classnotfoundexception for the following reasons:

a. The driver jar package of the database was not imported into the environment variable

b. Class. The string in forname is misspelled

2. Step 2: establish a connection with the database through drivermanager:

Its static method getconnection is used to get the connection. You usually need to pass in three parameters

Parameter 1: address and port of database (different database string contents are different)

Oracle address: JDBC: Oracle: thin: @ host: Port: sid

Parameter 2: user name of database

Parameter 3: password of user name corresponding to database

Connection conn = DriverManager. getConnect ("jdbc:oracle:thin:@host:port:oracle","user","psd");

3. Step 3: Java sql. Statement executes the SQL statement and gets the result

Statement state = conn.createStatement();

String SQL = "/ * here is the SQL statement * /";

Statement provides different execution methods for different SQL statements:

ResultSet executeQuery(String sql)

* this method is specially used to execute DQL statements. The returned resultset represents the queried result set

Int executeupdate (string SQL) * this method is specifically used to execute DML statements. The number returned indicates how many pieces of data in the table are affected by the execution of the statement

Boolean execute (string SQL) * theoretically, this method can execute any statement, but because DQL and DML have special methods to execute, this method is usually used to execute DDL statements

ResultSet rs = state. executeQuery(sql); Output query results: while (rs.next()) {output statement} resultset provides methods for traversing the result set:

boolean next()

* this method has two functions. First, when we query the result set, the pointer of RS points to the first data, so we need to call next() to move the pointer to the first data and represent the data. The second function is to look at the return value. If the pointer moves down and finds that there is no data, it will return false. If there is, it will return true. Therefore, we can obtain the value corresponding to each field of the current record only when this method returns true. RS also provides several getxxx (string fieldname) methods:

* these methods are used to obtain the value corresponding to a given field in the current record represented by rs. Different fields need to call corresponding methods due to different types

Step 4: close the connection and write it in the finally block

Put the database connection in a tool class to achieve the effect of reuse

Since accessing the database is a frequently used operation, a tool class for accessing the database is usually written in the project. After that, all operations accessing the database obtain connections from the tool class to implement the tool class in two ways:

1. Directly write the data configuration in the tool class dbutil

2. Write the database configuration in a properties property file, read the tool class into the property file, and obtain the database parameters line by line (generally use the second type)

If the first method is used, the data in the source code needs to be modified when the database used needs to be modified later, or the host, port, database connection name, password, etc., which is not convenient for system maintenance. Therefore, the second method is generally used, and the database connection tool class dbutil Main steps of Java and connection pool:

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
分享
二维码
< <上一篇
下一篇>>