Java insert modifies the basic method to delete database data

  • 2020-04-01 04:14:26
  • OfStack

Insert records for Java databases
There are three ways to insert data table records
1. Use Statement object

The syntax of the SQL statement to insert the data table record is:


 insert into  The name of the table ( The field name 1 The field name 2, ... )value ( The field values 1 Value of a field, 2 And... )


Such as:
 


  insert into ksInfo( Test number, name, grade, address, resume )value( ' 200701',' zhang '534 . ' Ouyang road, Shanghai 218 get 4-1202','')


The Java program code to achieve the same function is:


  sql =  " insert intoksIno( Test number, name, grade, address, resume ) " ;
  sql= = sq1+  " value( '" +txtNo.getTxt()+',' " +txtName.getText(0 " ', " ;
  sql = sql+txtScore.getText();
  sql=sql+ " ,' " +txtAddr.getText()+ " ',' " +txtResume.getText()+ " ') " ;
  stmt.executeUpdate(sql);

Second, use a ResultSet object

MoveToInsertRow (), a method of the ResultSet object, moves the table cursor to the insertion position, and after entering the data, inserts the record with the method insertRow(). For example, here is the code:


  String sql=  " select * from ksInfo " ;//Generate SQL statements
  ResultSet rs = stmt.executeQuery(sql);//Gets the data table result set
  rs.moveToInsertRow();//Moves the data table cursor to the insertion record position
  rs.updateString(1,'200701');//Fill in the test number field with data
  rs.updateString(2,' zhang ');//Fill the name field with data
  rs.updateInt(3,534);//Fill the grade field with data
  rs.updateString(4,' Ouyang road, Shanghai 218 get 4-1202');//Fill the address field with data
  rs.updateString(5,'');//Fill the resume field with data
  try{rs.insertRow();}catch(Exception e){};//Complete the insert

Use a PrepareStatement object

Similar to the way you would use a Statement object, except that when you create an SQL Statement, you temporarily use parameters. Represents the value, then generates a PrepareStatement object from the SQL statement object, and updates the record by setting the actual parameters when inserting. The code is as follows:


  sql =  " insert into ksInfo( Test number, name, grade, address, resume )value (?,?,?,?,'') " ;
  PrepareStatement pStmt = connect.prepareStatement(sql);
  pStmt.setString(1,'200701');//Fill in the test number field with data
  pStmt. setString (2,' zhang ');//Fill the name field with data
  pStmt.setInt(3,534);//Fill the grade field with data
  pStmt. setString (4,' Ouyang road, Shanghai 218 get 4-1202');//Fill the address field with data
  pStmt. setString (5,'');//Fill the resume field with data
  pStmt.executeUpdate();

There are three ways to insert data table records
1. Use Statement object

The syntax of the SQL statement to insert the data table record is:


  insert into  The name of the table ( The field name 1 The field name 2, ... )value ( The field values 1 Value of a field, 2 And... )


Such as:


  insert into ksInfo( Test number, name, grade, address, resume )value( ' 200701',' zhang '534 . ' Ouyang road, Shanghai 218 get 4-1202','')


The Java program code to achieve the same function is:


  sql =  " insert intoksIno( Test number, name, grade, address, resume ) " ;
  sql= = sq1+  " value( '" +txtNo.getTxt()+',' " +txtName.getText(0 " ', " ;
  sql = sql+txtScore.getText();
  sql=sql+ " ,' " +txtAddr.getText()+ " ',' " +txtResume.getText()+ " ') " ;
  stmt.executeUpdate(sql);


Second, use a ResultSet object

MoveToInsertRow (), a method of the ResultSet object, moves the table cursor to the insertion position, and after entering the data, inserts the record with the method insertRow(). For example, here is the code:


  String sql=  " select * from ksInfo " ;//Generate SQL statements
  ResultSet rs = stmt.executeQuery(sql);//Gets the data table result set
  rs.moveToInsertRow();//Moves the data table cursor to the insertion record position
  rs.updateString(1,'200701');//Fill in the test number field with data
  rs.updateString(2,' zhang ');//Fill the name field with data
  rs.updateInt(3,534);//Fill the grade field with data
  rs.updateString(4,' Ouyang road, Shanghai 218 get 4-1202');//Fill the address field with data
  rs.updateString(5,'');//Fill the resume field with data
  try{rs.insertRow();}catch(Exception e){};//Complete the insert

Use a PrepareStatement object

Similar to the way you would use a Statement object, except that when you create an SQL Statement, you temporarily use parameters. Represents the value, then generates a PrepareStatement object from the SQL statement object, and updates the record by setting the actual parameters when inserting. The code is as follows:


  sql =  " insert into ksInfo( Test number, name, grade, address, resume )value (?,?,?,?,'') " ;
  PrepareStatement pStmt = connect.prepareStatement(sql);
  pStmt.setString(1,'200701');//Fill in the test number field with data
  pStmt. setString (2,' zhang ');//Fill the name field with data
  pStmt.setInt(3,534);//Fill the grade field with data
  pStmt. setString (4,' Ouyang road, Shanghai 218 get 4-1202');//Fill the address field with data
  pStmt. setString (5,'');//Fill the resume field with data
  pStmt.executeUpdate();

Java database modification record
There are also three options for modifying data table records.
1. Use Statement object

The syntax of the SQL statement to modify the data table record is:


  update The name of the table  set  The field name 1 =  The field values 1 The field name 2 =  The field values 2 And... where Certain conditions 


Such as:


  update ksInfo set  The name  =  'xiao-wei zhang 'where  The name  =  "Zhang '

Start by creating an SQL Statement and then calling the executeUpdate() method of the Statement object. For example,


  sql =  " update ksInfo set  The name  =  '" +txtName.getText();
  sql = sql +  " , results = " +txtScore.getText();
  sql = sql + " , address =' " +txtAddr.getText();
  sql= sql+ " ',, resume =' " +txtResume.getText()+ " 'where  Candidate number = " +txtNo.getText();
  stmt.executeUpdate(sql);


Second, use a ResultSet object

First set up the ResultSet object, then directly set the field value of the record, modify the record of the data table. For example,


  String sql =  " select * from ksInfo where  The name =' zhang ' " ;//Generate SQL statements
  ResultSet rs = stmt.executeQuery(sql);//Gets the data table result set
  if(rs.next()){
    rs.updateString(2,' Xiao-wei zhang ');
    try{rs.updateRow();}catch(Exception e){}
  }

Use a PrepareStatement object

When creating an SQL statement, temporarily use parameters? Represents the value, and then the PrepareStatement object is generated by the SQL statement object, which then updates the record by setting the actual parameters. Schematic code:


  sql =  " update ksInfo set  The name =? where  The name  =  "Zhang ';
  PrepareStatement pStmt = connect.prepareStatement(sql);
  pStmt.setString(2,' Xiao-wei zhang ');//Fill the name field with data
  pStmt.executeUpdate();

Delete records for Java databases
There are also three options for deleting tables
1. Use Statement object

The syntax of the SQL statement that deletes the data table record is:


  delete from  The name of the table  where  Certain conditions 

Such as:


  delete from ksInfo where  The name  =  "Zhang '


Create an SQL Statement, then call the Statement object's executeUpdate() method:


  stmt.executeUpdate(sql);


Second, use a ResultSet object

You create an SQL Statement and then call the executeUpdate() method of the Statement object. Such as:


  String sql =  " select * from ksInfo where  The name  =  "Zhang ' " ;//Generate SQL statements
  ResultSet rs = stmt.executeQuery(sql);//Gets the data table result set
  if(rs.next()){
    rs.deleteRow();try{ rs.updateRow();}catch(Exception e){}
  }

Use a PrepareStatement object

When creating an SQL statement, temporarily use parameters? Represents the value, and then the PrepareStatement object is generated by the SQL statement object, which then sets the actual parameters to achieve the deletion of a particular record. For example, here is the code:


  sql =  " delete form ksInfo where  The name =? " ;
  PrepareStatement pStmt = connect.prepareStatement(sql);
  pStmt.setString(2,' zhang ');//Specify the data for the name field
  pStmt.executeUpdate();


Related articles: