Ingeneral execute() method can be used to execute selection group SQl queries for getting the data from Database , but execute() return a boolean value true so here how it possible to fetch the data from database?
- Execute() can be used to execute both selection group sql query and updation group sql query.
- If we use execute() to execute a selection group sql query then DBE(Database engine) will execute that sql query and send back the fetched data from database to java application. Now java application will prepare a ResultSet object with the fetched data but execute() will return “true” as a Boolean value.
- At this situation to get the reference of the ResultSet object explicitily, we will use the following method from Statement object.
public ResultSet getResultSet();
//eg
boolean b = st.execute(“select * from emp1”);
System.out.println(b);
ResultSet rs = st.getResultSet();
If we use selection group SQL query to executeUpdate() ,what happened?
- If we use selection group sql query as a parameter to executeUpdate(…) then JVM will send that sql query to the DBE, DBE will fetch the data and send back to the java application here java application will store the fetched data in the form of ResultSet object. But executeUpdate() is expecting records updated count value.
Due to this contradiction JVM will rise an exception like java.lang.SQLException.
If we handle the above exception properly then we will get ResultSet abject and we will get the data from Database.
What is ResultSet and What are the types of ResultSets are available in JDBC application?
In jdbc applications ResultSets could be classified in the following two ways.
- On the basis of ResultSet privilizations -
There are 2 types of ResultSets.
- Read only ResultSet
- Updatable ResultSet
Read only ResultSet:- It is a ResultSet, which will allow the users to read the data only. To refer this ResultSet, we will use the following constant from ResultSet interface.
public static final int CONCUR_READ_ONLY;
Updatable ResultSet:- If is a ResultSet object, which will allow users to perform some updations on its content. To refer this ResultSet we will use the following constant from ResultSet interface.
public static final int CONCUR_UPDATABLE;
2)On the basis of the ResultSet cursor movement:-
There are 2 types of ResultSets.
- Forward only ResultSet
- Scrollable ResultSet
Forward only ResultSet:- It is a ResultSet object, which will allow the users to iterate the data in any forward direction. To refer this ResultSet object we will use the following constant from ResultSet interface.
public static final int TYPE_FORWARD_ONLY;
Scrollable ResultSet:- These are the ResultSet objects, which will allow the users to iterate the data in both forward and backward directions.
There are 2 types of Scrollable ResultSets.
- Scroll sensitive ResultSets
- Scroll in sensitive ResultSets.
What is the difference between ScrollSensitive ResultSet and ScrollInsensitive ResultSets?
Scroll sensitive ResultSet is a ResultSet object, which will allow the later updations from database automatically after creating it. To refer this ResultSet we will use the following constant.
public static final int TYPE_SCROLL_SENSITIVE;
Scroll insensitive ResultSet is a ResultSet object, which will not allow later updations from database after creating it. To refer this ResultSet we will use the following constant from ResultSet interface.
public static final int TYPE_SCROLL_INSENSITIVE;
What is the default ResultSet type in JDBC application and How it is possible to create a specific type of ResultSet object?
- The default ResultSet type in the jdbc applications is Read only and forward only.
- In jdbc applications we are able to specify the following types of the ResultSet combination to any particular ResultSet.
- read-only, forward only
- read-only, scroll sensitive
- read-only, scroll insensitive
- updatable, forward only
- updatable, scroll sensitive
- updatable, scroll insensitive
- if we want to specity a particular type to the ResultSet object then we should use either of the above constants combination as a parameter to createStatement() method, for this we will use the following method.
public Statement createStatement(int forward / ScrollSensitive / ScrollInsensitive, int readonly / updatable)
Eg:
Statement st = con. createSensitive(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = con.executeQuery(….);
How to iterate the data from Scrollable ResultSet objuect in both forward and backword direction?
- to iterate the data in forward direction from a ResultSet object we will use the following 2 methods.
public Boolean next()
public xxx getXxx(int fieldno.)
Where xxx may be byte, short, char, int, long, float, double.
- To iterate the data in backward direction from Scrollable ResultSet object we will use the following 2 methods.
public Boolean previous()
public xxx getXxx(int fieldno)
Where previous() is a Boolean method, which can be used to check whether the previous record is available or not, if it is available then cursor will be moved to previous record position.