How to insert and delete a row programmatically ? (new feature in JDBC 2.0)
insetation Hi friends,
With the help of this example, you can update and delete data from ResultSet programmaticaly. Make sure the resultset is updatable.
Move the cursor to the specific position.
rs.moveToCurrentRow();
set value for each column.
//to set up for insert
rs.moveToInsertRow();
rs.updateString("Column_Name1" "Column-Value");
rs.updateInt("Column_Name1",Column-Value");
...
Call inserRow() method to finish the row insert process. rs.insertRow();
To delete a row: move to the specific position and call deleteRow() method:
rs.absolute(5);
//delete row 5
rs.deleteRow();
To see the changes call refreshRow();
rs.refreshRow();
// .....................Java Code...............
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class InsertNDeleteRowOfResultSet {
public static void main(String[] args) { try { String mysqlDriver = "com.mysql.jdbc.Driver"; Class.forName(mysqlDriver).newInstance(); String connectionString = "jdbc:mysql://192.168.10.13:3306/studentinformation"; String username = "root"; String password = "root"; Connection con = null; Statement stmt = null; ResultSet rs = null; con = DriverManager.getConnection(connectionString, username, password); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sqlString = "Select * from s_infor;"; rs = stmt.executeQuery(sqlString); System.out .println("---------Student Information----------"); while (rs.next()) { int roll = rs.getInt("s_Roll"); String Name = rs.getString("s_Name"); String Address = rs.getString("s_Address"); System.out.println(roll + " " + Name + " " + Address); } // Insert Row rs.moveToInsertRow(); rs.updateInt("s_Roll", 4); rs.updateString("s_Name", "Gyan"); rs.updateString("s_Address", "Bareilly"); rs.insertRow(); rs.beforeFirst(); System.out.println("---------Student Information(After insertation a row)----------"); while (rs.next()) { int roll = rs.getInt("s_Roll"); String Name = rs.getString("s_Name"); String Address = rs.getString("s_Address"); System.out.println(roll + " " + Name + " " + Address); } // Delete row rs.absolute(2); rs.deleteRow(); rs.beforeFirst(); System.out .println("---------Student Information(After deletion a row)----------"); while (rs.next()) { int roll = rs.getInt("s_Roll"); String Name = rs.getString("s_Name"); String Address = rs.getString("s_Address"); System.out.println(roll + " " + Name + " " + Address); } } catch (Exception e) { System.out.println(e.getMessage()); } }
}
Thanks.
insetation Hi friends,
With the help of this example, you can update and delete data from ResultSet programmaticaly. Make sure the resultset is updatable.
Move the cursor to the specific position. rs.moveToCurrentRow(); set value for each column. //to set up for insert rs.moveToInsertRow(); rs.updateString("Column_Name1" "Column-Value"); rs.updateInt("Column_Name1",Column-Value"); ... Call inserRow() method to finish the row insert process. rs.insertRow();
To delete a row: move to the specific position and call deleteRow() method:
rs.absolute(5); //delete row 5 rs.deleteRow();
To see the changes call refreshRow(); rs.refreshRow(); // .....................Java Code............... import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class InsertNDeleteRowOfResultSet {
public static void main(String[] args) { try { String mysqlDriver = "com.mysql.jdbc.Driver"; Class.forName(mysqlDriver).newInstance(); String connectionString = "jdbc:mysql://192.168.10.13:3306/studentinformation"; String username = "root"; String password = "root"; Connection con = null; Statement stmt = null; ResultSet rs = null; con = DriverManager.getConnection(connectionString, username, password);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sqlString = "Select * from s_infor;"; rs = stmt.executeQuery(sqlString); System.out .println("---------Student Information----------"); while (rs.next()) { int roll = rs.getInt("s_Roll"); String Name = rs.getString("s_Name"); String Address = rs.getString("s_Address"); System.out.println(roll + " " + Name + " " + Address); } // Insert Row rs.moveToInsertRow(); rs.updateInt("s_Roll", 4); rs.updateString("s_Name", "Gyan"); rs.updateString("s_Address", "Bareilly"); rs.insertRow(); rs.beforeFirst(); System.out.println("---------Student Information(After insertation a row)----------"); while (rs.next()) { int roll = rs.getInt("s_Roll"); String Name = rs.getString("s_Name"); String Address = rs.getString("s_Address"); System.out.println(roll + " " + Name + " " + Address); } // Delete row rs.absolute(2); rs.deleteRow(); rs.beforeFirst(); System.out .println("---------Student Information(After deletion a row)----------"); while (rs.next()) { int roll = rs.getInt("s_Roll"); String Name = rs.getString("s_Name"); String Address = rs.getString("s_Address"); System.out.println(roll + " " + Name + " " + Address); }
} catch (Exception e) { System.out.println(e.getMessage()); } }
}
===============OutPut=================
---------Student Information----------
1 Rohit Barabanki
2 Arunesh Allahabad
3 Bharat Bareilly
---------Student Information(After insertation a row)----------
1 Rohit Barabanki
2 Arunesh Allahabad
3 Bharat Bareilly
4 Gyan Bareilly
---------Student Information(After deletion a row)----------
1 Rohit Barabanki
3 Bharat Bareilly
4 Gyan Bareilly
Thanks.
Ads