Advanced Programming Java Database Connectivity Gera 2020 1
Introduction •A database is an organized collection of data. •There are many different strategies for organizing data to facilitate easy access and manipulation. •A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data for many users. •Database management systems allow for the access and storage of data without concern for the internal representation of data. 2
Java Database Connectivity (JDBC) •JDBC is Java application programming interface that allows the Java programmers to access database management system from the java code. •It was developed by JavaSoft, a subsidiary of Sun Microsystems. •It is a java API which enables the java programs to execute SQL statements. 3
Java Database Connectivity (JDBC)… • JDBC provides methods for querying and updating the data in Relational Database Management system such as SQL, Oracle etc. • Generally all Relational Database Management System supports SQL and we all know that Java is platform independent. • So JDBC makes it possible to write a single database application that can run on different platforms and interact with different Database Management Systems. 4
The Structured Query Language SQL •JDBC is an interface to SQL, which is the interface to essentially all modern relational databases. •Desktop databases usually have a graphical user interfaces that lets users manipulate the data directly, but server-based databases are accessed purely through SQL. •Most desktop databases have a SQL interface as well, but it often does not support the full range of ANSI SQL92 features, the current standard for SQL. 5
SQL Basic SELECT Query • Let us consider several SQL queries that extract information from database books. • A SQL query “selects” rows and columns from one or more tables in a database. Such selections are performed by queries with the SELECT keyword. • The basic form of a SELECT query is SELECT * FROM tableName in which the asterisk (*) indicates that all columns from the tableName table should be retrieved. • For example, to retrieve all the data in the authors table, use SELECT * FROM authors 6
SQL Basic SELECT Query • For example, to retrieve only the columns authorID and lastName for all rows in the authors table, use the query SELECT authorID, lastName FROM authors This query returns the data listed as following sample: 7
SQL Basic SELECT Query • SQL query keywords 8
Installing JDBC •First, you need a database program that is compatible with JDBC. •You will also need to create a database for your experimental use. •We assume you will call this database COREJAVA. •Create a new database, or have your database administrator create one with the appropriate permissions. You need to be able to create, update, and drop tables. 9
Installing JDBC… • If you have never installed a client/server database before, you may find that setting up the database is somewhat complex and that it can be difficult to diagnose the cause for failure. • One alternative is to install a pure Java database such as Cloudscape (http://www.cloudscape.com), PointBase (http://www.pointbase.com), or InstantDB (http://www.lutris.com/products/instantDB). • These databases are less powerful but simple to set up. 10
Installing JDBC… •Essentially all database vendors already have JDBC drivers. •You need to locate the vendor's instructions to load the driver into your program and to connect to the database. 11
Basic JDBC Programming Concepts •Programming with the JDBC classes is, by design, not very different from programming with the usual Java platform classes: •You build objects from the JDBC core classes, extending them by inheritance if need be. 12
Components of JDBC 1. The JDBC API. 2. The JDBC Driver Manager. 3. The JDBC Test Suite. 4. The JDBC-ODBC Bridge. 13
JDBC API • The JDBC API provides the facility for accessing the relational database from the Java programming language. • The user not only execute the SQL statements, retrieve results, and update the data but can also access it anywhere within a network because of it's "Write Once, Run Anywhere" (WORA) capabilities. • Due to JDBC API technology, user can also access other tabular data sources like spreadsheets or flat files even in the a heterogeneous environment. 14
JDBC API… • JDBC application programming interface is a part of the Java platform that have included Java SE and Java EE in itself. • The latest version of JDBC 4.0 application programming interface is divided into two packages. i) java.sql. ii) javax.sql. • Java SE and Java EE platforms are included in both the packages. 15
JDBC Driver • A JDBC driver translates standard JDBC calls into a network or database protocol OR into a database library API call that facilitates communication with the database. • There are four distinct types of JDBC drivers. 1. JDBC-ODBC Bridge + ODBC Driver, also called as Type1 2. Native API , Pure/partly Java Driver, also called Type 2. 3. JDBC –Net Pure Java Driver, also called Type 3. 4. Native Protocol , Pure Java Driver, also called Type 4. 16
JDBC Driver Manager • The JDBC Driver Manager is a very important class that defines objects which connect Java applications to a JDBC driver. • Usually Driver Manager is the backbone of the JDBC architecture. • It's very simple and small that is used to provide a means of managing the different types of JDBC database driver running on an application. 17
JDBC Driver Manager… •The main responsibility of JDBC database driver is to load all the drivers found in the system properly as well as to select the most appropriate driver from opening a connection to a database. •The Driver Manager also helps to select the most appropriate driver from the previously loaded drivers when a new open database is connected. 18
JDBC Test Suite •The function of JDBC driver test suite is to make ensure that the JDBC drivers will run user's program or not . •The test suite of JDBC application program interface is very useful for testing a driver based on JDBC technology during testing period. •It ensures the requirement of Java Platform Enterprise Edition (J2EE). 19
ODBC (Open Data Base Connectivity) •Open DataBase Connectivity is similar to Java Database Connectivity which is used for accessing and managing database, but the difference is that JDBC is designed specifically for Java programs, whereas ODBC is not depended upon any language. 20
JDBC-ODBC Bridge •The JDBC-ODBC bridge, also known as JDBC type 1 driver is a database driver that utilize the ODBC driver to connect the database. •This driver translates JDBC method calls into ODBC function calls. •The Bridge implements Jdbc for any database for which an Odbc driver is available. •The Bridge is always implemented as the sun.jdbc.odbc Java package and it contains a 21
Functions of JDBC-ODBC Bridge •Translates query obtained by JDBC into corresponding ODBC query, which is then handled by the ODBC driver. •Sun provides a JDBC-ODBC Bridge driver. sun.jdbc.odbc.JdbcOdbcDriver. This driver is native code and not Java, and is closed source. •Client -> JDBC Driver -> ODBC Driver -> Database •There is some overhead associated with the translation work to go from JDBC to ODBC. 22
Components of JDBC •This first two component of JDBC, the JDBC API and the JDBC Driver Manager manages to connect to the database and then build a java program that utilizes SQL commands to communicate with any RDBMS. •On the other hand, the last two components are used to communicate with ODBC or to test web application in the specialized environment. 23
References ➢S. Horstmann and Gary Cornell, Core Java 2 – Volume II- Advanced Features, Sun Microsystems Press ➢Harvey M. Deitel and Paul J. Deitel, Java How to Program, Deitel & Associates ➢Nitesh Kukreja and Pooja Talreja, “An introduction to java connectivity (JDBC),” 24 Gerabirhan Paulos ToCourseInfo@gmail.com

Java Database Connectivity (Advanced programming)

  • 1.
    Advanced Programming Java DatabaseConnectivity Gera 2020 1
  • 2.
    Introduction •A database isan organized collection of data. •There are many different strategies for organizing data to facilitate easy access and manipulation. •A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data for many users. •Database management systems allow for the access and storage of data without concern for the internal representation of data. 2
  • 3.
    Java Database Connectivity(JDBC) •JDBC is Java application programming interface that allows the Java programmers to access database management system from the java code. •It was developed by JavaSoft, a subsidiary of Sun Microsystems. •It is a java API which enables the java programs to execute SQL statements. 3
  • 4.
    Java Database Connectivity(JDBC)… • JDBC provides methods for querying and updating the data in Relational Database Management system such as SQL, Oracle etc. • Generally all Relational Database Management System supports SQL and we all know that Java is platform independent. • So JDBC makes it possible to write a single database application that can run on different platforms and interact with different Database Management Systems. 4
  • 5.
    The Structured QueryLanguage SQL •JDBC is an interface to SQL, which is the interface to essentially all modern relational databases. •Desktop databases usually have a graphical user interfaces that lets users manipulate the data directly, but server-based databases are accessed purely through SQL. •Most desktop databases have a SQL interface as well, but it often does not support the full range of ANSI SQL92 features, the current standard for SQL. 5
  • 6.
    SQL Basic SELECTQuery • Let us consider several SQL queries that extract information from database books. • A SQL query “selects” rows and columns from one or more tables in a database. Such selections are performed by queries with the SELECT keyword. • The basic form of a SELECT query is SELECT * FROM tableName in which the asterisk (*) indicates that all columns from the tableName table should be retrieved. • For example, to retrieve all the data in the authors table, use SELECT * FROM authors 6
  • 7.
    SQL Basic SELECTQuery • For example, to retrieve only the columns authorID and lastName for all rows in the authors table, use the query SELECT authorID, lastName FROM authors This query returns the data listed as following sample: 7
  • 8.
    SQL Basic SELECTQuery • SQL query keywords 8
  • 9.
    Installing JDBC •First, youneed a database program that is compatible with JDBC. •You will also need to create a database for your experimental use. •We assume you will call this database COREJAVA. •Create a new database, or have your database administrator create one with the appropriate permissions. You need to be able to create, update, and drop tables. 9
  • 10.
    Installing JDBC… • Ifyou have never installed a client/server database before, you may find that setting up the database is somewhat complex and that it can be difficult to diagnose the cause for failure. • One alternative is to install a pure Java database such as Cloudscape (http://www.cloudscape.com), PointBase (http://www.pointbase.com), or InstantDB (http://www.lutris.com/products/instantDB). • These databases are less powerful but simple to set up. 10
  • 11.
    Installing JDBC… •Essentially alldatabase vendors already have JDBC drivers. •You need to locate the vendor's instructions to load the driver into your program and to connect to the database. 11
  • 12.
    Basic JDBC ProgrammingConcepts •Programming with the JDBC classes is, by design, not very different from programming with the usual Java platform classes: •You build objects from the JDBC core classes, extending them by inheritance if need be. 12
  • 13.
    Components of JDBC 1.The JDBC API. 2. The JDBC Driver Manager. 3. The JDBC Test Suite. 4. The JDBC-ODBC Bridge. 13
  • 14.
    JDBC API • TheJDBC API provides the facility for accessing the relational database from the Java programming language. • The user not only execute the SQL statements, retrieve results, and update the data but can also access it anywhere within a network because of it's "Write Once, Run Anywhere" (WORA) capabilities. • Due to JDBC API technology, user can also access other tabular data sources like spreadsheets or flat files even in the a heterogeneous environment. 14
  • 15.
    JDBC API… • JDBCapplication programming interface is a part of the Java platform that have included Java SE and Java EE in itself. • The latest version of JDBC 4.0 application programming interface is divided into two packages. i) java.sql. ii) javax.sql. • Java SE and Java EE platforms are included in both the packages. 15
  • 16.
    JDBC Driver • AJDBC driver translates standard JDBC calls into a network or database protocol OR into a database library API call that facilitates communication with the database. • There are four distinct types of JDBC drivers. 1. JDBC-ODBC Bridge + ODBC Driver, also called as Type1 2. Native API , Pure/partly Java Driver, also called Type 2. 3. JDBC –Net Pure Java Driver, also called Type 3. 4. Native Protocol , Pure Java Driver, also called Type 4. 16
  • 17.
    JDBC Driver Manager •The JDBC Driver Manager is a very important class that defines objects which connect Java applications to a JDBC driver. • Usually Driver Manager is the backbone of the JDBC architecture. • It's very simple and small that is used to provide a means of managing the different types of JDBC database driver running on an application. 17
  • 18.
    JDBC Driver Manager… •Themain responsibility of JDBC database driver is to load all the drivers found in the system properly as well as to select the most appropriate driver from opening a connection to a database. •The Driver Manager also helps to select the most appropriate driver from the previously loaded drivers when a new open database is connected. 18
  • 19.
    JDBC Test Suite •Thefunction of JDBC driver test suite is to make ensure that the JDBC drivers will run user's program or not . •The test suite of JDBC application program interface is very useful for testing a driver based on JDBC technology during testing period. •It ensures the requirement of Java Platform Enterprise Edition (J2EE). 19
  • 20.
    ODBC (Open DataBase Connectivity) •Open DataBase Connectivity is similar to Java Database Connectivity which is used for accessing and managing database, but the difference is that JDBC is designed specifically for Java programs, whereas ODBC is not depended upon any language. 20
  • 21.
    JDBC-ODBC Bridge •The JDBC-ODBCbridge, also known as JDBC type 1 driver is a database driver that utilize the ODBC driver to connect the database. •This driver translates JDBC method calls into ODBC function calls. •The Bridge implements Jdbc for any database for which an Odbc driver is available. •The Bridge is always implemented as the sun.jdbc.odbc Java package and it contains a 21
  • 22.
    Functions of JDBC-ODBCBridge •Translates query obtained by JDBC into corresponding ODBC query, which is then handled by the ODBC driver. •Sun provides a JDBC-ODBC Bridge driver. sun.jdbc.odbc.JdbcOdbcDriver. This driver is native code and not Java, and is closed source. •Client -> JDBC Driver -> ODBC Driver -> Database •There is some overhead associated with the translation work to go from JDBC to ODBC. 22
  • 23.
    Components of JDBC •Thisfirst two component of JDBC, the JDBC API and the JDBC Driver Manager manages to connect to the database and then build a java program that utilizes SQL commands to communicate with any RDBMS. •On the other hand, the last two components are used to communicate with ODBC or to test web application in the specialized environment. 23
  • 24.
    References ➢S. Horstmann andGary Cornell, Core Java 2 – Volume II- Advanced Features, Sun Microsystems Press ➢Harvey M. Deitel and Paul J. Deitel, Java How to Program, Deitel & Associates ➢Nitesh Kukreja and Pooja Talreja, “An introduction to java connectivity (JDBC),” 24 Gerabirhan Paulos ToCourseInfo@gmail.com