Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JDBC insert and delete a row programmatically
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Clark
insert and delete a row programmatically
2 Answer(s)      4 years and a month ago
Posted in : JDBC


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


Advertisement
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
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
Advertisements
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
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
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
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
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
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
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: Insert Data Delete Data Thanks
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
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
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
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
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
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
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
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
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.... 6. Finally call deleteRow() ResultSet method for delete the current row form
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
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
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
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
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
Advertisements
 

 

 

DMCA.com