Java language code for adding, deleting, modifying and querying data in MySQL database

To put it simply, the steps are as follows:

1. Connect to the database

2. Send SQL statements to the database

3. Execute SQL statement

Here is an example:

In a database, there is a students table with student ID, name, sex, address, phone and Dept.

Here, write this table as an info_student

(please make sure you read the example first, otherwise you may not understand some parts of the code)

To achieve manipulation, we must first connect to the database, because each operation needs to be connected, so we directly encapsulate the connected operation in a class, and call it directly when we need to connect.

Database connection class:

The database has been connected, so the next step is to send SQL statements and execute statements.

The sending statement uses the operation preparestatement() of Preparedstatement object and connection object

Execute() of the Preparedstatement object used in the execution statement

Tip: the following is a description of some objects. You can look at the code first and come back when you encounter it.

************************

PreparedStatement

An object that represents a precompiled SQL statement.

The SQL statements are precompiled and stored in the Preparedstatement object. You can then use this object to execute the statement multiple times efficiently.

*************************

Connection

Connection (session) to a specific database. Execute SQL statements in the connection context and return results.

The database of the connection object can provide information describing its tables, supported SQL syntax, stored procedures, this connection function, and so on.

**********************

The following code is to realize the addition, deletion, modification and query of student information in the database.

1、 Increase

2、 Delete

3、 Change

4、 Check

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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