<?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; jdbc questions</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/jdbc-questions/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>Stored Procedures for Beginners</title>
		<link>http://www.bestjavainterviewquestions.com/stored-procedures-for-beginners/</link>
		<comments>http://www.bestjavainterviewquestions.com/stored-procedures-for-beginners/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 06:00:25 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Jdbc questions]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[jdbc questions]]></category>
		<category><![CDATA[stored procedures]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=623</guid>
		<description><![CDATA[you are reading my article because you want to learn how to write stored procedures. You are new to this, and you don&#8217;t know where to start. You are exactly where I was when I wanted to learn how to write stored procedures. The purpose of this article is to help the developer who doesn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>you are reading my article because you want to learn how to write stored procedures. You are new to this, and you don&#8217;t know where to start. You are exactly where I was when I wanted to learn how to write stored procedures. The purpose of this article is to help the developer who doesn&#8217;t know where to start. I will give you a place to start, teach you techniques beyond the basic &#8220;hello world&#8221;, and walk you through examples. I learned how to write stored procedures alone, and have decided to let you use my experiences to your advantage. This article will consist of the following topics:</p>
<p>1. Input Variables</p>
<p>2. Input Variables with SELECT and UPDATE Statements</p>
<p>3. Exercise:  Pass Data to a SELECT Stored Procedure</p>
<p>4. Conclusion</p>
<p>5. Database script for SPFORBEGINNERS (the database referenced in this article)</p>
<p>At the end, I have also included a SQL script for creating the database used in my examples. It is a small database, and easily replicated.</p>
<p>What Are Stored Procedures?</p>
<p>Have you ever written SQL statements, like inserts, selects, and updates? Then you have already written most of a stored procedure. A stored procedure is an already written SQL statement that is saved in the database. If you find yourself using the same query over and over again, it would make sense to put it into a stored procedure. When you put this SQL statement in a stored procedure, you can then run the stored procedure from the database&#8217;s command environment.<br />
An example is:</p>
<p>exec usp_displayallusers</p>
<p>The name of the stored procedure is &#8220;usp_displayallusers&#8221;, and &#8220;exec&#8221; tells SQL Server to execute the code in the stored procedure. (Note: &#8220;usp_&#8221; in front of the stored procedure name is used to designate this stored procedure as a user-created stored procedure.) The code inside the stored procedure can be something as simple as:</p>
<p>SELECT * FROM USERLIST</p>
<p>This &#8220;select&#8221; statement will return all data in the USERLIST table. You may think, skeptically, that stored procedures aren’t terribly useful. Just save the query and run it when you need to. Too easy, right?</p>
<p>Well, there is more to the story. Many queries get more complex than &#8220;select * from . . .&#8221;  Also, you may want to call the stored procedure from an application, such as an ASP page, Visual Basic application, or a Java servlet. With a stored procedure, you can store all the logic in the database, and use a simple command to call the stored procedure. Later, if you decide to use J2EE, you only need to change the application layer, which will be significantly easier. Much of the business logic will remain in the database.</p>
<p>Enough background—let’s write some stored procedures.</p>
<p>Getting Started with Stored Procedures</p>
<p>What do I need to get started? I have heard that question often. To begin writing stored procedures, the following are essential:</p>
<p>1. A database management system.</p>
<p>2. A database built inside the database management system (see the end of this article for a sample).</p>
<p>3. A text editor, such as Notepad or Query Analyzer.</p>
<p>Items 1 and 2 are absolutely essential. You can’t write stored procedures without a database. They would be useless. Sometimes, I write my procedures in Notepad (or another text editor), and copy them into the New Stored Procedure window in My SQL Server.</p>
<p>Next, you will have to decide what you want your stored procedure to do. It can be tempting to just dive right into the task at hand, but it is always prudent to sketch out some ideas first. Some considerations should be:</p>
<p>* Do you want to view data in the database (SELECT), insert new records (INSERT INTO), or do I want to change an existing record (UPDATE)?</p>
<p>* With which tables will you have to work? Does it make sense to create a VIEW first?</p>
<p>* How often will this procedure actually be used?</p>
<p>Once you have struggled with these questions (something of an exaggeration, I guess), you will be ready to start coding!</p>
<p>Note: Throughout this article, I will focus on stored procedures for My SQL Server. You can apply the same principles to other database management systems, but I will make clear references to working in a My SQL Server environment.</p>
<p>You May Consider this things also</p>
<p>Before creating a stored procedure, consider that:</p>
<p>* CREATE PROCEDURE statements cannot be combined with other SQL statements in a single batch.</p>
<p>* To create procedures, you must have CREATE PROCEDURE permission in the database and ALTER permission on the schema in which the procedure is being created. For CLR stored procedures, you must either own the assembly referenced in &lt;method_specifier&gt;, or have REFERENCES permission on that assembly.</p>
<p>* Stored procedures are schema-scoped objects, and their names must follow the rules for identifiers.</p>
<p>* You can create a stored procedure only in the current database.</p>
<p>When creating a stored procedure, you should specify:</p>
<p>* Any input parameters and output parameters to the calling procedure or batch.</p>
<p>* The programming statements that perform operations in the database, including calling other procedures.</p>
<p>* The status value returned to the calling procedure or batch to indicate success or failure (and the reason for failure).</p>
<p>* Any error handling statements needed to catch and handle potential errors.</p>
<p>Error handing functions such as ERROR_LINE and ERROR_PROCEDURE can be specified in the stored procedure.</p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/define-stored-procedures/" title="Define Stored Procedures"><img src="Array" alt="Define Stored Procedures" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/stored-procedures-for-beginners/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jdbc Interview Questions</title>
		<link>http://www.bestjavainterviewquestions.com/jdbc-interview-questions-2/</link>
		<comments>http://www.bestjavainterviewquestions.com/jdbc-interview-questions-2/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 19:26:39 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[jdbc questions]]></category>
		<category><![CDATA[jdbc interview questions]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=28</guid>
		<description><![CDATA[Q)  What are the steps involved in establishing a JDBC connection? A) This action involves two steps: loading the JDBC driver and making the connection. Q)  How can you load the drivers? A) Loading the driver or drivers you want to use is very simple and involves just one line of code. If, for example, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Q)  What are the steps involved in establishing a JDBC connection?<br />
A)</strong> This action involves two steps: loading the JDBC driver and making the connection.</p>
<p><strong>Q)  How can you load the drivers?<br />
A) </strong>Loading the driver or drivers you want to use is very simple and involves just one line of code. If, for example, you want to use the JDBC-ODBC Bridge driver, the following code will load it:</p>
<p>Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);</p>
<p>Your driver documentation will give you the class name to use. For instance, if the class name is jdbc.DriverXYZ, you would load the driver with the following line of code:</p>
<p>Class.forName(”jdbc.DriverXYZ”);</p>
<p><strong>Q)  What will Class.forName do while loading drivers?<br />
A)</strong> It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.</p>
<p><strong>Q)  How can you make the connection?<br />
A) </strong> To establish a connection you need to have the appropriate driver connect to the DBMS. The following line of code illustrates the general idea:</p>
<p>String url = “jdbc:odbc:Fred”;<br />
Connection con = DriverManager.getConnection(url, “Fernanda”, “J8?);</p>
<p><strong>Q)  How can you create JDBC statements and what are they?<br />
A)</strong> A Statement object is what sends your SQL statement to the DBMS. You simply create a Statement object and then execute it, supplying the appropriate execute method with the SQL statement you want to send. For a SELECT statement, the method to use is executeQuery. For statements that create or modify tables, themethod to use is executeUpdate. It takes an instance of an active connection to create a Statement object. In the following example, we use our Connection object con to create the Statement object</p>
<p>Statement stmt = con.createStatement();<br />
<strong><br />
Q)  How can you retrieve data from the ResultSet?<br />
A) </strong> JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object rs.<br />
ResultSet rs = stmt.executeQuery(”SELECT COF_NAME, PRICE FROM</p>
<p>COFFEES”);<br />
String s = rs.getString(”COF_NAME”);<br />
The method getString is invoked on the ResultSet object rs, so getString() will retrieve (get) the value stored in the column COF_NAME in the current row of rs.<br />
<strong><br />
Q)  What are the different types of Statements?<br />
A) </strong> Regular statement (use createStatement method), prepared statement (use prepareStatement method) and callable statement (use prepareCall)</p>
<p><strong>Q) How can you use PreparedStatement?<br />
A) </strong>This special type of statement is derived from class Statement.If you need a  Statement object to execute many times, it will normally make sense to use a PreparedStatement object instead. The advantage to this is that in most cases, this SQL statement will be sent to the DBMS right away, where it will be compiled. As a result, the PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement’s SQL statement without having to compile it first.<br />
PreparedStatement updateSales =<br />
con.prepareStatement(&#8220;UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?&#8221;);<br />
<strong><br />
Q)  What does setAutoCommit do?<br />
A) </strong> When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed. The way to allow two or more statements to be grouped into a transaction is to disable auto-commit mode:<br />
con.setAutoCommit(false);<br />
Once auto-commit mode is disabled, no SQL statements will be committed until you call the method commit explicitly.<br />
con.setAutoCommit(false);<br />
PreparedStatement updateSales =<br />
con.prepareStatement( &#8220;UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?&#8221;);<br />
updateSales.setInt(1, 50); updateSales.setString(2, &#8220;Colombian&#8221;);<br />
updateSales.executeUpdate();<br />
PreparedStatement updateTotal =<br />
con.prepareStatement(&#8220;UPDATE COFFEES SET TOTAL = TOTAL + ? WHERE COF_NAME LIKE ?&#8221;);<br />
updateTotal.setInt(1, 50);<br />
updateTotal.setString(2, &#8220;Colombian&#8221;);<br />
updateTotal.executeUpdate();<br />
con.commit();<br />
con.setAutoCommit(true);</p>
<p><strong>Q) How do you call a stored procedure from JDBC?<br />
A)</strong> The first step is to create a CallableStatement object. As with Statement an and PreparedStatement objects, this is done with an open Connection object. A CallableStatement object contains a call to a stored procedure.</p>
<p>CallableStatement cs = con.prepareCall(&#8220;{call SHOW_SUPPLIERS}&#8221;);<br />
ResultSet rs = cs.executeQuery();<br />
<strong><br />
Q)  How do I retrieve warnings?<br />
A) </strong>SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these  classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object:<br />
SQLWarning warning = stmt.getWarnings();<br />
if (warning != null)<br />
{<br />
System.out.println(&#8220;n&#8212;Warning&#8212;n&#8221;);<br />
while (warning != null)<br />
{<br />
System.out.println(&#8220;Message: &#8221; + warning.getMessage());<br />
System.out.println(&#8220;SQLState: &#8221; + warning.getSQLState());<br />
System.out.print(&#8220;Vendor error code: &#8220;);<br />
System.out.println(warning.getErrorCode());<br />
System.out.println(&#8220;&#8221;);<br />
warning = warning.getNextWarning();<br />
}<br />
}</p>
<p><strong>Q)  How can you move the cursor in scrollable result sets?<br />
A)</strong> One of the new features in the JDBC 2.0 API is the ability to move a result set’s cursor backward as well as forward. There are also methods that let you move the cursor to a particular row and check the position of the cursor.<br />
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);<br />
ResultSet srs = stmt.executeQuery(”SELECT COF_NAME, PRICE FROM COFFEES”);<br />
The first argument is one of three constants added to the ResultSet API to indicate the type of a ResultSet object: TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE , and TYPE_SCROLL_SENSITIVE. The second argument is one of two ResultSet constants for specifying whether a result set is read-only or updatable: CONCUR_READ_ONLY and CONCUR_UPDATABLE. The point to remember here is that if you specify a type, you must also specify whether it is read-only or updatable. Also, you must specify the type first, and because both parameters are of type int , the compiler will not complain if you switch the order.</p>
<p>Specifying the constant TYPE_FORWARD_ONLY creates a nonscrollable result set, that is, one in which the cursor moves only forward. If you do not specify any constants for the type and updatability of a ResultSet object, you will automatically get one that is TYPE_FORWARD_ONLY and CONCUR_READ_ONLY.</p>
<p><strong>Q)  What’s the difference between TYPE_SCROLL_INSENSITIVE , and TYPE_SCROLL_SENSITIVE?<br />
A)</strong> You will get a scrollable ResultSet object if you specify one of these ResultSet constants.The difference between the two has to do with whether a result set reflects changes that are made to it while it is open and whether certain methods can be called to detect these changes. Generally speaking, a result set that is TYPE_SCROLL_INSENSITIVE does not reflect changes made while it is still open and one that is TYPE_SCROLL_SENSITIVE does. All three types of result sets will make changes visible if they are closed and then reopened:<br />
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,<br />
ResultSet.CONCUR_READ_ONLY);<br />
ResultSet srs = stmt.executeQuery(&#8220;SELECT COF_NAME, PRICE FROM COFFEES&#8221;);<br />
srs.afterLast();<br />
while (srs.previous())<br />
{<br />
String name = srs.getString(&#8220;COF_NAME&#8221;);<br />
float price = srs.getFloat(&#8220;PRICE&#8221;);<br />
System.out.println(name + &#8221; &#8221; + price);<br />
}<br />
<strong><br />
Q)  How to Make Updates to Updatable Result Sets?<br />
A) </strong>Another new feature in the JDBC 2.0 API is the ability to update rows in a result set using methods in the Java programming language rather than having to send an SQL command. But before you can take advantage of this capability, you need to create a ResultSet object that is updatable. In order to do this, you supply the<br />
ResultSet constant CONCUR_UPDATABLE to the createStatement method.<br />
Connection con =DriverManager.getConnection(&#8220;jdbc:mySubprotocol:mySubName&#8221;);<br />
Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);<br />
ResultSet uprs =stmt.executeQuery(&#8220;SELECT COF_NAME, PRICE FROM COFFEES&#8221;);</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/explain-java-main-method/" title="Explain Java Main Method."><img src="Array" alt="Explain Java Main Method." /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-jsp-include-and-jsp-forward/" title="Define Jsp-include and Jsp-forward"><img src="Array" alt="Define Jsp-include and Jsp-forward" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-transaction-isolation-in-ejb/" title="Define Transaction Isolation in EJB"><img src="Array" alt="Define Transaction Isolation in EJB" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/example-programs-of-lifecycle-of-servlets/" title="Example programs of lifecycle of servlets"><img src="Array" alt="Example programs of lifecycle of servlets" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/gui-examples-using-applets/" title="GUI Examples Using Applets"><img src="Array" alt="GUI Examples Using Applets" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/jdbc-interview-questions-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jdbc Interview Questions</title>
		<link>http://www.bestjavainterviewquestions.com/jdbc-interview-questions/</link>
		<comments>http://www.bestjavainterviewquestions.com/jdbc-interview-questions/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 19:15:14 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[jdbc questions]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=27</guid>
		<description><![CDATA[Q) Is java.sql.Driver a class or an Interface ? A) It&#8217;s an interface. Q) Is java.sql.DriverManager a class or an Interface ? A) It&#8217;s a class. This class provides the static getConnection method, through which the database connection is obtained. Q)  Is java.sql.Connection a class or an Interface ? A) java.sql.Connection is an interface. The [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Q) Is java.sql.Driver a class or an Interface ?</strong><br />
<strong>A) </strong>It&#8217;s an interface.</p>
<p><strong>Q) Is java.sql.DriverManager a class or an Interface ?</strong><br />
<strong>A) </strong> It&#8217;s a class. This class provides the static getConnection method, through which the database connection is obtained.</p>
<p><strong>Q)  Is java.sql.Connection a class or an Interface ?</strong><br />
<strong>A)</strong> java.sql.Connection is an interface. The implmentation is provided by the vendor specific Driver.</p>
<p><strong>Q)  Is java.sql.Statement a class or an Interface ?</strong><br />
<strong>A)</strong> java.sql.Statement,java.sql.PreparedStatement and java.sql.CallableStatement are interfaces.</p>
<p><strong>Q)  Which interface do PreparedStatement extend?</strong><br />
<strong>A)</strong> java.sql.Statement</p>
<p><strong>9) Which interface do CallableStatement extend?<br />
A)</strong> CallableStatement extends PreparedStatement.</p>
<p><strong>Q) What is the purpose Class.forName(&#8220;&#8221;) method?<br />
A) </strong> The Class.forName(&#8220;&#8221;) method is used to load the driver.</p>
<p><strong>Q) Do you mean that Class.forName(&#8220;&#8221;) method can only be used to load a driver?<br />
A)</strong> The Class.forName(&#8220;&#8221;) method can be used to load any class, not just the database vendor driver class.</p>
<p><strong>Q)  Which statement throws ClassNotFoundException in SQL code block? and why?<br />
A) </strong> Class.forName(&#8220;&#8221;) method throws ClassNotFoundException. This exception is thrown when the JVM is not able to find the class in the classpath.<br />
<strong><br />
Q)  What exception does Class.forName() throw?<br />
A)</strong> ClassNotFoundException.</p>
<p><strong>Q)  What is the return type of Class.forName() method ?<br />
A)</strong> java.lang.Class</p>
<p><strong>Q)  Can an Interface be instantiated? If not, justify and explain the following line of code:<br />
A)</strong> Connection con = DriverManager.getConnection(&#8220;dsd&#8221;,&#8221;sds&#8221;,&#8221;adsd&#8221;);<br />
An interface cannot be instantiated. But reference can be made to a interface. When a reference is made to interface, the refered object should have implemented all the abstract methods of the interface. In the above mentioned line, DriverManager.getConnection method returns Connection Object with implementation for all abstract methods.</p>
<p><strong>Q)  What type of a method is getConnection()?<br />
A) </strong> static method.</p>
<p><strong>Q)  What is the return type for getConnection() method?<br />
A)</strong> Connection object.</p>
<p><strong>Q) What is the return type for executeQuery() method?<br />
</strong><strong>A) </strong>ResultSet</p>
<p><strong>Q)  What is the return type for executeUpdate() method and what does the return type indicate?<br />
A)</strong> int. It indicates the no of records affected by the query.</p>
<p><strong>Q) What is the return type for execute() method and what does the return type indicate?<br />
A) </strong> boolean. It indicates whether the query executed sucessfully or not.</p>
<p><strong>Q)  is Resultset a Classor an interface?<br />
A)</strong> Resultset is an interface.</p>
<p><strong>Q)  What is the advantage of PrepareStatement over Statement?<br />
A) </strong> PreparedStatements are precompiled and so performance is better. PreparedStatement objects can be reused with passing different values to the queries.</p>
<p><strong>Q)  What is the use of CallableStatement?<br />
A) </strong> CallableStatement is used to execute Stored Procedures.</p>
<p><strong>Q)  Name the method, which is used to prepare CallableStatement?<br />
A)</strong> CallableStament.prepareCall().</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li><a href="http://www.bestjavainterviewquestions.com/basic-java-interview-question/" title="Basic Java Interview Question"><img src="Array" alt="Basic Java Interview Question" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/getting-url-contents/" title="Getting URL contents"><img src="Array" alt="Getting URL contents" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/define-session-migration/" title="Define Session Migration"><img src="Array" alt="Define Session Migration" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/jdbc-questions/" title="JDBC Questions"><img src="Array" alt="JDBC Questions" /></a></li><li><a href="http://www.bestjavainterviewquestions.com/applets-tutorial-with-some-important-methods/" title="How to use Applets"><img src="Array" alt="How to use Applets" /></a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/jdbc-interview-questions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
