Connection Polling Transactions Result Set Types
Connection Polling Transactions Result Set Types
Connection Polling Transactions Result Set Types
2) Updatable 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.
2) Updatable ResultSet:-
It 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.
2)Scrollable ResultSet
It is a ResultSet object, which will allow the users to iterate the data in
These are the ResultSet objects, which will allow the users to iterate the data in
both forward and backward directions.
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.
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
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.
ResultSet rs = con.executeQuery(.);
23: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.
Where xxx may be byte, short, char, int, long, float, double.
note:-
this method is
import java.sql.*;
Class.forName(driver className);
Connection con =
DriverManager.getConnection(jdbc:oracle:thin:@localhost:1521:XE,system,man
ager);
Statement st =
con.createStatement(ResultSet.TYPE_SCROLL_SENSEITIVE,ResultSet.CONCUR_UP
DATABLE);
rs.next();
Rs.refreshRow();
this method is
Where xxx may be byte, short, int, char, double, float, long.
The following example demonstrates how to insert no. of records onto the database
through Updatable ResultSet objects.
import java.sql.*;
import java.io.*;
Class.forName(driverClass);
rs.moveToInsertRow();
while(true)
rs.updateInt(1,eno);
rs.updateString(2,ename);
rs.updateFloat(3,esal);
rs.updateString(4,eaddr);
rs.insertRow();
if(option.equals(n))
break;
}
}
Step1: Get the Updatable ResultSet objectd with the fetched data.
Step2: Move ResultSet cursor to a record where we want to perform updations, for
this we will use the following method.
Step3: Perform Temporary updations on to the particular record, for this we will
use following method.
import java.sql.*;
Class.forName(driverclassName);
Statement st =
con.createStatement(ResultSet.TYPE_SCROLL_SENSEITIVE,ResultSet.CONCUR_UP
DATABLE);
rs.absolute(3);
rs.updateFloat(3,newsal);
rs.updateRow();
Connection Pooling:-
In jdbc as a java programmer if we want to obtain a connection with a database
then we have the following two approaches.
DataSource will creates the logical connection to java application whenever the
application closes the logical connection .DataSource will send that connection back
to pool.
1. Every time getting the physical connection from the data base takes more time
and resources because of the above drawback application performance is
reduced.
2. Data base server provider for web-application will charge more why because
each new connection request is costly.
3. Even multithreaded environment is also not suggested, we find the problems
such as scalability, consistency... etc.
ans: connection pool is a group of logical data base connections associated with a
physical data base connection.
What is connection pooling?
The component of the application (project) uses this logical connection after using
the logical connection component gives that logical connection back to the provider
by calling close method call.
Connection pooling implementations:
In real time (or in industry) for web applications (or for web sites) server provided
connection pooling mechanism is used.
javax.sql.DataSource
javax.sql.ConnectionPoolDataSource
javax.sql.PooledConnection
1. Basic implementation
More clearly web server and application server providers will define a class that
implements javax.sql.DataSource by using this class object we cannot get physical
connection we can get logical connection that is already connected to a physical
data base connection.
This is used to getting physical data base connection and this is similar to
DriverManager
oracle.jdbc.pool.OracleDataSource ds;
ds = new oracle.jdbc.pool.OracleDataSource();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
ds.setURL(url);
Connection con= ds.getConnection("system", "sathish");
javax.sql.PooledConnection interfaces:
PooledConnection interface:
ConnectionPoolDataSource:
import javax.sql.*;
import java.sql.*;
import oracle.jdbc.pool.*;
DataSource :
DataSource will act as a meditator between a java application and a
Connection pool.
The Application Developer will get the DataSource object from JNDI Registry.
To get the DataSource object the Programmer will use InitialContext object
lookup(-) method
in between <context></context>
Step2:
DataSource dataSource=ic.lookup("jndiName");
C:\TOMCAT7\WEBAPPS\conncetionpoolingapp1
+---WEB-INF
web.xml
---classes
FirstServlet.java
+---com
Nit
FirstServlet.class
FirstServlet.java
package com.nit;
import javax.naming.*;
import java.io.*;
import javax.servlet.*;
import java.sql.*;
import javax.sql.*;
Connection con1=ds1.getConnection();
Connection con2=ds1.getConnection();
Connection con3=ds1.getConnection();
con1.close();
}catch(SQLException sqle){
sqle.printStackTrace(pw);
}catch(NamingException ne){
ne.printStackTrace(pw);
}
}
}
web.xml
<web-app>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.nit.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>
Transaction-Management :
What is meant by Transaction? How it is possible to
maintain Transactions in JDBC applications?
Transaction is nothing but an unit of work performed by
the applications. Every transaction should have the
following properties.
Atomicity
Consistency
Isolation
Durability
To increase performance
conn.setAutoCommit(false);
commit & rollback
Once you are done with your changes and you want to
commit the changes then call commit() method on
connection object as follows:
conn.commit( );
conn.rollback( );
DriverConnection.java
package com.nit.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DriverConnection {
static{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn()throws
SQLException{
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@loc
alhost:1521:xe","system" , "manager");
return con;
}
}
TestCase.java
package com.nit;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.nit.util.DriverConnection;
public class TestCase {
public static void main(String[] args) throws
SQLException{
Connection con=null;
try{
con=DriverConnection.getConn();
con.setAutoCommit(false);
Statement st=con.createStatement();
int count=st.executeUpdate("INSERT INTO STUDENT
VALUES(89,NITSTUDENT2',85)");
System.out.println("Number of Rows are Inserted:"+i);
If(count>0){
con.commit();
}
}
catch(SQLException e) {
con.rollback();
System.out.println(e);
}
}
}
OUTPUT
package com.nit;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.nit.util.DriverConnection;
public class TestCase {
public static void main(String[] args) throws
SQLException{
Connection con=null;
try{
con=DriverConnection.getConn();
con.setAutoCommit(true);
Statement st=con.createStatement();
int i=st.executeUpdate("INSERT INTO STUDENT
VALUES(89,'NITSTUDENT2',85)");
System.out.println("Number of Rows are Inserted:"+i);
}
catch(SQLException e) {
//con.rollback();
System.out.println(e);
}
}
}
OUTPUT
Use-Case: