IADCS Diploma Course Java Database Connectivity U Nyein Oo U Nyein Oo COO/Director (IT) COO/Director (IT) Myanma Computer Co., Ltd Myanma Computer Co., Ltd
IADCS Diploma CourseJava Database Connectivity
U Nyein OoU Nyein OoCOO/Director (IT)COO/Director (IT)Myanma Computer Co., LtdMyanma Computer Co., Ltd
Copyrigth: MCC Java Database Connectivity 2
JDBC API
JDBC API stands for Java Database Connectivity Application Programming Interface
It is a set of specifications that defines how a Java program can communicate with the database
It defines how an application opens a connection, communicates with the database, executes SQL statements and retrieves the results
Many of the JDBC API concepts are taken from other sources, particularly Microsoft’s ODBC (Open Database Connectivity)
Copyrigth: MCC Java Database Connectivity 3
JDBC API (Contd…) Figure below depicts the functioning of the JDBC API
Copyrigth: MCC Java Database Connectivity 4
JDBC Drivers
It ensures that the application interacts with all databases in a standard and uniform manner
It ensures that the requests made by the application are presented to the database in a language understood by the database
It receives the requests from the client, converts it into the format understandable by the database and then presents it to the database
It receives the response, translates it back to Java data format and presents it to the client application
All databases follow SQL and hence there is only one JDBC Driver, that is, the Java-to-SQL translator
Copyrigth: MCC Java Database Connectivity 5
JDBC Drivers (Contd…) Figure below depicts the working of JDBC Driver
Copyrigth: MCC Java Database Connectivity 6
JDBC Products
Three components of JDBC: – java.sql package– Test Suite– JDBC-ODBC bridge
Copyrigth: MCC Java Database Connectivity 7
java.sql package
It contains a set of interfaces and classes defined by JDBC API that are used for communicating with the database
Interfaces of java.sql package:
CallableStatement Connection DatabaseMetaData Driver
PreparedStatement ResultSet ResultSetMetaData Statement
Copyrigth: MCC Java Database Connectivity 8
java.sql package (Contd…)
Exceptions defined by java.sql package:– DataTruncation– SQLException– SQLWarning
Copyrigth: MCC Java Database Connectivity 9
JDBC Driver Test Suite
It tests the functionality of a JDBC Driver It ensures that all classes and methods defined in
the JDBC API are implemented Once the driver passes through all the tests, the test
suite can be designated as JDBC COMPLAINT
Copyrigth: MCC Java Database Connectivity 10
JDBC-ODBC Bridge
It is a JDBC driver designed to allow Java applications to communicate with the database using ODBC driver
It allows developers to begin writing JDBC applications without having to wait for a native driver for their database
It is a part of the JDBC package
Copyrigth: MCC Java Database Connectivity 11
JDBC Products (Contd…)
To work with JDBC API the following are required:– Java Development ToolKit (JDK) – SQL complaint database– JDBC driver for database
Copyrigth: MCC Java Database Connectivity 12
JDBC Design Considerations
JDBC driver fits into the architecture of various client/server models
Four types of JDBC drivers:– JDBC-ODBC Bridge – Native API Java – JDBC Network – Native Protocol
Copyrigth: MCC Java Database Connectivity 13
JDBC-ODBC Bridge
This driver is supplied by JavaSoft. It is the only driver that can be used with multiple
databases. The ODBC interface remains constant no matter
which database is used. Once the request is passed by the JDBC to the ODBC driver, it is the responsibility of the ODBC driver to communicate it with the database.
An disadvantage of JDBC-ODBC bridge driver is that it adds one more layer of complexity to the program and can make software bugs more difficult to solve.
Copyrigth: MCC Java Database Connectivity 14
JDBC-ODBC Bridge (Contd...)
Figure below depicts how JDBC-ODBC bridge driver is implemented
Copyrigth: MCC Java Database Connectivity 15
Native-API-Partly-Java Driver
It makes use of local native libraries to communicate with the database
It does this by calling to the local installed native call level interface (CLI)
The CLI libraries are actually responsible for the communication with the database server
When a client makes a request, the driver translates the JDBC request to the native method call and then passes the request to the native CLI
Copyrigth: MCC Java Database Connectivity 16
Native-API-Partly-Java Driver (Contd...)
Figure below depicts how native driver is implemented
Copyrigth: MCC Java Database Connectivity 17
JDBC-Net-All-Java Driver
The only difference between the previous two drivers is the placement of the native database access libraries
The native CLI libraries are placed on the remote server and the driver uses a network protocol to communicate between the application and the driver
The driver is split into two parts: one containing all Java portion that can be downloaded to the client and the server portion containing both Java and native methods
Copyrigth: MCC Java Database Connectivity 18
Native-Protocol-All-Java Driver
These drivers are 100% Java and use no CLI libraries
It is capable of communicating directly with the database without any need of translation
Copyrigth: MCC Java Database Connectivity 19
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 the server the second tier
In a two-tier JDBC environment, the database application is the client and the DBMS is the server
The client communicates directly with the server
Copyrigth: MCC Java Database Connectivity 20
Two-Tier Client Server Model (Contd...)
Figure below depicts a two-tier client-server model
Copyrigth: MCC Java Database Connectivity 21
Advantages of using a two-tier database system: – It is the least complicated system to implement – This architecture maintains a constant connection between
the client and the database – This system is usually faster than a three-tier
implementation Disadvantages of using this system:
– Most of the drivers require that native libraries be loaded on the client machine
– Local configuration has to be maintained for native code – Applets can open up connection to the server from which
they are downloaded
Two-Tier Client Server Model (Contd...)
Copyrigth: MCC Java Database Connectivity 22
Three-Tier Client Server Model
In this system, a third server is employed to handle requests from the client and then pass them to the database server
This third server acts as a proxy for all client requests This model has the advantage of allowing separation
of the database server from the web server In such an environment, the driver translates the
request into a network protocol and then requests via the proxy server
Copyrigth: MCC Java Database Connectivity 23
Three-Tier Client Server Model(Contd...)
Figure below depicts a three-tier client-server environment
Copyrigth: MCC Java Database Connectivity 24
Basic Steps to JDBC
Seven steps in using JDBC to access a database: – Importing java.sql package – Loading and registering the driver – Establishing a connection to the database server – Creating a statement – Executing the statement – Retrieving the results – Closing the statement and connection
Copyrigth: MCC Java Database Connectivity 25
Basic Steps to JDBC ( contd.. )
Figure below depicts the steps
Copyrigth: MCC Java Database Connectivity 26
Setting up a Connection to the Database
java.sql package provides database programming capabilities to Java
JDBC API is a programming interface for application developers doing development through database
Another major component of JDBC is the JDBC Driver API
A database server and database driver are required for using JDBC
Copyrigth: MCC Java Database Connectivity 27
Setting up a Connection to the Database (Contd...)
java.sql.DriverManager class provides methods to load drivers. It consists of the following methods:
– getDrivers( ) – getConnection( ) – registerDriver( ) – deregisterDriver( ) – getLoginTimeout( ) – setLoginTimeout( ) – getLogStream( ) – setLogStream( )
Copyrigth: MCC Java Database Connectivity 28
Creating and Executing SQL Statements
An SQL statement is at the center of any JDBC The SQL statement and the JDBC representation are
the same The JDBC string needs to be modified to ensure that
the database receives the intended SQL statement Any string that is identical to the SQL statement is
referred to as simple SQL Statement and those requiring some modifications are considered complex
Queries are one of the most important forms of SQL statements
Copyrigth: MCC Java Database Connectivity 29
Creating and Executing SQL Statements (Contd…)
In JDBC, all queries return results in the form of ResultSet objects
The most efficient way to execute a query is to use the Statement.executeQuery( ) method
Time and Date Literals handling is also possible in JDBC
Outer joins are also supported by JDBC
Copyrigth: MCC Java Database Connectivity 30
ResultSet and ResultSetMetaData Objects
The result of the query is returned in the form of rows and columns
The ResultSet interface is used to access this data The query results are returned as ResultSet objects
that in turn provide access to the tabular data, one row at a time
ResultSetMetaData interface provides constants and methods used to obtain information about the ResultSet object
Copyrigth: MCC Java Database Connectivity 31
Stored Procedures
A stored procedure is a group of SQL statements that form a logical unit and perform a particular task
They are used to encapsulate a set of operations or queries to execute on a database server
They can be compiled and executed with different parameters and results
They are supported by most DBMSs, but there is a fair amount of variation in their syntax and capabilities
Copyrigth: MCC Java Database Connectivity 32
Stored Procedures (Contd…)
Syntax for creating a procedure
create procedure <proc_name>
as
select <column_name/s>
from <table name/s>
where <query>
Copyrigth: MCC Java Database Connectivity 33
Calling Stored Procedures
Syntax for calling a stored procedure
CallableStatement cs = con.prepareCall("{call <Proc_name>}");
ResultSet rs = cs.executeQuery();
Note that the method used to execute ‘cs’ is executeQuery( ) because ‘cs’ calls a stored procedure that contains one query and thus produces one result set
If the procedure had contained one update or one DDL statement, the method executeUpdate( ) would have been the one to use
Copyrigth: MCC Java Database Connectivity 34
Database Security
Database security is of vital importance. The data contains sensitive and confidential
information about the company and hence vital care has to be taken to see that no unauthorized users access it and thereby tamper with the data.
Data availability is also of utmost importance. It should be available whenever required. JDBC
depends on the database server for providing security.
Copyrigth: MCC Java Database Connectivity 35
Database Security (Contd…)
The JDBC makes use of Secure Socket Layer (SSL) in their product lines that provides encrypted communication between database driver and server