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)