Modify data through JDBC

Source: Internet
Author: User

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.