Download JDBC Contents

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Serializability wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft Access wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

Database wikipedia , lookup

Concurrency control wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Relational model wikipedia , lookup

PL/SQL wikipedia , lookup

Clusterpoint wikipedia , lookup

Versant Object Database wikipedia , lookup

Database model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Transcript
Introduction

Database is organized collection of data

Database Management System provides mechanisms for storing, organizing, retrieving and modifying data.

Relational DBMS is popular one

SQL is a language used to create, manipulate, examine, and manage relational databases

SQL was standardized in 1992 so that a program could communicate with most database systems without
having to change the SQL commands

Examples of RDBMS: MS SQL SERVER, ORACLE, MYSQL, MS-ACCESS etc
APPLICATION
Request Query
Database
Respond To Query
FIG: Database Connectivity Architecture
Basic SQL Statements

INSERT Statement

SELECT Statement

UPDATE Statement

DELETE Statement

CREATE Statement

ALTER Statement
ODBC API (Open Database Connectivity API)

Though SQL was standardized, connection with the database could not be standardized as each database
vendor has a different interface as well as different extensions of SQL.

ODBC, a C-based interface to SQL-based database engines, provides a consistent interface for
communicating with a database and for accessing database metadata

ODBC API is a set of library routines that enables an application to access variety of databases

Individual vendors provide specific drivers or "bridges" to their particular database management system

Even if the database is upgraded to a newer version of RDBMS, all one requires is to change the ODBC
Driver
MS-ACCESS
Driver
APPLICATION
Request Query
MSACCESS
ODBC Driver
Manager
SQL Driver
MS-SQL
Database
Respond To Query
FIG: ODBC Application Structure
JDBC API (Java Database Connectivity API)

ODBC API is written in C Language and makes use of pointers

Since Java doesn’t support pointers, a Java Program cannot directly communicate with ODBC Driver
Manager

For the integration of Java Applications with ODBC API, Java provided JDBC API
APPLICATION
Request Query
JDBC Driver
Manager
JDBC-ODBC
Bridge Driver
MSACCESS
Driver
MSACCESS
SQL Driver
MS-SQL
Database
Respond To Query
FIG: JDBC Application Architecture
JDBC Drivers

Once your database engine is installed and your database is all set up, you will need a JDBC driver for that
database engine.

The more commercial database engines like Oracle have commercial JDBC drivers.

Most of them, however, allow you to have a free trial period for experimenting with the driver.

Follow the install instructions for the driver you choose, and remember that some JDBC drivers require to
you install native code on client machines.

To help you understand what different drivers require, the following driver categorization system id defined:
o Type-1 (JDBC-To-ODBC Bridge Driver)
o Type-2 (Native API, part Java Driver)
o Type-3 (JDBC-Net Pure Java Drivers)
o Type-4 (Native Protocol Pure Java Driver)

Type-1 (JDBC-To-ODBC Bridge Driver)
o These drivers use a bridging technology to access a database.
o The JDBC-ODBC Bridge that comes with the JDK 1.1 is a good example of this kind of driver.
o It provides a gateway to the ODBC API.
o Implementations of that API in turn do the actual database access.
o Bridge solutions generally require software to be installed on client systems, meaning that they are
not good solutions for applications that do not allow you to install software on the client.

Type-2 (Native API, part Java Driver)
o The type 2 drivers are native API drivers.
o This means that the driver contains Java code that calls native C or C++ methods provided by the
individual database vendors that perform the database access.
o Again, this solution requires software on the client system.

Type-3 (JDBC-Net Pure Java Drivers)
o Type 3 drivers provide a client with a generic network API that is then translated into database
specific access at the server level.
o In other words, the JDBC driver on the client uses sockets to call a middleware application on the
server that translates the client requests into an API specific to the desired driver.
o As it turns out, this kind of driver is extremely flexible since it requires no code installed on the
client and a single driver can actually provide access to multiple databases.

Type-4 (Native Protocol Pure Java Driver)
o Using network protocols built into the database engine, type 4 drivers talk directly to the database
using Java sockets.
o This is the most direct pure Java solution.
o In nearly every case, this type of driver will come only from the database vendor.
JDBC Driver Manager

Backbone of JDBC Architecture

Main function is to connect Java Application to the appropriate driver specified
Connecting To Database

Package used to create JDBC Connection and JDBC Application is java.sql.*

This package contains classes that helps in connecting to a database, creating SQL statements, sending the
SQL statements and processing the query result

The major steps involved in connecting to database and perform database activities are as follows:
1. Loading Driver
2. Making Connection
3. Querying the Database

Loading Driver:
o To establish a connection with database, one needs to register a JDBC driver of any category as
specified above
o Registration of JDBC driver is done by calling forName() method from ‘Class’ class
o This method is used as: Class.forName(String driver_name)
o Driver names and Database URLs for popular RDBMS are:
RDBMS
JDBC Driver Name
Database URL Format
MySQL
com.mysql.jdbc.Driver
jdbc:mysql://hostname/databasename
ORACLE
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@hostname:port:databasename
DB2
com.ibm.db2.net.DB2Driver
jdbc:db2:hostname:portno/databasename
Sybase
com.sybase.jdbc.SybDriver
jdbc:Sybase:Tds:hostname/databasename
MS-ACCESS
sun.jdbc.odbc.JdbcOdbcDriver
jdbc:odbc:databasename
o Eg :
String access_driver=” sun.jdbc.odbc.JdbcOdbcDriver”;
String mysql_driver=” com.mysql.jdbc.Driver”;
Class.forName(access_driver); // for loading access driver
// For loading mysql driver: Class.forName(mysql_driver);

Making Connection
o Connection interface object is used to represent a database connection
o Database connection is invoked by getConnection() method of DriverManager class
o This method is used as: DriverManager.getConnection(String Database_URL, <String username>,
<String password>)
o
Eg:
String mysql_driver=” com.mysql.jdbc.Driver”;
Class.forName(mysql_driver); // For loading mysql driver
String database_url=” jdbc:mysqk://hostname/test”; // test is our sample database
Connection conn=DriverManager.getConnection(database_url,”root”,””);

Querying Database
o Once the connection has been established, the actual action occurs which involves querying the
database
o Different queries are executed and the results from the queries are manipulated
o Major interface objects used in querying the database are as follows:

Statement

Resultset


Connection Class
Methods
o Statement createStatement()throws SQLException

Creates a Statement object for sending SQL statements to the database. SQL statements
without parameters are normally executed using Statement objects.

If the same SQL statement is executed many times, it may be more efficient to use a
PreparedStatement object.
o Statement createStatement(int resultSetType,int resultSetConcurrency) throws SQLException

Creates a Statement object that will generate ResultSet objects with the given type and
concurrency.

This method is the same as the createStatement method above, but it allows the default result
set type and concurrency to be overridden.

Parameters:

resultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY,
ResultSet.TYPE_SCROLL_INSENSITIVE, or
ResultSet.TYPE_SCROLL_SENSITIVE

resultSetConcurrency - a concurrency type; one of
ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
o PreparedStatement prepareStatement(String sql) throws SQLException

Creates a PreparedStatement object for sending parameterized SQL statements to the
database.
o CallableStatement prepareCall(String sql)throws SQLException

Creates a CallableStatement object for calling database stored procedures.

The CallableStatement object provides methods for setting up its IN and OUT parameters,
and methods for executing the call to a stored procedure.


Statement Class
Methods
o ResultSet executeQuery(String sql)

Executes the given SQL statement, which returns a single ResultSet object.
o int executeUpdate(String sql)

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE
statement or an SQL statement that returns nothing, such as an SQL DDL statement.

ResultSet Class

Fields:
o
static int CLOSE_CURSORS_AT_COMMIT

The constant indicating that open ResultSet objects with this holdability will be closed
when the current transaction is commited.
o
static int CONCUR_READ_ONLY

The constant indicating the concurrency mode for a ResultSet object that may NOT be
updated.
o
static int CONCUR_UPDATABLE

The constant indicating the concurrency mode for a ResultSet object that may be
updated.
o
static int FETCH_FORWARD

The constant indicating that the rows in a result set will be processed in a forward
direction; first-to-last.
o
static int FETCH_REVERSE

The constant indicating that the rows in a result set will be processed in a reverse
direction; last-to-first.
o
static int FETCH_UNKNOWN

The constant indicating that the order in which rows in a result set will be processed is
unknown.
o
static int HOLD_CURSORS_OVER_COMMIT

The constant indicating that open ResultSet objects with this holdability will remain open
when the current transaction is commited.
o
static int TYPE_FORWARD_ONLY

The constant indicating the type for a ResultSet object whose cursor may move only
forward.
o
static int TYPE_SCROLL_INSENSITIVE

The constant indicating the type for a ResultSet object that is scrollable but generally
not sensitive to changes to the data that underlies the ResultSet.
o
static int TYPE_SCROLL_SENSITIVE

he constant indicating the type for a ResultSet object that is scrollable and generally
sensitive to changes to the data that underlies the ResultSet.

Main Methods:
o
boolean absolute(int row)
Moves the cursor to the given row number in this ResultSet object.
o void afterLast()
Moves the cursor to the end of this ResultSet object, just after the last row. void
o beforeFirst()
Moves the cursor to the front of this ResultSet object, just before the first row. void
o void close()
Releases this ResultSet object's database and JDBC resources immediately instead of waiting
for this to happen when it is automatically closed.
o void deleteRow()
Deletes the current row from this ResultSet object and from the underlying database.
o int findColumn(String columnLabel)
Maps the given ResultSet column label to its ResultSet column index.
o boolean first()
Moves the cursor to the first row in this ResultSet object.
o
void insertRow()
Inserts the contents of the insert row into this ResultSet object and into the database.
o
boolean isAfterLast()
Retrieves whether the cursor is after the last row in this ResultSet object.
o boolean isBeforeFirst()
Retrieves whether the cursor is before the first row in this ResultSet object.
o boolean isClosed()
Retrieves whether this ResultSet object has been closed.
o boolean isFirst()
Retrieves whether the cursor is on the first row of this ResultSet object.
o boolean isLast()
Retrieves whether the cursor is on the last row of this ResultSet object.
o boolean last()
Moves the cursor to the last row in this ResultSet object.
o void moveToCurrentRow()
Moves the cursor to the remembered cursor position, usually the current row.
o void moveToInsertRow()
Moves the cursor to the insert row.
o boolean next()
Moves the cursor froward one row from its current position.
o boolean previous()
Moves the cursor to the previous row in this ResultSet object.

The updater methods may be used in two ways:
o to update a column value in the current row.

In a scrollable ResultSet object, the cursor can be moved backwards and forwards, to an
absolute position, or to a position relative to the current row.

The following code fragment updates the NAME column in the fifth row of the ResultSet object
rs and
then uses the method updateRow to update the data source table from which rs was
derived.
rs.absolute(5); // moves the cursor to the fifth row of rs
rs.updateString("NAME", "AINSWORTH"); // updates the
// NAME column of row 5 to be AINSWORTH
rs.updateRow(); // updates the row in the data source
o to insert column values into the insert row.

An updatable ResultSet object has a special row associated with it that serves as a staging area
for building a row to be inserted.

The following code fragment moves the cursor to the insert row, builds a three-column row,
and inserts it into rs and into the data source table using the method insertRow.
rs.moveToInsertRow(); // moves cursor to the insert row
rs.updateString(1, "AINSWORTH"); // updates the
// first column of the insert row to be AINSWORTH
rs.updateInt(2,35); // updates the second column to be 35
rs.updateBoolean(3, true); // updates the third column to true
rs.insertRow();
rs.moveToCurrentRow();