8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 1/39
ACEP/ Sun Java 2/ Session 16/ 1 of 39
ava a a ase
Connectivity
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 2/39
ACEP/ Sun Java 2/ Session 16/ 2 of 39
Session Objectives JDBC API JDBC Drivers JDBC Products
JDBC Design Considerations Two-Tier Client-Server Model Three-Tier Client-Server Model Basic Steps to JDBC
Setting up a connection to thedatabase
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 3/39
ACEP/ Sun Java 2/ Session 16/ 3 of 39
Session Objectives
(Contd«) Creating and Executing SQL Statements ResultSet & ResultSetMetaData Object
Database Security
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 4/39
ACEP/ Sun Java 2/ Session 16/ 4 of 39
JDBC API JDBC API stands for Java Database
Connectivity Application Programming
Interface. Defines how an application opens aconnection, communicates with thedatabase, execute SQL statementsand retrieves the results.
Developed by JavaSoft. Both ODBC and JDBC are based on
call level interface.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 5/39
ACEP/ Sun Java 2/ Session 16/ 5 of 39
JDBC API (Contd«) JDBC is a port of ODBC to Java programming
language. The major difference being ODBC is written in ³C´. The pointers are made use of extensively. But Java
does not pointers. JDBC was designed to be compact and focussed
only on the execution of SQL statements and thereby
retrieving the results, but ODBC has multiplemechanisms for performing a single task and hasadditional data handling capabilities.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 6/39
ACEP/ Sun Java 2/ Session 16/ 6 of 39
Functioning of JDBC API
GUI
JDBC API
Network
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 7/39
ACEP/ Sun Java 2/ Session 16/ 7 of 39
JDBC Driver Ensures that the application interacts
with the databases.
Ensures that the requests, made bythe application, are presented to thedatabase in a language, understoodby the database.
Receives the requests from the client,
converts it into the format familiar tothe database and then presents it to
the database.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 8/39
ACEP/ Sun Java 2/ Session 16/ 8 of 39
JDBC Products There are three components to the JDBC:
o java.sql package
This consists of the following interfaces: Callable Interface Connection DatabaseMetaData Driver PreparedStatement
ResultSet ResultSetMetaData Statement
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 9/39
ACEP/ Sun Java 2/ Session 16/ 9 of 39
java.sql. Package
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 10/39
ACEP/ Sun Java 2/ Session 16/ 10 of 39
JDBC Products
(Contd«)o The classes included in this package
are: Date DriverManager DriverPropertyInfo Time TimeStamp Type
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 11/39
ACEP/ Sun Java 2/ Session 16/ 11 of 39
JDBC Products
(Contd«) The exceptions included in this
package are: DataTruncation SQLException SQLWarning
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 12/39
ACEP/ Sun Java 2/ Session 16/ 12 of 39
JDBC Products
(Contd«) Test Suite
o Used to test the functionality of a JDBCDriver.
o Ensures that all the classes and methodsdefined in the JDBC API are implemented.
o Once the driver passes through all thetests, the test suite can be designated asJDBC COMPLAINT.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 13/39
ACEP/ Sun Java 2/ Session 16/ 13 of 39
JDBC Products (Contd«) JDBC-ODBC bridge
o Is a JDBC driver designed to allow Javaapplications communicate with the
database using ODBC driver.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 14/39
ACEP/ Sun Java 2/ Session 16/ 14 of 39
JDBC Design
Considerations The four types of drivers are:
o JDBC-ODBC Bridge Once the request is passed by the
JDBC to the ODBC driver, it is theresponsibility of the ODBC driver tocommunicate it with the database.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 15/39
ACEP/ Sun Java 2/ Session 16/ 15 of 39
JDBC Design Considerations
(Contd«) Native API Java
o The Native-API-Partly-Java Driver makesuse of local native libraries to
communicate with the database.o It is done by making calls to the local
installed native call level interface.o The Call Level Interface [CLI] libraries are
written in C.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 16/39
ACEP/ Sun Java 2/ Session 16/ 16 of 39
JDBC Design Considerations
(Contd«) JDBC Network
o The native CLI libraries are placed on theremote server and the driver uses a
network protocol to communicate betweenthe application and the driver.
o The driver is split into two parts: onecontaining all Java portion that can bedownloaded to the client and the server
portion containing both Java and nativemethods.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 17/39
ACEP/ Sun Java 2/ Session 16/ 17 of 39
JDBC Design Considerations
(Contd«) Native Protocol
o These drivers are 100% Java and use noCLI libraries.
o This driver is capable of communicatingdirectly with the database without anyneed of translation.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 18/39
ACEP/ Sun Java 2/ Session 16/ 18 of 39
Two-Tier Client-Server
Model The architecture of any client server
environment is by default a two-tier
system. The client is the first tier and theserver the second tier.
In a two-tier JDBC environment, thedatabase application is the client and
the DBMS is the server. The client communicates directly with
the server without any server.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 19/39
ACEP/ Sun Java 2/ Session 16/ 19 of 39
Two-Tier Client-Server Model [
Contd« ] Advantage
It is the leastcomplicated systemto implement.
This architecturemaintains a constantconnection betweenthe client and thedatabase.
This system isusually faster than athree-tier implementation
Disadvantages Most of the drivers
require that nativelibraries be loaded on theclient machine.
Local configuration has tobe maintained for nativecode.
Applets can open up
connection to the server from which they aredownloaded
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 20/39
ACEP/ Sun Java 2/ Session 16/ 20 of 39
Three-Tier Client-Server
Model A third server is employed to handlerequests from the client and then passthem to the database server.
This third server acts as a proxy for allclient requests.
All the client requests are routed throughthe proxy server thereby creating a moresecure environment for the database.
The driver translates the request into anetwork protocol and then requests viathe proxy server.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 21/39
ACEP/ Sun Java 2/ Session 16/ 21 of 39
BasicS
teps to JDBC There are about seven steps to usingJDBC access a database. The sevensteps are as mentioned below:o Import java.sql packageo Loading and registering the driver o Establishing a connection to the
database server o Creating a statemento Executing the statemento Retrieving the resultso Closing the statement and connection
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 22/39
ACEP/ Sun Java 2/ Session 16/ 22 of 39
Setting up a connection to the
database For using JDBC, we require a databaseserver and database driver.
JDBC consists of the following classes:o DriverManager Class
java.sql.DriverManager class provides withmethods to load drivers.
This class is basically used for managing JDBCdrivers installed in the system.
This class contains the following methods: getDrivers( ) getConnection( ) getLoginTimeout( ) & setLoginTimeout( ) getLogStream & setLogStream
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 23/39
ACEP/ Sun Java 2/ Session 16/ 23 of 39
Creating and Executing SQL
Statement Queries
o Queries are one of the most importantforms of SQL statements.
o In JDBC, all queries return results in theform of ResultSet objects.
o The most efficient way to execute a queryis to use the Statement executeQuery( )method.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 24/39
ACEP/ Sun Java 2/ Session 16/ 24 of 39
Creating and Executing SQL Statement [
Contd« ]Example In SQL
SELECT emp_no, emp_name, emp_age FROMemployee;
o The above query returns the employeenumber, name and age from the employeedatabase.
In JDBC
String stg = ³SELECT emp_no, emp_name,emp_age FROM employee´;
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 25/39
ACEP/ Sun Java 2/ Session 16/ 25 of 39
Creating and Executing SQL
StatementThe Java code for the above would look likethis:
String stg = ³SELECT emp_no, emp_name,emp_age FROM employee´;Statement st = conn.createStatement( );ResultSet rst = st.executeQuery(stg);
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 26/39
ACEP/ Sun Java 2/ Session 16/ 26 of 39
Creating and Executing SQL
Statement Updates
o Update statements do not return theresults as ResultSets instead return an
integer representing the number of rowsaffected by that statement.
o For updates, the Statement object andexecuteUpdate( ) methods are used.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 27/39
ACEP/ Sun Java 2/ Session 16/ 27 of 39
Creating and Executing SQL Statement [
Contd « ] Suppose we want to add some value in the inventorydatabase where price is equal to 200.
In SQLINSERT INTO inventory VALUES(500) WHERE price
= 200; In JDBC is:
String stg = ³INSERT INTO inventory´ +³VALUES(500)´ + ³WHERE price = 200´;Statement st = conn.createStatement( );
int rowcount = st.executeUpdate(stg);
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 28/39
ACEP/ Sun Java 2/ Session 16/ 28 of 39
Creating and Executing SQL Statement
(Contd«)
Data Definition Language [DDL]o Data Definition Language statements
are used to create tables, add columnsto the existing tables, delete tables, etc.
o DDL statements do not perform anyquery and hence do not return anyResultSet. DDL statements alwaysreturn a value of 0.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 29/39
ACEP/ Sun Java 2/ Session 16/ 29 of 39
Creating and Executing SQL Statement
(Contd«) The following code creates a new
table called ³employee´ In SQL:
CREATE TABLE test (emp_nameVARCHAR(25),emp_no int,emp_age int);
In JDBC:String stg = (³CREATE TABLE employee´ +³(emp_name VARCHAR(25), ³emp_no init´,
³emp_age int´);Statement st = conn.createStatement( );st.execute(stg);
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 30/39
ACEP/ Sun Java 2/ Session 16/ 30 of 39
Creating and Executing SQL Statement(Contd«)
LIKE Operator o The LIKE operator is used with the SQL statements,
to compare two strings.o The % and _ are used as pattern matching
characters
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 31/39
ACEP/ Sun Java 2/ Session 16/ 31 of 39
Creating and Executing SQL Statement
(Contd«) In SQL
SELECT emp_name FROM employee WHEREemp_no LIKE µE00%¶ In JDBC
String stg = ³SELECT emp_name FROMemployee´ + ³WHERE emp_no LIKE µE00%¶´; Other than the above-mentioned tasks, SQL
is used to ALTER TABLE, DROP TABLE,INSERT values, DELETE rows from the table.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 32/39
ACEP/ Sun Java 2/ Session 16/ 32 of 39
Creating and Executing SQL Statement(Contd«)
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 33/39
ACEP/ Sun Java 2/ Session 16/ 33 of 39
Creating and Executing SQL Statement
(Contd«)
Time and Date LiteralsThe code given below depicts the JDBC code that is
used to select all the employees employee id in theemployee table working from 1st December 1989. String stg = ³SELECT emp_no FROM employee´ + ³WHERE
doj = { d µ 1989 ± 12 ± 30¶ }´);
Statement st = conn.createStatement( );
st.execute(stg);
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 34/39
ACEP/ Sun Java 2/ Session 16/ 34 of 39
Creating and Executing SQL Statement
(Contd«)
Outer Joinso Ex ample
Suppose we want to know the list of alloutstanding orders and want to include the user name, the code in JDBC is as shown below:
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 35/39
ACEP/ Sun Java 2/ Session 16/ 35 of 39
Creating and Executing SQL Statement
(Contd«)o S ELECT users.usr_name,
users.usr_id, orders.ord_no FROM users, orders WHERE users.usr_id =
orders.usr_id;o In the above code, we find the field
usr_id is common between users andorders table. It is used commonly toestablish the relationship betweenboth.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 36/39
ACEP/ Sun Java 2/ Session 16/ 36 of 39
ResultSet & ResultSetMetaDataObject
How is the result of the query returned after execution of database query?o It is returned as tables of data according to
rows and columns.
How is this data accessed?o It is accessed using ResultSet interface.
The getMetaData( ) method is one suchthat returns the ResultSetMetaData object
containing the information about the row.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 37/39
ACEP/ Sun Java 2/ Session 16/ 37 of 39
ResultSet & ResultSetMetaDataObject
ResultSetMetaData objecto An object that provides constants and methods used
to obtain information about theR
esultSet object. The ResultSet object uses the getColumnCount( )method to return the number of columns in thetabular data accessed by the ResultSet.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 38/39
ACEP/ Sun Java 2/ Session 16/ 38 of 39
ResultSet & ResultSetMetaDataObject
The column name is returned using thegetColumnName( ) method.
The column¶s SQL type is returnedusing the getColumnType( ) method.
8/3/2019 CH 16 JDBC
http://slidepdf.com/reader/full/ch-16-jdbc 39/39
ACEP/ Sun Java 2/ Session 16/ 39 of 39
Database
Security Database security is of vital importance. Data should be available whenever required.
JDBC depends on the database server for providing security. The JDBC makes use of Secure Socket Layer
(SSL) in their product lines.
This provides encrypted communicationbetween database driver and server.
Top Related