Java Database Connectivity


Published on

Published in: Technology
1 Comment
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java Database Connectivity

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