Chapter5 DB
Chapter5 DB
Marks=12
• Advantages:
1. Easy to maintain
2. Communication is faster.
• Disadvantages:
1. performance will be degrade upon
increasing the users.
• Three-tier Architecture for Data Access:
– divided into three parts: 1)Client layer
2)Business layer
3)Data layer
Three-tier Architecture for Data Access:
• Advantages:
1. Scalability
2. Performance
3. Better Re-usability.
4. Improve Data Integrity.
5. Improved Security – Client can’t directly access
the database.
6. Easy to maintain, to manage, to scale, loosely
coupled etc.
• Disadvantages:
1. Increased Complexity
Java
Application
JDBC
API
• Advantages:
– faster than a type 1 driver
• Disadvantages:
– This driver is a platform dependent.
– Native API driver is database dependent
– It is not a portable driver
– Driver needs to be installed separately in
individual client machines
– client library needs to be installed on client
machine.
Type 3 Driver: Network-Protocol Driver (Pure Java driver for
database Middleware) :
3)Connection con=DriverManager.getConnection("jdbc:mysql://
localhost:3306/dbname","root",“pwd");
4)Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
3) Create the Statement object
• createStatement() method of Connection
interface is used to create statement.
• The object of statement is responsible to execute
queries with the database.
• Syntax of createStatement() method
public Statement createStatement()throws SQLException
• Example:
Statement stmt=con.createStatement();
4) Execute the query
• executeQuery() method of Statement interface is used
to execute queries to the database
• returns the object of ResultSet that can be used to get
all the records of a table.
• Syntax of executeQuery() method
public ResultSet executeQuery(String sql)
throws SQLException
• Example:
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
Close the connection object
void commit() This method makes all the changes made since the
last commit or rollback permanent. It throws
SQLExeception.
Method Description
void rollback() This method undoes all changes made to
the database.
boolean isClosed() Return true if the connection is close
else return false.
void setAutoCommit(boolean It is used to set the commit status.By
status) default it is true.
CallableStatement stmt=con.prepareCall("{call
myprocedure(?,?)}");
ResultSet interface
• Maintains a cursor pointing to a row of a
table.
• Initially, cursor points to before the first row.
• By default, ResultSet object can be moved
forward only and it is not updatable.
Type of ResultSet
ResultSet.TYPE_FORWARD_ONLY The cursor can only move
forward in the result set.
ResultSet.TYPE_SCROLL_INSENSITIVE It indicates that ResultSet
object that is scrollable but
generally not sensitive to
changes to the ResultSet data.
ResultSet.TYPE_SCROLL_SENSITIVE It indicates that ResultSet
object that is scrollable and
sensitive to changes to the
ResultSet data.
Concurrency of ResultSet
ResultSet.CONCUR_READ_ONL Creates a read-only result set. This
Y is the default
ResultSet.CONCUR_UPDATABLE Creates an updatable result set.
Navigating a Result Set
Method Description
public void beforeFirst() throws Moves the cursor just before
SQLException the first row.
public void afterLast() throws Moves the cursor just after the
SQLException last row.
public boolean first() throws Moves the cursor to the first
SQLException row.
public void last() throws Moves the cursor to the last
SQLException row.
public boolean absolute(int row) Moves the cursor to the
throws SQLException specified row
public boolean relative(int row) Moves the cursor the given
throws SQLException number of rows forward or
backward, from where it is
currently pointing
Method Description
public boolean previous() throws Moves the cursor to the
SQLException previous row
public boolean next() throws Moves the cursor to the next
SQLException row. This method returns false
if there are no more rows in
the result set.
public int getRow() throws Returns the row number that
SQLException the cursor is pointing to.
Viewing a ResultSet
• There is a get method for each of the possible
data types, and each get method has two
versions −
– One that takes in a column name.
– One that takes in a column index.