Insert records in Java database <br />There are three options for inserting data table records: 1. Use Statement object
The syntax of the SQL statement to insert records into the data table is:
insert into table name (field name 1, field name 2,...) value (field value 1, field value 2,...)
For example:
insert into ksInfo(examination number, name, grades, address, resume) value('200701','David Zhang'534,'4-1202, Lane 218, Ouyang Road, Shanghai','')
The Java program code to achieve the same function is:
sql = "insert intoksIno(examination number, name, grades, address, resume)"; sql= = sq1+ "value('"+txtNo.getTxt()+','"+txtName.getText(0"',"; sql = sql+txtScore.getText(); sql=sql+”,'”+txtAddr.getText()+”’,’”+txtResume.getText()+”’)”; stmt.executeUpdate(sql);
2. Use ResultSet object
Use the method moveToInsertRow() of the ResultSet object to move the data table cursor to the insertion position. After entering the data, use the method insertRow() to insert the record. For example, the following code:
String sql= “select * from ksInfo”;//Generate SQL statement ResultSet rs = stmt.executeQuery(sql);//Get the data table result set rs.moveToInsertRow();//Move the data table cursor to the insertion record position rs.updateString(1,'200701');//Fill in the examination number field Data rs.updateString(2,'David Zhang');//Fill in the name field with data r s.updateInt(3,534);//Fill in the data into the score field rs.updateString(4,'Shanghai Ouyang Road Lane 218 4-1202');//Fill in the data into the address field rs.updateString(5,'') ;//Fill in data into the resume field try{rs.insertRow();}catch(Exception e){};//Complete insertion
3. Use PrepareStatement object
Similar to the method of using Statement object, but only temporarily using parameters when creating SQL statements? Represents the value, and then generates a PrepareStatement object from the SQL statement object. During insertion, the record is updated by setting actual parameters. The schematic code is as follows:
sql = "insert into ksInfo(examination number, name, grades, address, resume) value (?,?,?,?,'')"; PrepareStatement pStmt = connect.prepareStatement(sql); pStmt.setString(1,' 200701');//Fill in data pStmt.setString into the examination number field (2,'David Zhang');//Fill the data into the name field pStmt.setInt(3,534);//Fill the data into the grade field pStmt.setString (4,'Shanghai Ouyang Road Lane 218 4-1202'); //Fill in data into the address field pStmt.setString (5,'');//Fill in data into the resume field pStmt.executeUpdate();
There are three options for inserting data table records <br />1. Using Statement objects
The syntax of the SQL statement to insert records into the data table is:
insert into table name (field name 1, field name 2,...) value (field value 1, field value 2,...)
For example:
insert into ksInfo(examination number, name, grades, address, resume) value('200701','David Zhang'534,'4-1202, Lane 218, Ouyang Road, Shanghai','')
The Java program code to achieve the same function is:
sql = "insert intoksIno(examination number, name, grades, address, resume)"; sql= = sq1+ "value('"+txtNo.getTxt()+','"+txtName.getText(0"',"; sql = sql+txtScore.getText(); sql=sql+”,'”+txtAddr.getText()+”’,’”+txtResume.getText()+”’)”; stmt.executeUpdate(sql);
2. Use ResultSet object
Use the method moveToInsertRow() of the ResultSet object to move the data table cursor to the insertion position. After entering the data, use the method insertRow() to insert the record. For example, the following code:
String sql= “select * from ksInfo”;//Generate SQL statement ResultSet rs = stmt.executeQuery(sql);//Get the data table result set rs.moveToInsertRow();//Move the data table cursor to the insertion record position rs.updateString(1,'200701');//Fill in the examination number field Data rs.updateString(2,'David Zhang');//Fill in the name field with data r s.updateInt(3,534);//Fill in the data into the score field rs.updateString(4,'Shanghai Ouyang Road Lane 218 4-1202');//Fill in the data into the address field rs.updateString(5,'') ;//Fill in data into the resume field try{rs.insertRow();}catch(Exception e){};//Complete insertion
3. Use PrepareStatement object
Similar to the method of using Statement object, but only temporarily using parameters when creating SQL statements? Represents the value, and then generates a PrepareStatement object from the SQL statement object. During insertion, the record is updated by setting actual parameters. The schematic code is as follows:
sql = "insert into ksInfo(examination number, name, grades, address, resume) value (?,?,?,?,'')"; PrepareStatement pStmt = connect.prepareStatement(sql); pStmt.setString(1,' 200701');//Fill in data pStmt.setString into the examination number field (2,'David Zhang');//Fill the data into the name field pStmt.setInt(3,534);//Fill the data into the grade field pStmt.setString (4,'Shanghai Ouyang Road Lane 218 4-1202'); //Fill in data into the address field pStmt.setString (5,'');//Fill in data into the resume field pStmt.executeUpdate();
Java database modification records <br />There are three options for modifying data table records.
1. Use Statement object
The syntax of the SQL statement to modify the data table records is:
update table name set field name 1 = field value 1, field name 2 = field value 2,...where specific conditions
For example:
update ksInfo set name='Zhang Xiaowei'where name='Zhang David'
First create a SQL statement, and then call 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+”’,, resume=’”+txtResume.getText()+”’where exam number=”+txtNo.getText(); stmt.executeUpdate(sql);
2. Use ResultSet object
First create a ResultSet object, then directly set the field values of the record and modify the records of the data table. For example,
String sql = "select * from ksInfo where name='David Zhang'";//Generate SQL statement ResultSet rs = stmt.executeQuery(sql);//Get the data table result set if(rs.next()){ rs. updateString(2,'Zhang Xiaowei'); try{rs.updateRow();}catch(Exception e){} }
3. Use PrepareStatement object
When creating a SQL statement, temporarily use parameters? represents the value, then generates a PrepareStatement object from the SQL statement object, and then updates the record by setting actual parameters. Indicative code:
sql = "update ksInfo set name =? where name = 'David Zhang'; PrepareStatement pStmt = connect.prepareStatement(sql); pStmt.setString(2,'Zhang Xiaowei');//Fill in the name field with data pStmt.executeUpdate( );
Deleting records in Java database <br />There are three options for deleting data tables: 1. Use the Statement object
The syntax of the SQL statement to delete data table records is:
delete from table name where specific conditions
For example:
delete from ksInfo where name='David Zhang'
First create a SQL statement, and then call the executeUpdate() method of the Statement object:
stmt.executeUpdate(sql);
2. Use ResultSet object
First create a SQL statement, and then call the executeUpdate() method of the Statement object. For example:
String sql = "select * from ksInfo where name = 'David Zhang'";//Generate SQL statement ResultSet rs = stmt.executeQuery(sql);//Get the data table result set if(rs.next()){ rs. deleteRow();try{ rs.updateRow();}catch(Exception e){} }
3. Use PrepareStatement object
When creating a SQL statement, temporarily use parameters? represents the value, then generates a PrepareStatement object from the SQL statement object, and then sets the actual parameters to achieve the deletion of specific records. For example, the following code:
sql = "delete form ksInfo where name=?"; PrepareStatement pStmt = connect.prepareStatement(sql); pStmt.setString(2,'David Zhang');//Specify data for the name field pStmt.executeUpdate();