You are on page 1of 81

JDBC

JDBC
Is an API spec. whose implementation comes in the form of jdbc drivers. JDBC API :

java.sql.* y javax.sql.*
y

JDBC DRIVER
Is a bridge s/w between java application and database s/w. Is a java class that implements java.sql.Driver interface. Why we use JDBC Driver?

JDBC ARCHITECTURE
Application
Java

JDBC

Driver

code calls JDBC library JDBC loads a driver Driver talks to a particular database Can have more than one driver -> more than one database

JDBC DRIVERS
Type I: Bridge Type II: Native Type III: Middleware Type IV: Pure

TYPE 1 DRIVER (JDBC - ODBC BRIDGE DRIVER )

Java App that uses JDBC API

Jdbc driver type1

ODBC Driver for Oracle

Vendor DB Library for Oracle

Oracle DB

ODBC Driver for MS-Access

Vendor DB Library for M S Access

MS Access

TYPE 1 DRIVER (CONT.)


Inbuilt driver of j2sdk s/w. Suitable to interact with almost all the database s/w s Driver performance is very poor. Not suitable for internet programming and applet to database communication

TYPE 2 DRIVER (NATIVE API /PARTLY JAVA DRIVER)

Java App that uses JDBC API

Jdbc driver type2

Vendor DB Library for Oracle

Oracle DB

Jdbc driver type2

Vendor DB Library for M S Access

MS Access

TYPE 2 DRIVER (CONT)


Specific to each database s/w. Significantly better performance than Type 1. Odbc drivers presence is not mandatory. Not suitable for large scale applications. Not suitable for internet / applet programs Every db requires a separate driver.

TYPE 4 DRIVER (NATIVE PROTOCOL /ALL JAVA DRIVER)

Java App that uses JDBC API

Jdbc driver type4

Oracle DB

Jdbc driver type4

MS Access

TYPE 4 DRIVER (NATIVE PROTOCOL / ALL JAVA DRIVER)


Completely developed in java. Can interact with db without having the support of odbc driver / vendor db library. Platform independent. Performance is good. Applet and internet programming is possible

TYPE 3 DRIVER ( NETWORK PROTOCOL DRIVER )


Java app manipulates DB data using con object Java App (Client App)

Gets object from conn pool using type 3

Application Server
JDBC CONNECTION POOL

Interact using type 1,2,4 drivers

DB S/W

Release connection object back to conn pool

con con

Java App (Client App)

con

con

TYPE 3 DRIVER
Is not a driver Its protocol Contains rules required to establish communication between java application and connection pool

JDBC DRIVERS (FIG.)


Type I Bridge JDBC Type II Native Type III Middleware Type IV Pure ODBC ODBC Driver

CLI (.lib)

Middleware Server

STEPS TO DEVELOP JAVA/JDBC APP


java.sql

Classes -----------Types DriverManager Date TimeStamp

Interfaces --------------Connection Statement ResultSet Driver PreparedState ment CallableStatem ent

STEPS TO DEVELOP JAVA/JDBC APP


Load the JDBC Driver class and register with DriverManager. Establish the connection with database s/w. Prepare Statement object Execute the query. Get result and process the result Close the connection.

LOADING & REGISTERING A DRIVER


statically load driver

Class.forName(sun.jdbc.odbc.JdbcO dbcDriver);

DRIVERMANAGER
All JDBC Drivers will be registered and managed by DriverManager. When a driver class is first loaded, it registers itself with the DriverManager Therefore, to register a driver, just load it!

CONNECTION
A

Connection represents a session with a specific database. Establishing connection with db s/w is nothing but creating communication channel Can have multiple connections to a database Once task with connection is completed, close the connection.

OBTAINING A CONNECTION
String url try { = "jdbc:odbc:datasource";

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection(url); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }

STATEMENT
Acts as a courier service to send queries to the db s/w. A Statement object is used for executing a static SQL statement and obtaining the results produced by it.

STATEMENT METHODS
ResultSet executeQuery(String)
y

Execute a SQL statement that returns a single ResultSet. Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed. Execute a SQL statement that may return multiple results.

int executeUpdate(String)
y

boolean execute(String)
y

What

is the difference between execute(-) and executeQuery(-)?

RESULTSET
A

ResultSet object is a java object which can store bunch of selected rows given by select query execution. Only one ResultSet per Statement can be open at once. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The 'next' method moves the cursor to the next row.

RESULTSET
Cursor BFR

ALR

RESULTSET METHODS

boolean next()
activates the next row y the first call to next() activates the first row y returns false if there are no more rows
y

void close()
disposes of the ResultSet y allows you to re-use the Statement that created it
y

RESULTSET METHODS

Type getType(int columnIndex)


returns the given field as the given type y fields indexed starting at 1 (not 0)
y

Type getType(String columnName)


same, but uses name of field y less efficient
y

RESULTSET METHODS
String getString(int columnIndex) boolean getBoolean(int columnIndex) byte getByte(int columnIndex) short getShort(int columnIndex) int getInt(int columnIndex) long getLong(int columnIndex) float getFloat(int columnIndex) double getDouble(int columnIndex) Date getDate(int columnIndex) Time getTime(int columnIndex) Timestamp getTimestamp(int columnIndex)

JDBC OBJECT CLASSES


DriverManager
y

Loads, chooses drivers connects to actual database a series of SQL statements to and from the DB a single SQL statement the records returned from a Statement

Driver
y

Connection
y

Statement
y

ResultSet
y

JDBC CLASS USAGE


DriverManager Driver Connection Statement

ResultSet

TYPES OF STATEMENT OBJECTS


Statement Object PreparedStatement Object CallableStatement Object. Operations performed by Database Engine :

Parse y Execute y Fetch


y

Send sql query

Java App

2) Parse 3) Execute 4) Fetch


DB Engine

LIMITATIONS OF STATEMENT OBJECT


DB s/w parses the same query multiple no. of times and executes, fetches the o/p . Framing query for simple Statement object using variable is quite unnecessary. Network traffic to the DB s/w is heavy since same query goes multiple no. of times to the DB s/w. To overcome these problems use precompiled queries.

PREPAREDSTAMENT OBJECT
A query that goes and resides on the DB s/w without values by becoming parsed query is a precompiled query. Precompiled queries will be parsed only once, but capable of executing multiple times with same or different values. PreparedStatement object represents this precompiled query. When to use Statement object and PreparedStatement object ?

STEPS TO WORK WITH PREPAREDSTATEMENT



Prepare the query having positional parameters.


String query=insert into item values(?,?,?,?);

Positional parameter indicates value to that query will be set afterwards. Create PreparedStatement object.

PreparedStatement ps=con.prepareStatement(query);
This method makes query as a precompiled query

Set the values for positional paremeters using setType(-,-) methods.


y

ps.setInt(-,-), ps.setString(-,-)

STEPS TO WORK WITH PREPAREDSTATEMENT


Execute the query


y

int result = ps.executeUpdate();

For more executions repeat step 3 & 4. Close PreparedStatement object


y

ps.close()

CALLABLESTATEMENT
DB s/w can maintain the business logic in the form of PL/SQL procedures and functions. To call PL/SQL procedures and functions from the java app , use CallableStatement. Procedure is a subprogram that performs specific action. A function is a subprogram that computes a value. Functions and procedures are structured alike, except that functions have a RETURN clause

CALLABLESTATEMENT

A parameter of function or procedure can be ther in three modes :


in (default) y out y inout
y

Y=X*X out in

X=X*X inout

STEPS TO WORK WITH CALLABLESTATEMENT OBJECT


Create query calling procedure


y

String query= { call procedure_name(?,?)}; CallableStatement cs=con.prepareCall(query);


Represents procedure is available on the db s/w

Create CallableStatement object


y

Register out parameters with jdbc types.


Jdbc types are bridge data types between java data types and db s/w data types. y cs.registerOutParameter(param_index,Types.INTEG ER);
y

Set values for IN parameters using setType(-,-).


y

cs.setInt(parameter_index,value);

STEPS TO WORK WITH CALLABLESTATEMENT OBJECT


Execute the procedure


y

boolean b= cs.execute();

Gather the result from OUT parameter using getType() method.


y

int result=cs.getType(column_index);

For more executions repeat steps 4 , 5, and 6. Close the CallableStatement object.
y

cs.close();

MAPPING JAVA TYPES TO SQL TYPES


SQL type Java Type CHAR, VARCHAR, LONGVARCHAR String NUMERIC, DECIMAL java.math.BigDecimal BIT boolean TINYINT byte SMALLINT short INTEGER int BIGINT long REAL float double FLOAT, DOUBLE byte[] BINARY, VARBINARY, LONGVARBINARY DATE java.sql.Date TIME java.sql.Time TIMESTAMP java.sql.Timestamp

DATABASE TIME
Java

defines three classes to help java.sql.Date


y

year, month, day hours, minutes, seconds

java.sql.Time
y

java.sql.Timestamp

year, month, day, hours, minutes, seconds, nanoseconds y usually use this one
y

METADATA WITH JDBC


Used to get information about tables, views, column names, column types, stored procedures, result sets, and databases. You can use database metadata to
y y y y y

Discover database schema information. Discover database users, tables, views, and stored procedures Understand and analyze the result sets returned by SQL queries Determine the signature of a specific stored procedure in the database. Identify the primary/foreign keys for a given table

METADATA IN JDBC

DatabaseMetaData : Gives limitations & capabilities of underlying DB s/w.


Object of a class that implements java.sql.DatabaseMetaData interface. y DatabaseMetaData dbmd=conn.getMetaData(); y Invoke various methos on dbmd object to gather the details about the DB s/w pointed by Connection object.
y

ResultsetMetaData : Gives details about table that is represented by Resultset object.


Object of a class that implements java.sql.ResultsetMetaData interface. y ResultsetMetaData rsmd=resltSetOb.getMetaData();
y

METADATA IN JDBC

ParameterMetaData : Gives details about place holders kept in queries.


Object of a class that implements java.sql.ParameterMetaData interface. y PreparedStatement pst=con.prepareStatement( insert into items values(?,?,?,?)); ParameterMetaData d=pst.getParameterMetaData();
y

If any method invoked in DatabaseMetaData returns 0 or null , it means the driver is not capable of gathering certain information regarding DB s/w Most of the drivers doesnt support ParameterMetaData

JDBC 2.0
Scrollable

result set Batch updates Advanced data types


y

Blobs, objects, structured types Persistent JavaBeans

Rowsets
y

JNDI Connection

Pooling Distributed transactions via JTS

SCROLLABLE RESULTSET

ResultSet Object
Non-Scrollable ResultSet Object y Scrollable ResultSet Object
y

ResultSet object that allows to access the records in one direction is non-scrollable. ResultSet object that allows to access the records bi-directionally is called scrollable

SCROLLABLE RESULTSET
JDBC 2.0 introduces scrollable results sets whose values can be read and updated if reading and updating is supported by the underlying database. With scrollable result sets, any row can be selected at random, and the result set can be traversed forwards and backwards. One advantage to the new result set is you can update a set of matching rows without having to issue an additional executeUpdate call.

SCROLLABLE RESULTSET

Both Statements and PreparedStatements have an additional constructor that accepts a scroll type and an update type parameter The scroll type value can be one of the following values:
ResultSet.TYPE_FORWARD_ONLY Default behavior in JDBC 1.0, application can only call next() on the result set. y ResultSet.SCROLL_SENSITIVE ResultSet is fully navigable and updates are reflected in the result set as they occur. y ResultSet.SCROLL_INSENSITIVE Result set is fully navigable, but updates are only visible after the result set is closed. You need to create a new result set to see the results.
y

SCROLLABLE RESULTSET

The update type parameter can be one of the following two values:
ResultSet.CONCUR_READ_ONLY The result set is read only. y ResultSet.CONCUR_UPDATABLE The result set can be updated.
y

You can verify that your database supports these types by calling :
y

con.getMetaData().supportsResultSetConcurrency() method

NAVIGATING THE RESULTSET


The fully scrollable result set returns a cursor which can be moved using simple commands. By default the result set cursor points to the row before the first row of the result set. A call to next() retrieves the first result set row
y y y y y

beforeFirst(): Default position. Puts cursor before the first row of the result set. first(): Puts cursor on the first row of the result set. last(): Puts cursor before the last row of the result set. afterLast() Puts cursor beyond last row of the result set. Calls to previous moves backwards through the ResultSet. absolute(pos): Puts cursor at the row number position where absolute(1) is the first row and absolute(-1) is the last row. relative(pos): Puts cursor at a row relative to its current position where relative(1) moves row cursor one row forward.

UPDATABLE RESULTSET OBJECT


If modifications done in the ResultSet object is reflecting in the DB table, then ResultSet object is called as Updatable ResultSet object. We can perform insert, update, delete operations on the table without SQL statements. You can update a value in a result set by calling the ResultSet.update<type> method on the row where the cursor is positioned The update applies only to the result set until the call to rs.updateRow(), which updates the underlying database Closing the result set before calling updateRow will lose any edits applied to the result set

UPDATABLE RESULTSET OBJECT


Inserting a new row uses the same update<type> methods rs.moveToInsertRow is called before and rs.insertRow() is called after the fields have been initialized You can delete the current row with a call to rs.deleteRow() Is not suitable for bulk delete and bulk update operations. Only ScrollableResultSet can become UpdatableResultSet.

BATCH PROCESSING
Is a process of combining related queries into single unit, sending them to DB s/w as batch and getting their result as a batch. Instead of sending multiple queries to DB s/w for multiple times, send queries as a batch. Reduces network round trips between java application and DB s/w. Helps the programmer to work with Transactions.

BATCH PROCESSING
The calls to stmt.addBatch(-) append statements to the original Statement. The call to executeBatch() submits the entire statement with all the appends to the database. The return result of the addBatch method is an array of row counts affected for each statement executed in the batch job. If a problem occurred, a java.sql.BatchUpdateException is thrown. Once select query is executed , it generates ResultSet object. Since ResultSet object cannot be stored in integer array, batch processing cannot include select queries.

TRANSACTION MANAGEMENT
Is all about combining set of related operations into a single unit and executing them by applying do everything or nothing principle Transactions are not explicitly opened and closed Instead, the connection has a state called AutoCommit mode if AutoCommit is true, then every statement is automatically committed default case: true

SETAUTOCOMMIT Connection.setAutoCommit(boolean) if AutoCommit is false, then every statement is added to an ongoing transaction you must explicitly commit or rollback the transaction using Connection.commit() and Connection.rollback()

ROWSETS
We can send only those java objects over the network which are serializable. ResultSet object is not a serializable object, so we cannot send that object over network. To overcome this problem jdbc has given support for RowSet object which are extension to ResultSet object and can be sent over network. All JDBC drivers doesnt support RowSets.

PERFORMANCE TUNING FOR JDBC API

WHY OPTIMIZE?
On average, a web request performs 4 database queries. Experience has shown that database calls are typical performance bottleneck. Bad JDBC can overwhelm the database.

JDBC API
SQL: SELECT * FROM TABLE java.sql.PreparedStatement java.sql.CallableStatement Cache data on client.

MOST VERSATILE

MOST OPTIMIZATION

JDBC API

SQL STATEMENTS
Most flexible y Least reliable y Must be recompiled in database for each use
y

PREPARED STATEMENT
Represents a precompiled SQL statement y Can be used to efficiently execute statement multiple times y Somewhat flexible can create new ones as needed
y

JDBC API

CALLABLE STATEMENT
Used to execute SQL stored procedures. y Same syntax as PreparedStatement. y Least flexible. y Most optimized DB call.
y

PreparedStatement gives better performance when compared to Statement because it is pre-parsed and pre-compiled by the database once for the first time and then onwards it reuses the parsed and compiled statement. CallableStatement gives better performance when compared to PreparedStatement and Statement when there is a requirement for single request to process multiple complex statements

JDBC API

CACHE
y y y

Keep data within client to reduce the number of roundtrips to the database. Every database schema generally has read-only and readmostly tables. These tables are called as lookup tables. Read-only tables contain static data that never changes in its life time. Read-mostly tables contain semi dynamic data that changes often If an application reads data from these tables for every client request, then it is redundant, unnecessary and expensive. The solution for this problem is to cache the read-only table data by reading the data from that table once and caching the read-mostly table data by reading and refreshing with time limit

BASIC DESIGN TECHNIQUES


Use Database Connection Pool


Dont useDriverManager.getConnection() often. JDBC connections can take 0.5 to 2 seconds to create. y Create Pool of Connections and reuse them. y Use multi-threading with Connection Pooling to address network latency y Threads can issue queries over separate database connections.
y

OPTIMIZATION WITH RESULTSET OBJECT


Set up proper direction for processing the rows y Use proper get methods y Close ResultSet when finished
y

BASIC DESIGN TECHNIQUES


Single-batch Transactions
Collect set of operations and submit transaction in one statement y BEGIN TRANSACTION UPDATE TABLE1...
y

INSERT INTO TABLE2

DELETE TABLE3 COMMIT y DB obtains necessary locks on rows and tables, uses and releases them in one step

BASIC DESIGN TECHNIQUES


Smart Queries
Make queries as specific as possible y Put more logic into SQL statements y DB are designed to use SQL efficiently y Proper use of SQL can avoid performance problems
y

Smart Query Ex: get employees in ENG dept


y

Instead of: SELECT * FROM employees;

SELECT * FROM dept; (and joining on Java application side)


y

Use database join: SELECT employees.* FROM employees E, dept D WHERE E.DEPTNO = D.DEPTNO AND D.DEPTTYPE = ENG;

BASIC DESIGN TECHNIQUES


Smart Query Guidelines


Use DB for filtering y Use Java for business logic y DB does filtering very well y DB business logic is poor
y

CONNECTION POOLING

There are two types of jdbc connection objects :


Direct Connection Object y Pooled Connection Object
y

The JDBC Connection object that is created by the programmer manually is called direct connection object. The JDBC Connection object that is collected from JDBC Connection pool is called pooled connection object. JDBC Connection pool is a factory that contains set of readily available JDBC connection objects.

CONNECTION POOLING
Using connection pools helps to both alleviate connection management overhead and decrease development tasks for data access. Each time an application attempts to access a backend store (such as a database), it requires resources to create, maintain, and release a connection to that datastore. To mitigate the strain this process can place on overall application resources, the Application Server enables administrators to establish a pool of backend connections that applications can share on an application server

ADVANTAGES OF CONNECTION POOLING


Connection pooling can improve the response time of any application that requires connections, especially Web-based applications. With connection pooling, most user requests do not incur the overhead of creating a new connection because the data source can locate and use an existing connection from the pool of connections Re usability, we can reuse the existing connection already existing It also simplifies the design in terms of managing the connections; we can delegate the connection creation, max number of connections for an application, max idle time for a connection etc. using the some kind of a connection pool manager

C3P0 CONNECTION POOLING


c3p0 is an easy-to-use library for making traditional JDBC drivers "enterprise-ready" by augmenting them with functionality defined by the jdbc3 spec and the optional extensions to jdbc2 c3p0 provides several useful services:

Classes which adapt traditional DriverManagerbased JDBC drivers to the new javax.sql.DataSource scheme for acquiring database Connections. y Transparent pooling of Connection and PreparedStatements behind DataSources which can "wrap" around traditional drivers or arbitrary unpooled DataSources.
y

WORKING WITH C3P0 CONNECTION POOL


Put the file lib/c3p0-0.9.1.2.jar somewhere in your CLASSPATH There are three ways of acquiring c3p0 poolbacked DataSources:

directly instantiate and configure a ComboPooledDataSource bean y use the DataSources factory class y "build your own" pool-backed DataSource by directly instantiating PoolBackedDataSource and setting its ConectionPoolDataSource
y

INSTANTIATING AND CONFIGURING A COMBOPOOLEDDATASOURCE the most straightforward way to create a c3p0 pooling DataSource is to instantiate an instance of com.mchange.v2.c3p0.ComboPooledDataSource This is a JavaBean-style class with a public, noarg constructor Before you use the DataSource, you'll have to be sure to set at least the property jdbcUrl You may also want to set user and password, and if you have not externally preloaded the old-style JDBC driver you'll use you should set the driverClass.

USING THE DATASOURCES FACTORY CLASS


use the static factory class com.mchange.v2.c3p0.DataSources Can be used to build unpooled DataSources from traditional JDBC drivers Can be used to build pooled DataSources from unpooled DataSources If you use the DataSources factory class, and you want to programmatically override default configuration parameters, you can supply a map of override properties

JAVA NAMING AND DIRECTORY INTERFACE (JNDI)


Java application uses JNDI API to interact with Naming Registry s/w. Every JDBC Connection pool will be represented by Data Source object. To make this data source object publically visible, we register data source object in a special place called naming registry having nick name (jndi name). In order to access a JDBC Connection object of a Connection pool, we need to access the data source object pointing to that connection pool.

JAVA NAMING AND DIRECTORY INTERFACE (JNDI)


Java application needs to perform look up or search operation on naming registry to get the data source object. Use getConnection () method on the data source object to get the connection object from the connection pool.

An open source DataBase Management System


Community Editions free y Enterprise Editions free license but a subscription and maintenance fee
y

Suitable situations range from small enterprise to global, enterprise-wide systems Multiple platforms

MYSQL TOOLS

The core system includes only a command-line tool


y

mysqladmin

GUI tools available separately from MySQL are


MySQL Administrator (a DBA tool) and y MySQL Query Browser
y

SQLyog

is a GUI front-end for MySQL (there are others) - Community and Enterprise Editions Allows Data Definition and Data Manipulation After loading it and before using it you need to Connect to the right MySQL instance A tutorial, with a range of activities to get familiar with it, is available at http://www.databasejournal.com/features/my sql/article.php/1558731

SQLYOG SCREEN

REFERENCES

For further information see


www.mysql.com y www.webyog.com/en/ includes forums and FAQs
y

You might also like