Assume that there is a data table named "people". Its column structure and data content are as follows:
| ID (column type varchar) |
Name (column type varchar) |
Salary (column type INT) |
| 001 |
Wang Xiaoming |
30000 |
| 002 |
Chen Yongnian |
35000 |
| 003 |
Wang xinguo |
28000 |
A connection con has been created.
1. Use SQL syntax to add and modify and delete data
1. Add a new item
Add a new number 004, name Liu Shaoqi, salary 31000
String insertstr = "insert into people (ID, name, salary) values (?,?,?) ";
Preparedstatement pstmt = con. preparestatement (insertstr );
Pstmt. setstring (1, "004 ");
Pstmt. setstring (2, "Liu Shaoqi ");
Pstmt. setint (3,31000 );
Pstmt.exe cuteupdate ();
2. modify a document
Change the salary information of Wang Xiaoming at 001 to 27000
String updatestr = "Update people set salary =? Where id =? ";
Preparedstatement pstmt = con. preparestatement (updatestr );
Pstmt. setint (1, 27000 );
Pstmt. setstring (2, "001 ");
Pstmt.exe cuteupdate ();
3. delete a document
Delete information from Wang xinguo, 003
String delstr = "delete from people where id =? ";
Preparedstatement pstmt = con. preparestatement (delstr );
Pstmt. setstring (1, "003 ");
Pstmt.exe cuteupdate ();
Ii. Use resultset (SQL syntax-free) to add and delete data
Applicable to the jdbc2.0 driver, and the driver can be used in the following ways:
1. Add a new item
Add a new number 004, name Liu Shaoqi, salary 31000
Statement stmt = con. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs1_stmt.exe cutequery ("select * from people order by ID ");
Rs. movetoinsertrow ();
Rs. updatestring ("ID", "004"); // or Rs. updatestring (1, "004); indicates the first column
Rs. updatestring ("name", "Liu Shaoqi ");
Rs. updateint ("salary", 31000 );
Rs. insertrow ();
2. modify a document
Change the salary information of Wang Xiaoming at 001 to 27000
Statement stmt = con. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs1_stmt.exe cutequery ("select * from people order by ID ");
Rs. Absolute (1); // move the cursor to the first file of Wang Xiaoming
Rs. updateint ("salary", 27000 );
Rs. updaterow ();
3. delete a document
Delete information from Wang xinguo, 003
Statement stmt = con. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Resultset rs1_stmt.exe cutequery ("select * from people order by ID ");
Rs. Absolute (3); // move the cursor to the third item of Wang xinguo
Rs. deleterow ();