Home  >  Article  >  Java  >  How to open database in java?

How to open database in java?

coldplay.xixi
coldplay.xixiOriginal
2020-06-13 10:39:104204browse

How to open database in java?

How to open the database in java?

How to open the database in java:

1. Get the database connection

How to open database in java?

#The url in the code is the jdbc database connection address, user is the username, and password is the password. These are all It is set by the developer himself. driver is the class name of the driver, which for MySQL is com.mysql.jdbc.Driver. The static statement means to register the driver when the class is initialized. This step is necessary. If the driver is not registered, the database connection cannot be used. Next, in the getConnection method, we use DriverManager to obtain the database connection and save it in the global variable connection. This is to reuse the database connection and prevent frequent opening and closing.

2. Compile sql statement

How to open database in java?

We first wrote a sql statement: insert into user(id,name)values( ?,?). It means to insert the id and name into the user table. The two question marks are placeholders. Then compile the sql through PreparedStatement, and then use the setXXX method to replace the two places containing the question marks with real data. , according to the implementation in the code, the final SQL statement will become: insert into user(id,name)values(1,'test').

3. Execute sql statement

In the above figure, we use PreparedStatement to compile sql. After the compilation is completed, we can use its execute method to execute. At this time, JDBC will use the underlying MySQL driver to send real sql commands to the remote database to complete database insertion.

4. Processing return results

Processing return results are generally used in query statements, as shown below:

How to open database in java?

The execution of query statements uses executeQuery instead of execute. It will return a

ResultSet, through which we can get the returned value. ResultSet fetches data row by row. It has a next method to determine whether there is still data. If there is still data, the next row will be fetched. For a row of data, we need to know its column name, and then use getXXX to get the column value.

5. Close the connection

Close the connection using the close method of connection. However, since the establishment of a database connection is relatively expensive, we generally do not close it, but reuse one or more database connections to improve system performance.

Recommended tutorial: "JAVA Video Tutorial"

The above is the detailed content of How to open database in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn