Jdbc 2

Post on 10-May-2015

596 views 3 download

Tags:

Transcript of Jdbc 2

JDBC

tnngo2@gmail.com

Session 2

Stored Procedure can be defined as a group of SQL statements performing a particular task used to group or batch a set of operations or queries to be executed on a database server Faster, more efficient than individual SQL query stmt

Characteristics of Stored Procedure contain SQL statements using constructs and control structures be invoked by name in a application allow an application program to run in two parts such as the application on the client and the stored procedure on the server

Characteristics of Stored Procedure

Benefits of Stored Procedure Reduced network traffic Enhanced hardware and software capabilities Distributed computing Increased security

Client need not have the privilege as DBA Decrease in development cost

Reusing a common stored procedure Centralized control

Security, administrator and maintenance in one place at the server

Create stored procedure using Statement obj Step 1:

Create stored procedure using Statement obj Step 2:

Parameters of a stored procedure Stored procedures can accept data in the form of input parameters that are specified at execution time There are three parameters IN cannot changed or reassigned within module is constant OUT IN/OUT

Creating and executed CallableStatement does not contain the stored procedure itself only a call to it. CallableStatement cst = cn.preparedCall(“{call functioname(?,?)}”)

ResultSet A default result set object cannot be updated or scrolled backward and forward. By default the cursor moves forward only. The characteristics of ResultSet are Scrollable Updatable Holdable Check whether the cursor stays open after a commit

Scrollable ResultSet allows the cursor to be moved to any row in the result set.

Scrollable ResultSet allows the cursor to be moved to any row in the result set.

ResultSet constant values TYPE_FORWARD_ONLY TYPE_SCROLL_INSENSITIVE TYPE_SCROLL_SENSITIVE

Row Positioning Methods method of ResultSet

Row Positioning Methods method of ResultSet interface

Updatable “ResultSet” update rows in a result set using JAVA code rather than SQL commands. update, insert, delete

Concurrency Updatability require database write locks to provide access to the underlying database. Because you cannot have multiple write locks concurrently Updatability in a result set is associated with concurrency in database access. Concurrency is a process wherein two events take place in parallel Two types: CONCURRENCY.READ_ONLY Result set cannot be modified CONCURRENCY.UPDATABLE Update, insert and delete can performed

Batch Update can be defined as a set of multiple update statements that is submitted to the database for processing as a batch Statement, PreparedStatement, CallableStatement obj can be used to submit batch updates

Batch Update Benefits: allow to request records, bring them to the client, make changes to the records on the client side, and then send the updated record back to the data source at some other time. improve performance no need to maintain a constant connection to the database

Batch Update using “Statement ” interface Disable the auto-commit mode Create a Statement instance Add SQL commands to the batch Execute the batch commands Commit the changes in the database Remove the commands from the batch

Batch Update using “Statement ” interface

Batch Update using “Statement ” interface

Batch Update using “Statement ” interface

Batch Update using “PreparedStatement” interface

Batch Update using “CallableStatement” interface

Transactions is a set of one or more statements that are executed together as a unit Ensures either all the statements in the set or none of them are executed bank funds tranfer withdraw from one : completed deposit to another: not => Fail

Properties of Transactions Atomicity all or none performed Consistency cannot break the rules such as integrity constraints Isolation no conflicts between concurrent transaction Durability recover committed transaction if needed

Implementing Transaction using JDBC

Implementing Transaction using JDBC

Implementing Transaction using JDBC