This is my 3rd post on JDBC TUTORIAL. Today we will be learning about how to access the results that we get for a query using ResultSet.
Whenever we execute a query to retrieve data from a table using a Java application, the output is stored in a ResultSet object in a tabular format. The ResultSet object maintains a cursor that helps us to move to and fro in the rows stored in the object and the cursor intially points before the first row.
Types of Result Sets
Now we know that the ResultSet object stores the output of a query in a tabular form and there are various types of ResultSet object :
- Read only : Allows us to only read the rows in a ResultSet object.
- Forward only : Allows us to move only in the forward direction i.e. the result set cursor can move only in the forward direction.
- Scrollable : Allows us to move the result set cursor backward and forward.
- Updatalbe : Allows us to update the result set rows retrieved from a database table.
We can specify the type of ResultSet object using the createStatement() method of the Connection interface. This was done in my previous tutorial on JDBC API TUTORIAL.
The table below lists the various fields of the ResultSet interface that we can use :
ResultSet | Description |
---|---|
TYPE_SCROLL_SENSITIVE
|
Specifies that the cursor of the ResultSet object is scrollable and it reflects the changes in the data made by other users
|
TYPE_SCROLL_INSENSITIVE
|
Specifies that the cursor of the ResultSet object is scrollable and it DOES NOT reflects the changes in the data made by other users
|
TYPE_FORWARD_ONLY
|
Specifies that the cursor of the ResultSet object moves in forward direction only from the first row to the last row
|
The createStatement() method is an overloaded method that has three prototypes.
- Statement createStatement() : Does not accept any parameter. This create sa default ResultSet object that only allows forward scrolling.
- Statement createStatement(int, int) : Accepts two parameters. The first parameter indicated the ResultSet type and the second parameter indicates the concurrency mode for the result set that determines whether the data in the result set can be updated.
- Statement createStatement(int , int, int) : In addition to the ResultSet type and concurrency mode, this method accepts third parameter. This parameter indicates whether or not, the result set is closed after committing data to the database.
public static void Rule1()
ReplyDelete{
Connection con = null;
Statement st = null;
ResultSet rs = null;
int updateQuery=0;
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "aiman";
/***************/
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance(); // Load JBBC driver "com.mysql.jdbc.Driver".
con = DriverManager.getConnection(url, "root", "aiman");/*Create a connection*/
st = con.createStatement();
/***count rows**/
int count=0;
ResultSet res = st.executeQuery("SELECT COUNT(*) FROM feature");
while (res.next())
{
count = res.getInt(1);
}
System.out.println("Number of rows:"+count);
/*****************************Inserting Values**************************/
String w1,w2;
String nsubj="nsubj";
String query[] ={"SELECT * FROM Dependency WHERE header like 'nsubj'"};
System.out.println("before printing");
for(String q : query)
{
res = st.executeQuery(q);
//System.out.println("Names for query "+ q +" are");
while (res.next())
{
String header = res.getString("header");
String wi = res.getString("wi");
String wj = res.getString("wj");
System.out.println(header+"|"+wi+"|"+wj);
ResultSet res1 = null;
Statement st1=null;
st1=con.createStatement();
String query1[] ={"SELECT * FROM tagtable WHERE word like 'wi' AND tag like '%NN%'"};
for(String q1 : query1)
{
res1 = st1.executeQuery(q1);
while (res1.next())
{
System.out.println("in the loop");
System.out.println("*******************");
String w = res1.getString("word");
String tag = res1.getString("tag");
System.out.println(w+"|"+tag);
}
}
res1.close();
}//end while
System.out.println("done");
}//end for
res.close();
st.close();
con.close();
/************************************************************************/
}//try ends
catch(Exception e)
{
System.out.println("Exception is = "+e);
}
}//rule 1 ends here
}//feature class ends
----------------------------------------------------------------------
THE INNER LOOP:
while (res1.next())
{ System.out.println("in the loop"); System.out.println("*******************");
String w = res1.getString("word");
String tag = res1.getString("tag");
System.out.println(w+"|"+tag);
}
DOES NOT GET EXECUTED ...!! HELP !