Adding, Updating and Deleting Rows using ResultSets.

text zoom

The next section will tell you how to enter the data without any insert query and with the help of ResultSet class.


package com.visualbuilder;


import java.sql.Connection;


import java.sql.DriverManager;


import java.sql.ResultSet;


import java.sql.Statement;


 


public class UpdateableAndScrollableRS {


    public static void main(String[] args) {


        try {


            /** Loading the ODBC- Bridge driver*/


                Class.forName("com.mysql.jdbc.Driver");


             /** Getting Connection*/


                                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");


 


            /** Creating Statement*/


                                Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);


 


            /** Getting ResultSet*/


                                ResultSet rs=stmt.executeQuery("select * from visualbuilder");


 


                while(rs.next()) {


                                        System.out.print("Id is " + rs.getInt("id"));


                                        System.out.println(" Name is "+ rs.getString("name"));


                                }


                                rs.moveToInsertRow();


                                rs.updateInt(1,3);


                                rs.updateString(2,"ResultSet Example");


                                rs.insertRow();


                                rs.beforeFirst();


                                System.out.println("After Adding The record");


                while(rs.next()) {


                                        System.out.print("Id is " + rs.getInt("id"));


                                        System.out.println(" Name is "+ rs.getString("name"));


                                }


 


                                rs.last();


                                rs.deleteRow();


                                rs.updateRow();


                                rs.beforeFirst();


                                System.out.println("After Deleting The record");


                while(rs.next()) {


                                        System.out.print("Id is " + rs.getInt("id"));


                                        System.out.println(" Name is "+ rs.getString("name"));


                                }


            /** Closing the Connection*/


                                stmt.close();


                                con.close();


                        }catch (Exception e) {


                                e.printStackTrace();


                        }


            }


}


 


Output:-


Id is 1 Name is Visual Builder


Id is 2 Name is VisualBuilder


After Adding The record


Id is 1 Name is Visual Builder


Id is 2 Name is VisualBuilder


Id is 3 Name is ResultSet Example


After Deleting The record


Id is 1 Name is Visual Builder


Id is 2 Name is VisualBuilder

                    

Copyright © 2008 VisualBuilder. All rights reserved