<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1816645927538051205</id><updated>2012-02-16T01:05:32.852-08:00</updated><title type='text'>Java</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javathreads.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1816645927538051205/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://javathreads.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Abhishek Mehta</name><uri>http://www.blogger.com/profile/18069471551641467742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='26' src='http://lh3.google.com/image/abhishek.mehta/RYfI65Q0XXI/AAAAAAAAAAg/0xfTwZQQsTA/s288/DSC00019.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1816645927538051205.post-8446747080551524275</id><published>2007-04-05T01:51:00.000-07:00</published><updated>2007-04-05T07:33:34.665-07:00</updated><title type='text'>JDBC</title><content type='html'>&lt;p class="MsoListParagraphCxSpFirst" style=""&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;JDBC&lt;/span&gt;&lt;span style="font-size:100%;"&gt; (Java Database Connectivity) is a standard interface for connecting from java to &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;relational database&lt;/span&gt;&lt;span style="font-size:100%;"&gt;. JDBC provides programmers a facility to generate runtime &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;SQL&lt;/span&gt;&lt;span style="font-size:100%;"&gt; commands.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoListParagraphCxSpFirst" style=""&gt;&lt;span style="font-size:100%;"&gt;The architecture of JDBC has four main components.  &lt;/span&gt;&lt;/p&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Application&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Driver Manager&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Drivers&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Data Source&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p class="MsoListParagraphCxSpFirst" style=""&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Application &lt;/span&gt;&lt;span style="font-size:100%;"&gt;initiates and terminates the connection with the data source. It also set transaction boundaries , submits SQL statements and retries the results.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoListParagraphCxSpFirst" style=""&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Driver Manager&lt;/span&gt;&lt;span style="font-size:100%;"&gt; load the JDBC driver and pass JDBC function calls from application to the correct driver. It also perform the error checking.&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoListParagraphCxSpFirst" style=""&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Driver &lt;/span&gt;&lt;span style="font-size:100%;"&gt;establishes the connection with data source&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoListParagraphCxSpFirst" style=""&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Data source&lt;/span&gt;&lt;span style="font-size:100%;"&gt; processes the commands from the driver and returns the results.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoListParagraphCxSpFirst" style=""&gt;&lt;span style="font-size:100%;"&gt;So one can say that &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;JDBC&lt;/span&gt;&lt;span style="font-size:100%;"&gt; is nothing but a collection of Java classes and interfaces that enables database access from programs written in the Java language. An Java application works in following steps:&lt;/span&gt;&lt;/p&gt;&lt;!--[endif]--&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Creates a database connection object&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Create a statement object to execute SQL statement by using database connection object.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:100%;"&gt;Access the result from Result Set Object by executing statement object.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;!--[if !supportLists]--&gt;&lt;!--[endif]--&gt;&lt;!--[if !supportLists]--&gt;&lt;!--[endif]--&gt;      &lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;This API has a SQLException class to handle the exception at runtime.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Note :&lt;/span&gt;&lt;span style="font-size:100%;"&gt; In this article all example are MySQL specific&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p style="font-weight: bold;" class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;Create database connection&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;  &lt;div style="text-align: center;"&gt;    &lt;/div&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;To establish a database connection JDBC API provides &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Data Source&lt;/span&gt;&lt;span style="font-size:100%;"&gt; interface and &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Driver Manager&lt;/span&gt;&lt;span style="font-size:100%;"&gt; class which has a &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;getConnection()&lt;/span&gt;&lt;span style="font-size:100%;"&gt; method which returns a Connection object. Here programmer is suppose to &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;register the suitable driver&lt;/span&gt;&lt;span style="font-size:100%;"&gt; before calling getConnection() method. Driver is nothing but a software program that translate the JDBC calls into DBMS specific calls. There are two ways to register the driver.&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p  style="text-align: left; font-style: italic; color: rgb(153, 51, 0);font-family:arial;" class="MsoNormal"&gt;&lt;span style=";font-size:100%;" &gt;DriverManager.registerDriver(&lt;/span&gt;&lt;span style="color: rgb(153, 51, 0); font-weight: bold;font-size:100%;" &gt;&lt;span style=""&gt;new&lt;/span&gt;&lt;/span&gt;&lt;span style=";font-size:100%;" &gt; com.mysql.jdbc.Driver());&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="font-style: italic; color: rgb(153, 51, 0);"&gt;  &lt;/div&gt;&lt;p  style="text-align: left; font-style: italic;font-family:arial;" class="MsoNormal"&gt;&lt;span style="line-height: 115%;font-size:100%;color:black;"  &gt;or&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;p  style="text-align: left; font-style: italic; color: rgb(153, 51, 0);font-family:arial;" class="MsoNormal"&gt;&lt;span style="line-height: 115%;font-size:100%;" &gt;Class.forName(&lt;/span&gt;&lt;span style="line-height: 115%;font-size:100%;" &gt;"com.mysql.jdbc.Driver"&lt;/span&gt;&lt;span style="line-height: 115%;font-size:100%;" &gt;);&lt;/span&gt;&lt;/p&gt;  &lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;   Calling the&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;code&gt;&lt;/code&gt; &lt;/span&gt;&lt;span style="line-height: 115%;font-size:100%;color:black;"  &gt;Class.&lt;i&gt;forName &lt;/i&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;automatically creates an instance of a driver and registers it with the DriverManager, so you don't need to create an instance of the class. This way the second way is better than the first because here you will not face any kind of duplication.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:100%;"&gt;After registering the driver first you are suppose to create a URL, this requires info related to authentication to the connection string. Either you can embed the authentication info to the url string or you can use java.util.Property class to pass the info to the getConnection().&lt;br /&gt;&lt;br /&gt;Here is the example for both&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div style="text-align: left;"&gt;&lt;span style="font-style: italic; color: rgb(153, 51, 0);font-size:100%;" &gt;String url= " jdbc:mysql: //localhost:" + portNumber+ "/dbName? user= dbUser &amp; password ="+ dbPassword ;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(153, 51, 0);font-size:100%;" &gt;Connection connectionObj = DriverManager.getConnection(url);&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic;font-size:100%;" &gt;or&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(153, 51, 0);font-size:100%;" &gt;java.util.Properties dbInfo = new java.util.Properties();&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(153, 51, 0);font-size:100%;" &gt;info.put ("defaultRowPrefetch","5");&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(153, 51, 0);font-size:100%;" &gt;info.put ("user", dbUser);&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(153, 51, 0);font-size:100%;" &gt;info.put ("password", dbPassword );&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(153, 51, 0);font-size:100%;" &gt;String url= "jdbc:mysql://localhost:"+ portNumber+"/dbName";&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(153, 51, 0);font-size:100%;" &gt;Connection connectionObj = DriverManager.getConnection(url , info);&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;Second method is better than the first because here one can also set the value of number of rows to be fetched while returning the ResultSet object. Here one thing is very important that this feature is driver specific, driver may ignore this count.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;In above example I am providing dbName to the Connection URL, in case one doesn't provide the dbName to the connection URL one is suppose to call static SQL statement with following query string to select the database "use database dbName".&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="font-weight: bold;" class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;Create a statement&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;JDBC API provides &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Statement&lt;/span&gt;&lt;span style="font-size:100%;"&gt; interface to create a static SQL statement. This interface has two sub interfaces &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;PreparedStatement &lt;/span&gt;&lt;span style="font-size:100%;"&gt;and &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;CallableStatement&lt;/span&gt;&lt;span style="font-size:100%;"&gt;.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;Object of Statement can be get from &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Connection &lt;/span&gt;&lt;span style="font-size:100%;"&gt;object. Here is the code fragment to get the Statement object&lt;/span&gt;&lt;/p&gt;&lt;p style="color: rgb(153, 51, 0); font-style: italic;" class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;Statement stmt = connectionObj.createStatement();&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;This interface has execute(), executeQuery() and executeUpdate() methods to execute SQL statements.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="color: rgb(153, 51, 0); font-style: italic;" class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;String myQuery = "select * from my_table";&lt;br /&gt;ResultSet rs = stmt.executeQuery(myQuery );&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;Its good habit to call the &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;close()&lt;/span&gt;&lt;span style="font-size:100%;"&gt; method to release the statement object as it also clears the ResultSet object automatically.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;If one wants to execute the query statement multiple times then it is better to use prepared statements. Because by using Statement every time it does some kind of validation of SQL syntax and pseudo code generation but by using PreparedStatement  it will be executed only once and this precompiled  SQL statement is stored in PreparedStatement object. Due to this the RDBMS caches the query plan for this execution and makes it performance wise better then the Statement.&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;Here is pseudo code to create a &lt;span style="font-weight: bold;"&gt;PreparedStatement&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="color: rgb(153, 51, 0); font-style: italic;" class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;String myQuery = "select * from my_table where id = ?";&lt;br /&gt;PreparedStatement pstmt = connectionObj.prepareStatement(myQuery);&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;This will create the PreparedStatement  object which can be used multiple times. For executing a query initially one is suppose to set the variable value (here it is id) according to its type. Suppose it is a int then one will call&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style="color: rgb(153, 51, 0); font-style: italic;" class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;pstmt.setInt(1,idValue);&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;After setting the value one will call&lt;/span&gt;&lt;/p&gt;&lt;p style="color: rgb(153, 51, 0); font-style: italic;" class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;ResultSet rs = pstmt.executeQuery();&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;This will execute the query and store the result in ResultSet object. Once you are done with all execution call &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;close() &lt;/span&gt;&lt;span style="font-size:100%;"&gt;method to release the PreparedStatement object.&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;JDBC also provide the facility to execute &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Procedure&lt;/span&gt;&lt;span style="font-size:100%;"&gt; from your Java program for that it has &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;CallableStatement &lt;/span&gt;&lt;span style="font-size:100%;"&gt;interface. CallableStatement object can be created from connection object and pseudo code for this is&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(153, 51, 0); font-style: italic;"&gt;CallableStatement cstmt = connectionObj.prepareCall("{call my_procedure(?,?)}");&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here we have two input parameters so before calling the execute command one is suppose to set the value of both parameter&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(153, 51, 0);font-size:100%;" &gt;&lt;span style="font-style: italic;"&gt;cstmt.setInt(1, intValue);&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(153, 51, 0); font-style: italic;"&gt;cstmt.setString(2, strValue);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If this procedure is suppose to returns some value in that case programmer has to register that output parameter by calling following command.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(153, 51, 0); font-style: italic;font-size:100%;" &gt;cstmt.registerOutParameter(1, Types.INTEGER);&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(153, 51, 0); font-style: italic;"&gt;cStmt.registerOutParameter(2, Types.VARCHAR);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;After this initialization call &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;execute() &lt;/span&gt;&lt;span style="font-size:100%;"&gt;method like&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(153, 51, 0); font-style: italic;"&gt;boolean hadResults = cstmt.execute();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here the return type is boolean this will be &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;true&lt;/span&gt;&lt;span style="font-size:100%;"&gt; if the first result is a ResultSet object and &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;false &lt;/span&gt;&lt;span style="font-size:100%;"&gt;if the first result is an update count or there is no  result and to access the ResultSet&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="color: rgb(153, 51, 0); font-style: italic;"&gt;ResultSet rs = cstmt.getResultSet();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In similar manner CallableStatement interface has &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;close() &lt;/span&gt;&lt;span style="font-size:100%;"&gt;method to release the object of CallableStatement.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Access the result&lt;/span&gt;&lt;span style="font-size:100%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;By &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;execute()&lt;/span&gt;&lt;span style="font-size:100%;"&gt; method one can get the object of &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;ResultSet&lt;/span&gt;&lt;span style="font-size:100%;"&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt; ResultSet object maintains the cursor to get the data from it. Initially this cursor is positioned before the first row. ResultSet interface provides &lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;next() &lt;/span&gt;&lt;span style="font-size:100%;"&gt;method to access the next row in it.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;It has getter method to access the value of the columns from row and input type to these methods are int or String. By this way it provides programmers a facility to access the column value by the column name or by column  number. Here is the pseudo code to access the ResultSet.&lt;/span&gt;&lt;/p&gt;&lt;p style="color: rgb(153, 51, 0); font-style: italic;" class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;while(rs.next())&lt;br /&gt;{&lt;br /&gt;emp_id = rs.getInt(1);&lt;br /&gt;emp_name = rs.getString(2);&lt;br /&gt;emp_type = rs.getString("emp_type");&lt;br /&gt;}&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;Suppose two column have same name in that case whichever column it access first the value of that column will be returned so unless you are sure with the column name its good habit to access the column value by number.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;ResultSet interface also provides the facility of updater method by which you can insert and update the data in databse. But the default ResultSet is not updatable and has a cursor that moves forwards only. Thus one can iterate on this only once from first row to last row. It is possible to make this scrollable and updatable. Here is the pseudo code for the same&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color: rgb(153, 51, 0); font-style: italic;font-size:100%;" &gt;Statement stmt = connectionObj.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,                                      ResultSet.CONCUR_UPDATABLE);&lt;br /&gt;&lt;br /&gt;String myQuery = "select * from my_table";&lt;br /&gt;ResultSet rs = stmt.executeQuery(myQuery);&lt;/span&gt;&lt;p class="MsoNormal"&gt;&lt;span style="font-size:100%;"&gt;By creating Statement like this one can scroll and update it.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1816645927538051205-8446747080551524275?l=javathreads.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1816645927538051205/posts/default/8446747080551524275'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1816645927538051205/posts/default/8446747080551524275'/><link rel='alternate' type='text/html' href='http://javathreads.blogspot.com/2007/04/jdbc.html' title='JDBC'/><author><name>Abhishek Mehta</name><uri>http://www.blogger.com/profile/18069471551641467742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='26' src='http://lh3.google.com/image/abhishek.mehta/RYfI65Q0XXI/AAAAAAAAAAg/0xfTwZQQsTA/s288/DSC00019.JPG'/></author></entry><entry><id>tag:blogger.com,1999:blog-1816645927538051205.post-879767164109137822</id><published>2007-04-02T00:56:00.000-07:00</published><updated>2008-12-11T00:19:38.326-08:00</updated><title type='text'>Threads</title><content type='html'>In recent days I have worked on Threads that makes me to write a blog on it. Here I am sharing some of the basic characteristics of Threads.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Some Definitions&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;In an operating system a running program is known as &lt;span style="font-weight: bold; font-style: italic;"&gt;Process&lt;/span&gt; which internally can have separate runnable &lt;span style="font-weight: bold; font-style: italic;"&gt;tasks&lt;/span&gt;. These tasks are knows as &lt;span style="font-weight: bold; font-style: italic;"&gt;Threads&lt;/span&gt; so we can say that thread is single sequential flow of process and it is also known as &lt;span style="font-weight: bold; font-style: italic;"&gt;lightweight process&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;If there are multiple programs or processes are executed it is known as &lt;span style="font-weight: bold; font-style: italic;"&gt;Multi-Programming&lt;/span&gt; and when there is concurrency exists among different threads it is known as &lt;span style="font-weight: bold; font-style: italic;"&gt;Multi-Threading&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; font-style: italic;"&gt;Thread&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Every Java program has &lt;span style="font-weight: bold;"&gt;at least one thread&lt;/span&gt; which is executed when the main()  is invoked. Java also  supports Multi-Threading for that it has &lt;span style="font-weight: bold; font-style: italic;"&gt;class&lt;/span&gt; ( java.lang.Thread )  and &lt;span style="font-weight: bold; font-style: italic;"&gt;interface&lt;/span&gt; ( java.lang.Runnable). It uses java.lang.Object class to call &lt;span style="font-weight: bold; font-style: italic;"&gt;methods&lt;/span&gt; like wait() , notify() and notifyAll(). It also has concept of &lt;span style="font-weight: bold; font-style: italic;"&gt;synchronize&lt;/span&gt; on an Object which internally provides a facility that only one thread can access that object at any given point in time.&lt;br /&gt;&lt;br /&gt;Note : You can't change threads status after it has been started.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Creating a Thread&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Java provides two way to instantiate a thread&lt;br /&gt;1. By extending it from Thread class&lt;br /&gt;class myThread extends Thread {&lt;br /&gt;myThread()    {&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;2. By implementing it from Runnable interface&lt;br /&gt;class myThread implements Runnable {&lt;br /&gt;myThread()    {&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void run()  {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;This is helpful when your class is already extending any other class.&lt;br /&gt;&lt;br /&gt;You can also give a particular name to Thread.&lt;br /&gt;Thread myThread = new Thread("MyThread");&lt;br /&gt;&lt;br /&gt;If you do not provide any name in that case it will classify Thread name as Thread-m where m is an integer which will vary from 0 to m.&lt;br /&gt;&lt;br /&gt;Java also provides a facility to group particular Threads by &lt;span style="font-weight: bold;"&gt;ThreadGroup&lt;/span&gt; class. By this we can handle multiple threads as a unit , all threads in same group can access information about other threads within that group.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Starting a Thread&lt;/span&gt;&lt;br /&gt;To actually start a thread you must invoke its start() method. This method internally allocate required system resources and call run() method of the Thread class. You are suppose to override run() method according to your requirement.&lt;br /&gt;&lt;br /&gt;Each thread has life cycle and during this life cycle it will be in one of the following state :&lt;br /&gt;1. New&lt;br /&gt;2. Runnable&lt;br /&gt;3. Not Runnable&lt;br /&gt;4. Dead&lt;br /&gt;&lt;br /&gt;Calling &lt;span style="font-weight: bold;"&gt;new&lt;/span&gt; will create a new Thread but it will not allocate any system resources to it until we call start() method. One thing you must notice that after calling new you can only able to call start() method otherwise it will throw IllegalThreadStateException.&lt;br /&gt;&lt;br /&gt;once the run()  method is called the state of Thread becomes &lt;span style="font-weight: bold;"&gt;Runnable&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There are 3 possible way to make a thread to &lt;span style="font-weight: bold;"&gt;Non Runnable&lt;/span&gt; state&lt;br /&gt;1. sleep()&lt;br /&gt;2. wait()&lt;br /&gt;3. Blocked for I/O&lt;br /&gt;&lt;br /&gt;It will gain  the Runnable state whenever it meets the required conditions. Following possible ways may change the state from Non Runnable to Runnable&lt;br /&gt;1. notify() or notifyAll()&lt;br /&gt;2. When called milliseconds in sleep() method elapsed.&lt;br /&gt;3. When it get the access to Blocked I/O resource.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Dead&lt;/span&gt; state will occur when thread comes out from run() method or anyone call destroy() method for that thread.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TbTeivXW6mU/RhHs_HkgA-I/AAAAAAAAA2s/bxrlBLgIZsU/s1600-h/threadStates.gif"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer;" src="http://4.bp.blogspot.com/_TbTeivXW6mU/RhHs_HkgA-I/AAAAAAAAA2s/bxrlBLgIZsU/s320/threadStates.gif" alt="" id="BLOGGER_PHOTO_ID_5049077226367091682" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;One thing you must notice that &lt;span style="font-weight: bold; font-style: italic;"&gt;interrupt&lt;/span&gt; doesn't kill any thread and any thread can jump from Runnable to Non Runnable or vice versa from two methods&lt;br /&gt;1. Scheduling&lt;br /&gt;2. programming control&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Scheduling&lt;/span&gt;&lt;br /&gt;It is nothing but execution of multiple threads in order. Java uses priority algorithms for scheduling. High priority threads will have preference on low priority threads. It doesnt mean that high priority thread will run all time it depends on the OS. Scheduling is of two types&lt;br /&gt;1. Preemptive scheduling:  In this higher thread will get preference until it gets over only other higher level thread can make that thread to wait.&lt;br /&gt;2. Time slicing : In this each thread get the access for a scheduled period of time.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Ending a Thread&lt;/span&gt;&lt;br /&gt;A thread normally ends when its execution completes but there are other methods to stop the execution of running thread.&lt;br /&gt;1. interrupt()&lt;br /&gt;2. join()&lt;br /&gt;3. stop()&lt;br /&gt;4. destroy()&lt;br /&gt;5. suspend() and resume() : depricated&lt;br /&gt;6. yield()&lt;br /&gt;7. setDemon(true)&lt;br /&gt;&lt;br /&gt;These are the basic characteristics of threads.&lt;br /&gt;&lt;br /&gt;To read more on threads&lt;br /&gt;&lt;a href="http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html"&gt;http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Next blog will be on : How Thread Synchronization works.......&lt;br /&gt;&lt;div style="text-align: justify;"&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1816645927538051205-879767164109137822?l=javathreads.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javathreads.blogspot.com/feeds/879767164109137822/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1816645927538051205&amp;postID=879767164109137822' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1816645927538051205/posts/default/879767164109137822'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1816645927538051205/posts/default/879767164109137822'/><link rel='alternate' type='text/html' href='http://javathreads.blogspot.com/2007/04/threads.html' title='Threads'/><author><name>Abhishek Mehta</name><uri>http://www.blogger.com/profile/18069471551641467742</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='33' height='26' src='http://lh3.google.com/image/abhishek.mehta/RYfI65Q0XXI/AAAAAAAAAAg/0xfTwZQQsTA/s288/DSC00019.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TbTeivXW6mU/RhHs_HkgA-I/AAAAAAAAA2s/bxrlBLgIZsU/s72-c/threadStates.gif' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
