Java and Database JDBC
Contents Client-Server JDBC JDBC Drivers JDBC API 2
Client-Server The Architecture
Introduction A Client is defined as a requester of services and a Server is defined as the provider of services. The term client/server was first used in the 1980s in reference to personal computers (PCs) on a network. The client/server architecture is a versatile and modular infrastructure. 4
Cont d It is intended to improve usability, flexibility, interoperability, and scalability of an application. 5
Advantages This approach introduced a DB server to replace the file server. Using a relational database management system (DBMS), user queries could be answered directly. This architecture reduced network traffic by providing a query response rather than total file transfer. 6
Cont d It improves multi-user updating through a GUI front end to a shared database. In this architectures structured query language (SQL) statements are typically used to communicate between the client and server. 7
Architectures This architecture of Client-Server is referred to as Application Tier architecture. Few well known are Two-Tier Three-Tier N-Tier 8
Two-Tier Architecture Client Tier Data Tier GUI Client Database Storage 9
Three-Tier Architecture Client Tier Middle Tier Data Tier Web Client GUI Client Business Logic Database Storage 10
N-Tier Architecture Client Tier Middle Tier Data Tier Web Client GUI Client Business Logic Transaction Manager Message Server Database Storage 11
JDBC The API
Introduction Java Database Connectivity API API=Application Programmer s Interface Java specific PI to Databases Compare to ODBC 13
Cont d JDBC is overall API for connecting to databases Low-level JDBC Driver developers use JDBC to make JDBC drivers High-level Java developers use JDBC to call JDBC driver to access databases 14
Why JDBC??? DBMS independent interface generic SQL database access framework uniform interface to different data sources 15
JDBC Just another API It s a series of inter-connected classes & interfaces It s just Java JDBC drivers actually do all of the real work Contain routines for connecting, querying, etc for its database Explicitly or Implicitly create instance of driver 16
JDBC Drivers Types
Types of JDBC Drivers There are four basic types of drivers available for JDBC Each one has a specific use Each one has specific limitation 18
Type I This driver type is the JDBC-ODBC bridge It is limited to running locally Must have ODBC installed on computer Must have ODBC driver for specific database installed on computer Generally can t run inside an applet because of Native Method calls 19
JDBC-ODBC Bridge Driver Client Machine Java Application JDBC-ODBC Bridge Server Machine ODBC Driver Database Server Vendor DB Library DB 20
Type II Native Database library driver Uses Native Database library on computer to access database Generally can t run inside an applet because of Native Method calls Must have database library installed on client example: DB-lib for Sybase, Oracle, MS-SQL server 21
Native-API/Partly Java Driver Client Machine Java Application Native-API/ Partly Java Driver Server Machine Database Server Vendor DB Library DB 22
Type III 100% Java Driver, NO native methods Does NOT require pre-installation on client Can be downloaded and configured on-thefly just like any Java class file Uses a proprietary protocol for talking with a middleware server Middleware server converts from proprietary calls to DBMS specific calls 23
Net-Protocol/All-Java Driver Client Machine Java Application Net-Protocol/ All- Java Driver Server Machine Middleware/ Application Server Database Server DB 24
Type IV 100% Java Driver, NO native methods Does NOT require pre-installation on client Can be downloaded and configured on-thefly just like any Java class file Unlike Type III driver, talks DIRECTLY with DBMS server Converts JDBC calls directly to database specific calls 25
Native-Protocol/Pure Java Driver Client Machine Java Application Server Machine Database Server Native-Protocol/ Pure Java Driver DB 26
Which driver to use??? Type I driver is handy for prototyping Type III driver adds security, caching, and connection control Type III and Type IV drivers need no preinstallation Preferred by 9 out of 10 Java developers: Type IV 27
JDBC Portability Type IV and Type III drivers are truly portable Other two aren t portable across OS platforms 28
JDBC API Programming
Introduction JDBC API package is java.sql There are few steps to be remembered Identifying appropriate driver Registering driver to the driver manager Requesting connection to the manager Creating statement Getting the result back 30
Identifying driver There are four JDBC driver types We ll be either using I or IV For Type-I the driver name is sun.jdbc.odbc.jdbcodbcdriver For Type-IV Oracle driver is oracle.jdbc.driver.oracledriver For Type-IV DB2 driver is com.ibm.db2.jdbc.db2driver 31
Where to find the driver JDBC-ODBC Bridge driver comes along the JDK Oracle driver can be found in oracle installed directory hierarchy ora9x/jdbc/lib/classes12.jar IBM DB2 driver can be found in db2 installed directory hierarchy SQLLIB/java/db2java.jar 32
Registering driver There are couple of ways to register a JDBC driver to the driver manager Class.forName() It takes the classpath as a String DriverManager.registerDriver() It needs the handle of the driver instance 33
Requesting connection Now here onwards we ll be using interfaces from the java.sql package DriverManager class method getconnection() is used to request the connection Connection is an interface the handle of which reference to the connection given by the manager 34
Cont d For getting connection we have to provide a URL which differ from driver to driver Jdbc-Odbc bridge driver jdbc:odbc:dsn_name Oracle Type-IV driver jdbc:oracle:thin:@host:port:dbname IBM DB2 Type-IV driver jdbc:db2://host:port//dbname 35
Creating statement Here it doesn t only means writing a SQL query it also has more in it Three types of statements can be created using JDBC API Statements can be only after getting the connection object Connection interface provides methods to create statements of all types 36
Cont d Statement (I) For performing sql queries and Batch of statements PreparedStatement (I) For creating Pre-Compiled sql queries CallableStatement (I) For calling Stored Procedures and functions 37
Getting Result For getting and working with the result of the SQL query we have an interface in out API ResultSet (I) It stores the result of the executed statement. It is keeping the record in a logical table format i.e. in rows and cols 38
Thank You Zubair Shaikh me@zubairshaikh.com