<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Explain JDBC executeQuery</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/explain-jdbc-executequery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 30 Jul 2010 06:00:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Explain executeQuery Method</title>
		<link>http://www.bestjavainterviewquestions.com/explain-executequery-method/</link>
		<comments>http://www.bestjavainterviewquestions.com/explain-executequery-method/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 04:39:24 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Explain JDBC executeQuery]]></category>
		<category><![CDATA[jdbc executeQuery]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=433</guid>
		<description><![CDATA[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 &#8216;Before first Row&#8217; To make the ResultSet point to the first row we can call [...]]]></description>
			<content:encoded><![CDATA[<p><strong>ExecuteUpdate() Mehod :- </strong></p>
<p>executeQuery() method returns a Resultset object(ResultSet is an Interface)</p>
<p>A java application can used to result set object to fetch the data from the database server</p>
<p><em><strong>Note: </strong></em>When a Result set is opened(created) it will point to <em>&#8216;<span style="text-decoration: underline;">Before first Row&#8217;</span></em></p>
<p><em></em>To make the ResultSet point to the first row we can call <em><span style="text-decoration: underline;">rs.next()</span> </em>immedetly after opening the <span style="text-decoration: underline;"><em>ResultSet</em></span></p>
<p>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)</p>
<p>we can provide the code as shown below to perform <em><span style="text-decoration: underline;">&#8216;Read Operations&#8217;</span></em></p>
<p>Statement stmt=con.createStatement();</p>
<p>String Vsql=&#8221;select * from student&#8221;;</p>
<p>ResultSet rs=stmt.executeQuery(Vsql);</p>
<p>while(rs.next())</p>
<p>{</p>
<p>System.out.println(&#8220;current Rows&#8211;&gt;&#8221;+rs.getRow());</p>
<p>System.out.println(&#8220;rs.getString(&#8220;sid&#8221;));</p>
<p>System.out.println(&#8220;rs.getString(&#8220;sname&#8221;));</p>
<p>System.out.println(&#8220;rs.getString(&#8220;age&#8221;));</p>
<p>System.out.println(&#8220;rs.getString(&#8220;address&#8221;));</p>
<p>}</p>
<p>Note:  By default statement executeQuery creates forward only Readonly resultset</p>
<p>1) &#8211;&gt;An application can use resultset.next() mehod to move in the forward direction. If the resultset is forward only ResultSet.But we can&#8217;t use the method last,first,previouson, forward only ResultSet.</p>
<p>Creating the ResultSet &lt;=&gt; Opening the ResultSet<br />
&#8211;&gt; When rs.refereshRow() is used there is an oracle bug in select *  from instead of &#8216;*&#8217; we use the row names directly then it work.</p>
<p>2) There are two types of SCROLABLE RESULTSETS</p>
<p>&#8211;&gt; SCROLL-SENSITIVE<br />
&#8211;&gt; SCROLL-INSENSITIVE<br />
&#8211;&gt; In case of both this ResultSet we can use the methods of First.Last(),next(),previous,absolute().</p>
<p>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.<br />
In case of SCROLL-INSENSETIVE ResultSet we can&#8217;t use RefereshRow() method (i.e we can&#8217;t get the data that is modified by another application after opening the ResultSet.</p>
<p>(TYPE-SCROLL-SENSITIVE)&#8211;&gt; is called as Constant.</p>
<p>Prog)<br />
statement stmt=con.createStatement(ResultSet.TYPE_SCROLL-SENSITIVE,ResultSet.CONCUR_READ_ONLY);<br />
String Vsql=&#8221;select sid,sname,age,address from student&#8221;;<br />
ResultSet rs=stmt.executeQuery(Vsql);<br />
rs.absolute(2);<br />
System.outprintln(&#8220;rs.getString(&#8220;sname&#8221;));<br />
System.in.read();System.in.read();<br />
rs.refreshRow();<br />
System.our.println(rs.getString(&#8220;sname&#8221;)):</p>
<p>&#8212;&gt; System.in.read() the application will wait until the user press the enter button.</p>
<p>3) On a readonly ResultSet an application can&#8217;t perform the operation like deleteRow,UpdateRow,InserRow.Reading the data is done by only getString() method.</p>
<p>&#8212;&gt; We can modify the data in a table by using concurrent updatable ResultSet.</p>
<p>4) we can use the code shown below to insert a row in a table using CONCURRENT UPDATABLE RESULTSET</p>
<p>Prog)</p>
<p>Statement stmt=con.createStatement<br />
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);<br />
String Vsql=&#8221;select std,sname,age,address from student&#8221;;<br />
ResultSet rs=stmt.executeQuery(Vsql);<br />
rs.moveToInsertRow();</p>
<p>//It will not isert the data but it will create the memory in the system and place it in the JDBC by JVM</p>
<p>rs.updateString(&#8220;sid&#8221;,400);<br />
rs.updateString(&#8220;Sname&#8221;,&#8221;XYZ&#8221;);<br />
rs.updateString(&#8220;age&#8221;,21);<br />
rs.updateString(&#8220;address&#8221;,&#8221;ABC&#8221;);<br />
rs.insertRow();</p>
<p>//It will create the Insert data and insert in DB(effect)create</p>
<p>&#8211;&gt;This all tools which we are using are make the burden on the JDBC (or) System so the senior prog will not use this<br />
We can make the change to the data using CONCURRENT_UPDATABLE ResultSet by providing the code as shown below.</p>
<p>rs.last();<br />
rs.updateString(&#8220;sname&#8221;,&#8221;newstud&#8221;);<br />
rs.updateString(&#8220;addresss&#8221;,&#8221;NewAddr&#8221;);<br />
rs.updateRow()</p>
<p>&#8211;&gt; We can perform the deletion operation on CONCURRENT_UPDATABLE ResultSet using rs.deleteRow()</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/difference-between-executeupdate-and-executequery-methods/" title="Difference between executeUpdate and executeQuery Methods"><img src="Array" alt="Difference between executeUpdate and executeQuery Methods" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-static-keyword/" title="Define Static Keyword"><img src="Array" alt="Define Static Keyword" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/date-format/" title="Date Format"><img src="Array" alt="Date Format" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/servlets-and-jsps/" title="Servlets and Jsp&#8217;s"><img src="Array" alt="Servlets and Jsp&#8217;s" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/overview-of-servlets/" title="OVERVIEW OF SERVLETS"><img src="Array" alt="OVERVIEW OF SERVLETS" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/explain-executequery-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
