00311_ Preprocessing Object Executeupdate method (implement database increment, delete, change)

Source: Internet
Author: User

1. Overview

(1) The execution of the recorded Insert\update\delete statement is accomplished by preprocessing the Executeupdate method of the object;

(2) The operation format is unified as follows:

① Registration Drive;

② get the connection;

③ gets the preprocessing object;

The ④sql statement placeholder sets the actual parameters;

⑤ Execute SQL statement;

⑥ release resources.

2, insert the RECORD: INSERT, implement to insert the specified new classification into the classification table

1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.PreparedStatement;4 5  Public classDemo01 {6      Public Static voidMain (string[] args)throwsException {7         //1 Registration Drive8Class.forName ("Com.mysql.jdbc.Driver");9         //2 Getting the connectionTenConnection conn =Drivermanager.getconnection ( One"Jdbc:mysql://localhost:3306/mybase", "root", "root"); A         //3 getting preprocessed objects -String sql = "INSERT into sort (sname) values (?)"; -PreparedStatement stat =conn.preparestatement (SQL); the         //4 SQL statement placeholder set actual parameters -Stat.setstring (1, "Air conditioning"); -         //5 Execute SQL statement -         intline =stat.executeupdate (); +System.out.println ("Number of newly added records:" +Line ); -         //6 Releasing Resources + stat.close (); A conn.close (); at  -     } -}

Operation Result:

  

  

3. Update record: Update, implement the classification name of the record corresponding to the category ID specified in the classification table.

1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.PreparedStatement;4 5  Public classDemo02 {6      Public Static voidMain (string[] args)throwsException {7         //1 Registration Drive8Class.forName ("Com.mysql.jdbc.Driver");9         //2 Getting the connectionTenConnection conn =Drivermanager.getconnection ( One"Jdbc:mysql://localhost:3306/mybase", "root", "root"); A         //3 Getting the preprocessed object -String sql = "Update sort set sname=?" where sid=? "; -PreparedStatement stat =conn.preparestatement (SQL); the         //4 SQL statement placeholder set actual parameters -Stat.setstring (1, "digital products"); -Stat.setint (2, 1);//the back of this 1 refers to the SID -         //5 Execute SQL statement +         intline =stat.executeupdate (); -System.out.println ("Number of update records:" +Line ); +         //6 Releasing Resources A stat.close (); at conn.close (); -  -     } -}

Operation Result: "Home appliance" updated to "digital product"

  

4, delete records: Delete, implementation of the deletion of the classification table in the specified category ID records

1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 Importjava.sql.PreparedStatement;4 5  Public classDemo03 {6      Public Static voidMain (string[] args)throwsException {7         //1 Registration Drive8Class.forName ("Com.mysql.jdbc.Driver");9         //2 Getting the connectionTenConnection conn =Drivermanager.getconnection ( One"Jdbc:mysql://localhost:3306/mybase", "root", "root"); A         //3 getting preprocessed objects -String sql = "Delete from sort where sid=?"; -PreparedStatement stat =conn.preparestatement (SQL); the         //4 SQL statement placeholder set actual parameters -Stat.setint (1, 1); -         //5 Execute SQL statement -         intline =stat.executeupdate (); +System.out.println ("Number of deleted records:" +Line ); -         //6 Releasing Resources + stat.close (); A conn.close (); at  -     } -}

Operation Result: The product of Sid 1 was deleted

  

    

00311_ Preprocessing Object Executeupdate method (implement database increment, delete, change)

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.