insert and delete a row programmatically

insert and delete a row programmatically

How to insert and delete a row programmatically ? (new feature in JDBC 2.0)

View Answers

November 13, 2010 at 4:25 PM

insetation Hi friends,

With the help of this example, you can update and delete data from ResultSet programmaticaly. Make sure the resultset is updatable.

  1. Move the cursor to the specific position.

    rs.moveToCurrentRow();

  2. set value for each column.
    //to set up for insert
    rs.moveToInsertRow();
    rs.updateString("Column_Name1" "Column-Value");
    rs.updateInt("Column_Name1",Column-Value");
    ...

  3. 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.


November 13, 2010 at 4:29 PM

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.









Related Tutorials/Questions & Answers:
insert and delete a row programmatically
insert and delete a row programmatically  How to insert and delete a row programmatically ? (new feature in JDBC 2.0
delete row from a table in hibernate
delete row from a table in hibernate  is there method to delete row in a table using hibernate like save(-) to insert row
Advertisements
delete row
delete row  how to delete row using checkbox and button in php... if(isset($_POST['delete'])) { for($i=0;$i<$row;$i++) { $delete...("sourabh", $link); $rows=mysql_query("select * from sonu"); $row=mysql
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
sqlite database delete row
sqlite database delete row  How to delete row from SQLite Database?    NSString *updateSQL = [NSString stringWithFormat: @"DELETE FROM aListDB WHERE id='%@'",details.ids
delete row using id
delete row using id  package pkg2; import org.hibernate.Query; import... = "delete from Insurance insurance where id = 2"; Query query = sess.createQuery(hql); int row = query.executeUpdate(); if (row == 0
delete multiple row using checkbox
delete multiple row using checkbox  how to delete multiple row in a table which is connected to database using checkbox
delete multiple row using checkbox
delete multiple row using checkbox  how to delete multiple row in a table which is connected to database using checkbox
uitableview manually delete row
uitableview manually delete row  uitableview manually delete row   NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; [array removeObjectAtIndex:indexPath.row]; if(![array count]) [indexes addIndex
insert and delete data in database
insert and delete data in database  insert and delete data in database from servlets through JDBC   Hi Friend, Please visit the following links:ADS_TO_REPLACE_1 Insert Data Delete Data ThanksADS_TO_REPLACE_2
Delete a row from database by id
Delete a row from database by id  I m creating a small application...) for "DELETE" AND "UPDATE". On clicking delete which is hyper link that particular row.... So anyone will tell me how to give hyper link to Delete and Update and delete
how to delete a row in sql without using delete command.
how to delete a row in sql without using delete command.  how to delete a row in sql without using delete command. thanks in advance
delete multiple row using checkbox
delete multiple row using checkbox  delete multiple row using checkbox   We are providing you the code where we have specified only three...; st=conn.createStatement(); for(int a=0;a<10;a++){ st.executeUpdate("delete
data grid with edit and delete options at each row.
data grid with edit and delete options at each row.  i want to display the table data in the format of data grid with edit and delete options at each row. i need it very urgently. advance thanks
Hibernate delete a row error - Hibernate
Hibernate delete a row error  Hello, I been try with the hibernate delete example (http://www.roseindia.net/hibernate/hibernate-delete.shtml...(); //======================================= sess = fact.openSession(); String hql = "delete from Contact contact
Using insert update and delete in the same servlet
Using insert update and delete in the same servlet  How to write insert, update and delete coding in the same servlet
delete row from a table using hibernate
delete row from a table using hibernate  //code in java file String hql="delete from CONTACT c where ID=6"; Query query=session.createQuery... [delete from CONTACT] int i=query.executeUpdate
Insert specific fields into table dynamically for each row.
Insert specific fields into table dynamically for each row.  ... insert there for each row and for each row there is a button "done".if he click... will enter those details in a webpage. So that the field values will insert
Insert specific fields into table dynamically for each row.
Insert specific fields into table dynamically for each row.  ... insert there for each row and for each row there is a button "done".if he click... will enter those details in a webpage. So that the field values will insert
How to delete the row from the Database by using servlet
How to delete the row from the Database by using servlet  Dear Sir/Madam I am trying to delete the one user data in the Oracle SQL server database...: Delete row from database using servlet   In that link solution
insert , edit , and delete button in one jsp page
insert , edit , and delete button in one jsp page  hello I want to ask about the way of creating a jsp page contains insert , edit , and delete buttons and manipulate data in database directly. any help please or hints
Update delete perticular row from brower on link - Struts
Update delete perticular row from brower on link   how can update and delete perticular row from List of employee in brower format are ramesh... delete when i click on update than that perticular row are display on another
JDBC Delete Row In Table Example
JDBC Delete Row In Table Example : In this tutorial we will learn how delete... or more specific  row delete from table that follow any given condition...; delete row then count deleted row and display output "Deleted specific
Java Program to insert a row in the same sheet of excel file
Java Program to insert a row in the same sheet of excel file  Java program to insert a row in the same sheet of excel file using poi package in java
JDBC ResultSet Delete Row Example
JDBC ResultSet Delete Row Example: Learn how to delete row using ResultSet. We are also used ResultSet object with update capability for delete rows from... for delete the current row form the table of the ResultSet object. Example
Query to insert values in the empty fields in the last row of a table in Mysql database?
Query to insert values in the empty fields in the last row of a table in Mysql... row of my MYSQL database. Now I want to fill up those empty fields in the last row. So what will be the query
using insert and delete in a single page in jsp
using insert and delete in a single page in jsp  I am using the following code in jsp to declare two javascript functions to insert and delete in a single jsp page but insert function doesn't works only delete function works even
Insert and Delete an element in between an array
Insert and Delete an element in between an array In this section, you will learn how to insert and delete an element in between an array. For this purpose, we... a new array. Following code insert an element in an array: public int
insert data 50 row *column into 300 row *column by using xls sheet data (50 row *column) are given in xls sheet
insert data 50 row *column into 300 row *column by using xls sheet data (50 row *column) are given in xls sheet   1- there are matrix of data with 300 row and 300 column,this is master table we have input data 50 row and 50
Delete and add row from Table View iPhone
Delete and add row from Table View iPhone In this tutorial will learn how to delete and also how to add row into the table view iPhone, with the help of edit... and Delete buttons on Table view. Table views are commonly found in iPhone applications
include a delete option in every row of table in a JSP page
include a delete option in every row of table in a JSP page  I have the following code of a JSP page........... <blockquote> <p>... on Delete it should delete the particular record from the database and reflect
How to delete the row from the Database by using while loop in servlet
How to delete the row from the Database by using while loop in servlet  Dear Sir/Madam, I am trying to delete the one user data in the Oracle SQL... are USERS (Char), PASS (Varchar2) and CODE (Varchar2). I am trying to delete 4th user
Delete row and column from table through java code
Delete row and column from table through java code... will see how to delete row and column from given table through java code. Java code... | +----+----------+------------+---------+ In this table we will delete the row having minimum value of ID then delete
Deleting row and column from a table
Deleting row and column from a table  In this program ,we delete row... connection interface and java driver. After it we can delete row using "delete... the row having minimum ID.  statement.executeUpdate("delete from cellular where id
How to insert or delete records in MS access database using jsp - JSP-Servlet
How to insert or delete records in MS access database using jsp  Hi friends please provide me a solution that i insert or delete record from a database using java server pages. I used the microsoft access 2003 database. PlZ
JDBC: Delete Record using Prepared Statement
how to delete row of a table using Prepared Statements. Delete Record ... Clause. So you can delete one row or multiple row of table as specified... : Delete Record using PreparedStatement... Row deleted successfully
UITableviewcell programmatically
UITableviewcell programmatically  Hi, How to create uitableviewcell programmatically? Thanks
UITableViewCell programmatically
UITableViewCell programmatically  Hi, How to create UITableViewCell programmatically? Thanks
UILabel programmatically
UILabel programmatically  Hi, How to create UILabel programmatically? Thanks
how to insert multiple columns of a single row into my sql database using jsp
how to insert multiple columns of a single row into my sql database using jsp  hi sir, how to insert multiple columns of a single row into my sql database using jsp. when i click ADD ROW,rows are added.when i click submit
Delete a Specific Row from a Database Table
Delete a Specific Row from a Database Table   ... in a row, now we need to delete that wrong data. This can be done very easily, and in this section we are going to do the same that is, how to delete a specific row
JDBC: Insert Record using Prepared Statements
JDBC: Insert Record using Prepared Statements In this section, you will learn how to insert row using Prepared Statements. Insert Record  ... SQL statements(insert, update or delete)in PreparedStatement object, which
delete
delete  how delete only one row in the database using jsp.database...;td><input type="button" name="edit" value="Delete" style="background-color..."); Statement st = conn.createStatement(); st.executeUpdate("DELETE FROM employee
UITextField Programmatically iPhone
UITextField Programmatically iPhone  How to create UITextField Programmatically in iPhone
DELETE
DELETE   I AM DOING IT IN MYSQL. DELETE FROM EMP WHERE SAL>(SELECT SAL FROM EMP WHERE ENAME='MILLAR') AND ENAME='ALLEN'; THIS IS GIVING THE FOLLOWING ERROR Error Code : 1093 You can't specify target table 'EMP
insert
insert  insert data in database from servlet through JDBC   Ho Friend, Please visit the following:ADS_TO_REPLACE_1 Insert data into database Thanks
Insert a row in 'Mysql' table using JSP Code
Insert a row in 'Mysql' table using JSP Code In this section, we will discuss about how to insert data in Mysql database using JSP code. Query... between your Tomcat server & Mysql database Table. Code to insert row in Mysql
Update a resultset programmatically
Update a resultset programmatically  How to update a resultset programmatically
SQL Bulk Insert
SQL Bulk Insert       SQL Bulk Insert is performed when you insert a records  or row to a table... an example from 'SQL Bulk Insert'. To understand and grasp example we create a table
custom uibutton programmatically
custom uibutton programmatically  Hi, How to create custom uibutton programmatically? Thanks   Hi, Please see the thread UIButton custom programmatically. Thanks

Ads