Explain executeQuery Method
By Ramakrishna on Oct 5, 2008 in Explain JDBC executeQuery
ExecuteUpdate() Mehod :-
executeQuery() method returns a Resultset object(ResultSet is an Interface)
A java application can used to result set object to fetch the data from the database server
Note: When a Result set is opened(created) it will point to ‘Before first Row’
To make the ResultSet point to the first row we can call rs.next() immedetly after opening the ResultSet
rs.next method returns true if it is able to point to the next available row.This method return false when there is no rows (when it reaches after last row)
we can provide the code as shown below to perform ‘Read Operations’
Statement stmt=con.createStatement();
String Vsql=”select * from student”;
ResultSet rs=stmt.executeQuery(Vsql);
while(rs.next())
{
System.out.println(“current Rows–>”+rs.getRow());
System.out.println(“rs.getString(“sid”));
System.out.println(“rs.getString(“sname”));
System.out.println(“rs.getString(“age”));
System.out.println(“rs.getString(“address”));
}
Note: By default statement executeQuery creates forward only Readonly resultset
1) –>An application can use resultset.next() mehod to move in the forward direction. If the resultset is forward only ResultSet.But we can’t use the method last,first,previouson, forward only ResultSet.
Creating the ResultSet <=> Opening the ResultSet
–> When rs.refereshRow() is used there is an oracle bug in select * from instead of ‘*’ we use the row names directly then it work.
2) There are two types of SCROLABLE RESULTSETS
–> SCROLL-SENSITIVE
–> SCROLL-INSENSITIVE
–> In case of both this ResultSet we can use the methods of First.Last(),next(),previous,absolute().
A SCROLL-SENSETIVE RESULTSET will be able to pickup the modified(updated) data from the DB in order to get fresh data REFERESHROW() method must be used.
In case of SCROLL-INSENSETIVE ResultSet we can’t use RefereshRow() method (i.e we can’t get the data that is modified by another application after opening the ResultSet.
(TYPE-SCROLL-SENSITIVE)–> is called as Constant.
Prog)
statement stmt=con.createStatement(ResultSet.TYPE_SCROLL-SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String Vsql=”select sid,sname,age,address from student”;
ResultSet rs=stmt.executeQuery(Vsql);
rs.absolute(2);
System.outprintln(“rs.getString(“sname”));
System.in.read();System.in.read();
rs.refreshRow();
System.our.println(rs.getString(“sname”)):
—> System.in.read() the application will wait until the user press the enter button.
3) On a readonly ResultSet an application can’t perform the operation like deleteRow,UpdateRow,InserRow.Reading the data is done by only getString() method.
—> We can modify the data in a table by using concurrent updatable ResultSet.
4) we can use the code shown below to insert a row in a table using CONCURRENT UPDATABLE RESULTSET
Prog)
Statement stmt=con.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String Vsql=”select std,sname,age,address from student”;
ResultSet rs=stmt.executeQuery(Vsql);
rs.moveToInsertRow();
//It will not isert the data but it will create the memory in the system and place it in the JDBC by JVM
rs.updateString(“sid”,400);
rs.updateString(“Sname”,”XYZ”);
rs.updateString(“age”,21);
rs.updateString(“address”,”ABC”);
rs.insertRow();
//It will create the Insert data and insert in DB(effect)create
–>This all tools which we are using are make the burden on the JDBC (or) System so the senior prog will not use this
We can make the change to the data using CONCURRENT_UPDATABLE ResultSet by providing the code as shown below.
rs.last();
rs.updateString(“sname”,”newstud”);
rs.updateString(“addresss”,”NewAddr”);
rs.updateRow()
–> We can perform the deletion operation on CONCURRENT_UPDATABLE ResultSet using rs.deleteRow()
