"Go" Java Insert Modify the basic method of deleting database data __c#

Source: Internet
Author: User
Tags gettext stmt

Submission: Goldensun font: [ increase decrease] Type: Reprint time: 2015-10-12 I want to comment this article mainly introduces the Java insert modifies the basic method that deletes the database data, is the elementary knowledge in the Java introductory study, Need friends can refer to the following

Insert record of Java database
There are 3 scenarios for inserting data table records
I. Using the Statement object

The syntax for implementing SQL statements that insert data table records is:

Insert into table name (field name 1, field name 2,......) Value (field value 1, field value 2, ...)


For example:

  INSERT into Ksinfo (exam number, name, score, address, resume) value (' 200701 ', ' Zhang Dawei ' 534, ' Shanghai Ouyang Road 218 Get 4-1202 ', ')


The Java program code that implements the same functionality 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);

Two. Using the ResultSet object

Use the ResultSet object's method Movetoinsertrow () to move the datasheet cursor to the insertion position, and after entering the data, insert the record with the method InsertRow (). For example, the following schematic code:

  String sql= "SELECT * from Ksinfo";//Generate SQL statement
  ResultSet rs = stmt.executequery (sql); Get data table result set
  Rs.movetoinsertrow ()//() move the datasheet cursor to the insertion record position
  rs.updatestring (1, ' 200701 ');//Enter the test number field into the Data
  rs.updatestring (2, ' Zhang Dawei ') //Fill in the Name field with Data
  rs.updateint (3,534);//Enter data into the score field
  rs.updatestring (4, ' Shanghai Ouyang Lu 218 Get 4-1202 ');//Fill in the Address field with data
  Rs.updatestring (5, '); Enter data into the Resume field
  Try{rs.insertrow ();} catch (Exception e) {};//complete insert

Three. Using the Preparestatement object

Similar to the method used to use the statement object, you only use parameters temporarily when you create the SQL statement. Represents a value, and then generates a Preparestatement object by the SQL statement object, which, when inserted, implements an update of the record by setting the actual parameters. The schematic code is as follows:

  sql = "INSERT into Ksinfo (test number, name, score, address, resume) value (?,?,?,?, ')";
  Preparestatement pstmt = connect.preparestatement (sql);
  Pstmt.setstring (1, ' 200701 '); Fill in the data in the Test field
  pstmt. setstring (2, ' Zhang Dawei ');//Fill in the Name field with the data
  pstmt.setint (3,534);// Fill in the data to the score field
  pstmt. setstring (4, ' Shanghai Ouyang Lu 218 Lane 4-1202 ');//Fill in the Address field with Data
  pstmt. SetString (5, '); Fill in the data
  in the Resume field Pstmt.executeupdate ();

There are 3 scenarios for inserting data table records
I. Using the Statement object

The syntax for implementing SQL statements that insert data table records is:

  Insert into table name (field name 1, field name 2,......) Value (field value 1, field value 2, ...)


For example:

  INSERT into Ksinfo (exam number, name, score, address, resume) value (' 200701 ', ' Zhang Dawei ' 534, ' Shanghai Ouyang Road 218 Get 4-1202 ', ')


The Java program code that implements the same functionality 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);


Two. Using the ResultSet object

Use the ResultSet object's method Movetoinsertrow () to move the datasheet cursor to the insertion position, and after entering the data, insert the record with the method InsertRow (). For example, the following schematic code:

  String sql= "SELECT * from Ksinfo";//Generate SQL statement
  ResultSet rs = stmt.executequery (sql); Get data table result set
  Rs.movetoinsertrow ()//() move the datasheet cursor to the insertion record position
  rs.updatestring (1, ' 200701 ');//Enter the test number field into the Data
  rs.updatestring (2, ' Zhang Dawei ') //Fill in the Name field with Data
  rs.updateint (3,534);//Enter data into the score field
  rs.updatestring (4, ' Shanghai Ouyang Lu 218 Get 4-1202 ');//Fill in the Address field with data
  Rs.updatestring (5, '); Enter data into the Resume field
  Try{rs.insertrow ();} catch (Exception e) {};//complete insert

Three. Using the Preparestatement object

Similar to the method used to use the statement object, you only use parameters temporarily when you create the SQL statement. Represents a value, and then generates a Preparestatement object by the SQL statement object, which, when inserted, implements an update of the record by setting the actual parameters. The schematic code is as follows:

  sql = "INSERT into Ksinfo (test number, name, score, address, resume) value (?,?,?,?, ')";
  Preparestatement pstmt = connect.preparestatement (sql);
  Pstmt.setstring (1, ' 200701 '); Fill in the data in the Test field
  pstmt. setstring (2, ' Zhang Dawei ');//Fill in the Name field with the data
  pstmt.setint (3,534);// Fill in the data to the score field
  pstmt. setstring (4, ' Shanghai Ouyang Lu 218 Lane 4-1202 ');//Fill in the Address field with Data
  pstmt. SetString (5, '); Fill in the data
  in the Resume field Pstmt.executeupdate ();

Java Database modification Record
There are 3 different scenarios for modifying data table records.
I. Using the Statement object

The syntax for implementing SQL statements that modify data table records is:

  Update table name SET field Name 1 = field value 1, field name 2 = field value 2,......where specific condition


For example:

  Update Ksinfo Set name = ' Zhang Xiao wei ' where name = ' Zhang Dawei '

First create an SQL statement, and then 砶 invoke the Executeupdate () method of the Statement object. For example

  sql = "Update ksinfo set name = '" +txtname.gettext ();
  sql = SQL + ", score =" +txtscore.gettext ();
  sql = SQL + ", address = '" +txtaddr.gettext ();
  Sql= sql+ "',, CV = '" +txtresume.gettext () + "' where test number =" +txtno.gettext ();
  Stmt.executeupdate (SQL);


Two. Using the ResultSet object

First set up the ResultSet object, and then directly set the record field values, modify the data table records. For example

  String sql = "SELECT * from ksinfo where name = ' Zhang Dawei '";//Generate SQL statement
  ResultSet rs = stmt.executequery (sql); Get data table result set
  I F (Rs.next ()) {
    rs.updatestring (2, ' Zhang Xiao Wei ');
    Try{rs.updaterow ();} catch (Exception e) {}
  }

Three. Using the Preparestatement object

When creating an SQL statement, use parameters temporarily. Represents the value, then generates the Preparestatement object by the SQL statement object, and then implements the update of the record by setting the actual parameters. Schematic code:

  sql = "Update ksinfo set name =?" where name = ' Zhang Dawei ';
  Preparestatement pstmt = connect.preparestatement (sql);
  Pstmt.setstring (2, ' Zhang Xiao Wei ');//Fill in the Name field with the data
  pstmt.executeupdate ();

Java Database Delete Record
There are 3 different scenarios for deleting a datasheet
I. Using the Statement object

The syntax of the SQL statement that deletes a datasheet record is:

  Delete from table name where specific condition

For example:

  Delete from ksinfo where name = ' Zhang Dawei '


First create an SQL statement, and then call the Executeupdate () method of the Statement object:

  Stmt.executeupdate (SQL);


Two. Using the ResultSet object

First create an SQL statement, and then call the Executeupdate () method of the Statement object. For example:

  String sql = "SELECT * from ksinfo where name = ' Zhang Dawei '";//Generate SQL statement
  ResultSet rs = stmt.executequery (sql); Get data table result set
  if (Rs.next ()) {
    rs.deleterow (); try{rs.updaterow ();} catch (Exception e) {}
  }

Three. Using the Preparestatement object

When creating an SQL statement, use parameters temporarily. Represents the value, then generates the Preparestatement object by the SQL statement object, and then sets the actual parameter to implement the deletion of the specific record. For example, the following schematic code:

  sql = "Delete form ksinfo where name =?";
  Preparestatement pstmt = connect.preparestatement (sql);
  Pstmt.setstring (2, ' Zhang Dawei ');//Specify Data for the Name field
  pstmt.executeupdate ();
Related Article

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.