Advance Java Module 5
Advance Java Module 5
.IN
• When using JDBC, Java programmers have the ability to request connections to a
database, send queries to the database using SQL statements, and receive results for
advanced processing.
C
N
SY
2. JDBC Drivers
• To connect with individual databases, JDBC requires drivers for each database.
U
• The advantage for using this type of driver is that it allows access to almost any database
since the database ODBC drivers are readily available.
• Disadvantages for using this type of driver include the following:
U
• Performance is degraded since the JDBC call goes through the bridge to
the ODBC driver then to the native database connectivity interface. The
VT
• C
Advantages for using this type of driver include the following:
Allows access to almost any database since the databases ODBC drivers are readily
N
available
SY
• Offers significantly better performance than the JDBC/ODBC Bridge and Type 2 Drivers
• Scalable
• Caching
U
.IN
{
S.o.p(e);
}
Connect to the dbms: C
N
• Once driver is loaded, the j2ee components must connect to the dbms using the static
SY
method getConnection().
• Where getConnection() methods belong to class called as DriverManager.
• getConnection() method passed the URL as argument of database and username
U
,password if necessary to [Link] URL is the string object that contains the driver
name and databse name that is being accessed by the j2ee components.
VT
.IN
▪ ExecuteUpadate(String)
▪ Execute(string)
Code snippet :
try C
N
{
[Link](“[Link]”);
SY
Connection c=[Link](“JDBC:ODBC:CSB”);
Statement s=[Link]();
U
catch(Exception e)
{
S.o.p(e);
}
Process data returned by the dbms:
• ResultSet object is assigned to receive the data from the DBMS after the query processed.
• ResultSet object conatins the method used to intract with the data that is returned by
DBMS to the j2ee components.
• Next() method is used to process the data from the [Link] is pointing to the first row of
table. Next() method is always used in iterative process.
.IN
ResultSet r=[Link](“Select *from emp”);
while([Link]())
{
String name=[Link](1); C
N
[Link](“name=”+name);
SY
}
catch(Exception e)
{
U
S.o.p(e);
}
VT
.IN
Connection c=[Link](“JDBC:ODBC:CSB”);
Statement s=[Link]();
ResultSet r=[Link](“Select *from emp”);
while([Link]())
C
N
{
String name=[Link](1);
SY
String usn=[Link](2);
[Link](“name=”+name);
[Link](“USN=”+usn);
U
}
VT
[Link]();
}
catch(Exception e)
{
S.o.p(e);
}
}
public stataic void main(String ar[])
{
A a1=new A();
}
}
5. Database connection :
• Connection can be established using the [Link]() method.
• The data source that the jdbc components will connect to is defined using the url format.
.IN
The url consist of three parts.
• JDBC-Which indicates that the jdbc protocol is to be used to read the url
•
•
C
<subprotocal>-which indicates the jdbc driver name
<subname>- which indicates the name of the database.
N
• the three overloaded [Link]() methods −
SY
• getConnection(String url)
• getConnection(String url, Properties prop)
• getConnection(String url, String user, String password)
U
VT
.IN
}
catch(Exception e)
{
[Link](e); C
N
}
SY
.IN
Number/databaseName
Connection conn = [Link](URL, USER, PASS);
• preparedStatemnt object
VT
• callableStatement object
.IN
A()
{
try
{ C
N
[Link](“[Link]”);
SY
Connection c=[Link](“JDBC:ODBC:CSB”);
Statement s=[Link]();
ResultSet r=[Link](“Select *from emp”);
U
while([Link]())
{
VT
String name=[Link](1);
String usn=[Link](2);
[Link](“name=”+name);
[Link](“USN=”+usn);
}
[Link]();
}
catch(Exception e)
{
S.o.p(e);
}
.IN
PreparedStatement p=new PreparedStatement(“select name from emp
where usn=?”);
• C
The setXXX() methods bind values to the parameters, where XXX represents the Java
N
data type of the value you wish to bind to the input parameter.
SY
o setXXX(int,string);
• First parameter represent the column index and second parameter represent the values
that replace the ? mark in the query.
U
class A
{
A()
{
try
{
[Link](“[Link]”);
Connection c=[Link](“JDBC:ODBC:CSB”);
PreparedStatement p=[Link](“select name from emp where usn=?”);
[Link](2, ”12cs001”);
ResultSet r=[Link]();
.IN
{
S.o.p(e);
}
} C
N
public static void main(String ar[])
SY
{
A a1=new A();
}
U
}
The CallableStatement Objects
VT
• Just as a Connection object creates the Statement and PreparedStatement objects, it also
creates the CallableStatement object, which would be used to execute a call to a database
stored procedure.
• Three types of parameters exist: IN, OUT, and INOUT. The PreparedStatement object
only uses the IN parameter. The CallableStatement object can use all the three.
• Here are the definitions of each −
Parameter Description
A parameter whose value is unknown when the SQL statement is
IN created. You bind values to IN parameters with the setXXX()
methods.
.IN
PreparedStatement object; use the setXXX() method that corresponds to the Java data
type you are binding.
• When you use OUT and INOUT parameters you must employ an additional
C
CallableStatement method, registerOutParameter(). The registerOutParameter() method
binds the JDBC data type, to the data type that the stored procedure is expected to return.
N
• Once you call your stored procedure, you retrieve the value from the OUT parameter
SY
with the appropriate getXXX() method. This method casts the retrieved value of SQL
type to a Java data type.
Program:
U
import [Link].*;
VT
class A
{
A()
{
try
{
[Link](“[Link]”);
Connection c=[Link](“JDBC:ODBC:CSB”);
CallableStatement p=[Link](“Call lastOrderNumber(?)”);
[Link](1,[Link]);
[Link]();
.IN
public stataic void main(String ar[])
{
A a1=new A();
} C
N
}
SY
6. ResultSet
A ResultSet consists of records. Each records contains a set of columns.
A ResultSet can be of a certain type. The type determines some characteristics and abilities of the
U
ResultSet.
VT
Scrollable ResultSet:
At the time of writing there are three ResultSet types:
1. ResultSet.TYPE_FORWARD_ONLY
2. ResultSet.TYPE_SCROLL_INSENSITIVE
3. ResultSet.TYPE_SCROLL_SENSITIVE
The default type is TYPE_FORWARD_ONLY
• TYPE_FORWARD_ONLY means that the ResultSet can only be navigated forward. That is,
you can only move from row 1, to row 2, to row 3 etc. You cannot move backwards in
the ResultSet.
• TYPE_SCROLL_INSENSITIVE means that the ResultSet can be navigated (scrolled) both
forward and backwards. You can also jump to a position relative to the current position,
.IN
Method Description
absolute() Moves the ResultSet to point at an absolute position. The position is a row
C
number passed as parameter to the absolute() method.
N
afterLast() Moves the ResultSet to point after the last row in the ResultSet.
SY
beforeFirst() Moves the ResultSet to point before the first row in the ResultSet.
first() Moves the ResultSet to point at the first row in the ResultSet.
U
last() Moves the ResultSet to point at the last row in the ResultSet.
VT
next() Moves the ResultSet to point at the next row in the ResultSet.
previous() Moves the ResultSet to point at the previous row in the ResultSet.
relative() Moves the ResultSet to point to a position relative to its current position. The
relative position is passed as a parameter to the relative method, and can be
both positive and negative.
PROGRAM:
import [Link].*;
.IN
While([Link]())
{
String name=[Link](1);
String usn=[Link](2); C
N
[Link](“name=”+name);
[Link](“USN=”+usn);
SY
}
[Link]();
U
[Link]([Link](1));
[Link]();
VT
[Link]([Link](1));
[Link]();
[Link]([Link](1));
[Link](2);
[Link]([Link](1));
[Link](2);
[Link]([Link](1));
[Link](-2);
[Link]([Link](1));
[Link]();
}
.IN
Updatable ResultSet :
• The ResultSet concurrency determines whether the ResultSet can be updated, or only
•
read.
C
A ResultSet can have one of two concurrency levels:
N
1. ResultSet.CONCUR_READ_ONLY
SY
2. ResultSet.CONCUR_UPDATABLE
• CONCUR_READ_ONLY means that the ResultSet can only be read.
• CONCUR_UPDATABLE means that the ResultSet can be both read and updated.
U
• If a ResultSet is updatable, you can update the columns of each row in the ResultSet. You
• updateRow() is called that the database is updated with the values of the row
import [Link].*;
class A
{
A()
{
try
{
[Link](“[Link]”);
Connection c=[Link](“JDBC:ODBC:CSB”);
.IN
}
[Link]();
}
catch(Exception e) C
N
{
SY
S.o.p(e);
}
}
U
A a1=new A();
}
}
Inserting Rows into a ResultSet
If the ResultSet is updatable it is also possible to insert rows into it. You do so by:
1. update row column values using updateXX(string,string);
2. call [Link]()
import [Link].*;
class A
{
A()
.IN
{
String name=[Link](1);
String usn=[Link](2);
[Link](“name=”+name); C
N
[Link](“USN=”+usn);
SY
}
[Link]();
}
U
catch(Exception e)
{
VT
S.o.p(e);
}
}
public stataic void main(String ar[])
{
A a1=new A();
}
}
.IN
Try
{
[Link](“[Link]”);
C
Connection c=[Link](“JDBC:ODBC:CSB”);
N
Statement s=[Link](ResultSet.CONCUR_UPDATABLE);
ResultSet r=[Link](“Select *from emp ”);
SY
[Link](0);
while([Link]())
U
{
String name=[Link](1);
VT
String usn=[Link](2);
[Link](“name=”+name);
[Link](“USN=”+usn);
}
[Link]();
}
catch(Exception e)
{
S.o.p(e);
}
}
.IN
• If one of sql is failed, the sql statement that is executed successfully upto the point in the
• releaseSavePoint(String);-it realse the save point assing to the sql statement if and only
U
• rollback();-if one of the sql statement is failed,then rollback() method is invoked and
control goes back to the fail sql statement for further execution.
Program:
import [Link].*;
class A
{
A()
{
try
{
[Link](“[Link]”);
.IN
}
catch(Exception e)
{
S.o.p(e); C
N
[Link]();
SY
}
}
public static void main(String ar[])
U
{
A a1=new A();
VT
}
}
8. Metadata:
Metadata is data about data. J2ee component can access metadata by using
• DatabaseMetaData interface.
• ResultSetMetaData interface
DatabaseMetaData interface:
• The DatabaseMetaData interface is used to retrieve information about database,table,columns
connection interface object. The getMetaData() method return a DatabaseMetaData object that
contain information of database and components.
• Most commonly used DatabaseMetaData interface methods as follows:
.IN
program:
import [Link].*;
class A
{ C
N
A()
{
SY
try
{
[Link](“[Link]”);
U
Connection c=[Link](“JDBC:ODBC:CSB”);
VT
Statement s=[Link]();
ResultSet r=[Link](“Select *from emp”);
DatabaseMetaData d=[Link]();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
[Link]();
}
catch(Exception e)
{
ResultSetMetaData interface:
.IN
• ResultSetMetaData interface is used to retrieve the information by calling the getMetaData()
• C
getColunmCount()-returns the number of column available in the table
N
• getColunmName(int)-returns the name of column specified by the column
number
SY
import [Link].*;
class A
VT
{
A()
{
try
{
[Link](“[Link]”);
Connection c=[Link](“JDBC:ODBC:CSB”);
Statement s=[Link]();
ResultSet r=[Link](“Select *from emp”);
ResultSetMetaData d=[Link]();
[Link]([Link](1));
.IN
public static void main(String ar[])
{
A a1=new A();
} C
N
}
SY
Data Types:
The JDBC driver converts the Java data type to the appropriate JDBC type, before sending it to the
U
database. It uses a default mapping for most data types. The following table summarizes the default
JDBC data type that the Java data type is converted to, when you call the setXXX() method.
VT
SQL JDBC/Java
VARCHAR String
CHAR String
LONGVARCHAR String
BIT boolean
NUMERIC [Link]
TINYINT byte
SMALLINT short
INTEGER int
BIGINT long
REAL float
FLOAT float
DOUBLE double
Exception :
SQLException Methods
.IN
An SQLException can occur both in the driver and the database. When such an exception occurs,
an object of type SQLException will be passed to the catch clause.
C
The passed SQLException object has the following methods available for retrieving additional
information about the exception −
N
Method Description
SY