1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
Distributed Application Development B. Ramamurthy.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
JDBC. In This Class We Will Cover: What SQL is What ODBC is What JDBC is JDBC basics Introduction to advanced JDBC topics.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
Lab: JDBC 1 Computer Engineering Lab III v Objective –to give some background on JDBC to help with the lab exercises , Semester 1,
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
MIS 3023 Business Programming II Professor: Akhilesh Bajaj Introduction to JDBC © Akhilesh Bajaj, All Rights Reserved.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
JDBC
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
JDBC Chapter 1 JDBC Introduction
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
SQL and Java The vision for Java is to be the concrete and nails that people use to build this incredible network system that is happening all around us.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
DEPTT. OF COMP. SC & APPLICATIONS
Interacting with Database
JDBC Database Management Database connectivity
CS320 Web and Internet Programming Database Access with JDBC
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
Java API for Database Connectivity
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few. In the late 1990's Sun Microsystems started to develop a way for Java developers to write high-level code that accesses all of the popular DBMS's. In 1996 Sun Microsystems came out with the JDBC driver and the JDBC API. The JDBC driver is really a specification that described the detail functionality of a JDBC driver. DBMS manufactures and third-party vendors were encouraged to build JDBC drivers that conformed to Sun's specifications. This allowed manufactures of DBMS's to tap into the growing Java application market.

2 Java Database Connection (JDBC) Java programmers could then use high-level Java data objects defined in the JDBC API to write a routine that interacted with the DBMS. Java data objects convert the routine into low-level messages that conform to the JDBC driver specification and send them to the JDBC driver. The JDBC driver translates the routine into low-level messages that are understood and processed by the DBMS.

3 Java Database Connection (JDBC) JDBC drivers created by DBMS manufactures have to: Open a connection between the DBMS and the J2EE component. Translate low-level equivalents of SQL statements sent by the J2EE component into messages that can be processed by the DBMS. Return data that conforms to the JDBC specification to the JDBC driver. Return information such as error messages that conforms to the JDBC specification to the JDBC driver. Provide transaction management routines that conform to the JDBC specification. Close the connection between the DBMS and the J2EE component.

4 Java Database Connection (JDBC) Today there are JDBC drivers for nearly every commercial DBMS, and they are available from either from Sun Microsystem's web site ( or from the DBMS manufacture's web site. Java code independence is also extended to implementation of the SQL queries. SQL queries are passed from the JDBC API through the JDBC driver to the DBMS without validation. This means it is the responsibility of the DBMS to implement SQL statements contained in the query.

5 JDBC Driver Types JDBC drivers are classified into four groups: Type 1 JDBC-to-ODBC Driver Type 2 Java/Native Code Driver Type 3 JDBC Driver Type 4 JDBC Driver

6 Type 1 JDBC-to-ODBC Driver Microsoft's Open Database Connection (ODBC) was used by Sun Microsystems for the basis for creating JDBC. Both ODBC and JDBC have similar driver specifications and API. This driver type is sometimes referred to as the JDBC/ODBC Bridge. This driver is used to translate DBMS calls between the JDBC specification and the ODBC specification. The JDBC-to-ODBC driver receives messages from a J2EE component that conforms to the JDBC specification talked about earlier. Those messages are translated by the JDBC-to-ODBC driver into the ODBC message format, which is then translated into the message format understood by the DBMS. This type driver should not be used in a mission-critical application because the extra translation might negatively impact performance.

7 Type 2 Java/Native Code Driver This driver uses Java classes to generate platform-specific code (code that is only understood by a specific DBMS). The manufacturer of the DBMS provides both the Java/Native Code driver and API classes so the J2EE component can generate the platform specific-code. The disadvantages that you have a loss of some portability of code and that the API classes for the driver probably won't work with another manufacturer's DBMS.

8 Type 3 JDBC Driver This driver type is also referred to as the Java Protocol and is the most commonly used JDBC driver. The Type 3 JDBC Driver converts SQL queries into JDBC-formatted statements. These statements are then translated into the format required by the DBMS.

9 Type 4 JDBC Driver This driver type is also known as the Type 4 database protocol. It is similar to the Type 3 JDBC driver except SQL queries are translated into the format required by the DBMS. SQL queries do not need to be converted to JDBC-formatted systems. This is the fastest way to communicate SQL queries to the DBMS.

10 JDBC Packages The JDBC API is contained in two packages. java.sql javax.sql

11 java.sql This package contains core Java data objects of the JDBC API. These include Java data objects that provide the basis for connecting to the DBMS and interacting with the data stored in the DBMS. java.sql is part of the J2SE. To use this package in a Java application you must use the following: import java.sql.*;

12 javax.sql This package contains the JDBC API. javax.sql extends java.sql and is in the J2EE. Included in this package are the Java data objects that interact with Java Naming and Directory Interface (JNDI) and the Java data objects that manage connection pooling, among other advanced JDBC features. To use this package in a Java application you must use the following: import javax.sql.*;

13 A Brief Overview of the JDBC Process Although each J2EE component is different, J2EE components use a similar process for interacting with a DBMS. This process is divided into five routines: 1.Loading the JDBC driver 2.Connecting to the DBMS 3.Creating and executing a statement (like a SQL query) 4.Processing data returned by the DBMS (a result set) 5.Terminating the connection with the DBMS

14 Loading the JDBC driver The JDBC driver must be loaded before the J2EE component can connect to the DBMS. The Class.forName() method is used to load the JDBC driver. Suppose you want to work offline and write a J2EE component that interacts with Microsoft Access on a developer's PC. The developer must write a routine that loads the JDBC/ODBC Bridge driver. This routine is called sun.jdbc.odbc.JdbcOdbcDriver. You should also check for and handle errors.

15 Loading the JDBC driver - Example import java.sql.*; try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); } catch ( ClassNotFoundException e ) { System.out.println( "classname not found '" + e.getMessage() + "'" ); System.exit( 0 ); } catch (SQLException error) { System.err.println("Cannot connect to the database." + error); System.exit( 2 ); }

16 Connect to the DBMS Once the driver is loaded, the J2EE component must connect to the DBMS using the DriverManager.getConnection() method. The java.sql.DriverManager class is the highest class in the java.sql hierarchy and is responsible for managing driver information. The DriverManager.getConnection() method is passed the URL of the database, and the user ID and password if required by the DBMS. The URL is a String object that contains the driver name and the name of the database that is being accessed by the J2EE component.

17 Connect to the DBMS The DriverManager.getConnection() method returns a Connection interface that is used throughout the process to reference the database that is being accessed by the J2EE component. The java.sql.Connection interface is another member of the java.sql package that manages communication between the driver and the J2EE component. It is the java.sql.Connection interface that sends statements to the DBMS for processing.

18 Connect to the DBMS Example import java.sql.*; private String url = "jdbc:odbc:database-name"; private String userID = "user-id for database, if any"; private String password = "password for database, if any"; private static Connection Db; try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); Db = DriverManager.getConnection( url, userID, password ); } catch ( ClassNotFoundException e ) { System.out.println( "classname not found '" + e.getMessage() + "'" ); System.exit( 0 ); } catch (SQLException error) { System.err.println( "Cannot connect to the database." + error ); System.exit( 2 ); }

19 Create and Execute a SQL Statement After the JDBC driver is loaded and the connection is successfully made with a particular database managed by the DBMS you will need to send a SQL query to the DBMS for processing. The Connect.createStatement() method is used to create a Statement object. The resulting Statement object is then used to execute a query and return a ResultSet object that contains the response from the DBMS. This response is usually one or more rows of information requested by the J2EE component. Typically, the query is assigned to a String object, which is passed to the object's executeQuery() method. Once the ResultSet is received from the DBMS, the close() method is called to terminate the statement.

20 Create and Execute a SQL Statement - Example import java.sql.*; private static Connection Db; // Connection object from above private Statement DataRequest; // create a Statement object private ResultSet Results; // create a ResultSet object try { // build SQL query in a String object String query = "SELECT... FROM table-name"; DataRequest = Db.createStatement(); Results = DataRequest.executeQuery( query ); } catch ( SQLException error ){ System.err.println( "SQL error." + error ); System.exit( 3 ); }

21 Processing Data Returned by the DBMS The java.sql.ResultSet object is assigned the results received from the DBMS after the query is processed. It also consists of methods used to interact with data that is returned by the DBMS to the J2EE component. Some of the methods used are the next(), getString(), getxxx(), and close() methods.

22 next( ) method public boolean next() throws SQLException Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on. If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read. This method returns true if the new current row is valid or false if there are no more rows Throws: SQLException - if a database access error occurs

23 close() method public void close() throws SQLException Releases this ResultSet object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. Note: A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re- executed, or is used to retrieve the next result from a sequence of multiple results. A ResultSet object is also automatically closed when it is garbage collected. Throws: SQLException - if a database access error occurs

24 Example import java.sql.*; private String LastName; private boolean Records; // used for the next() method. try { Records = Results.next(); if ( !Records ) { System.out.println( "No data returned" ); System.exit( 4 ); } catch ( SQLException error ) { System.err.println( "SQL error." + error ); System.exit( 3 ); }

25 Example (continued) try { do {. LastName = Results.getString ( 2 ) ; OR LastName = Results.getString ( "LastName" ) ;. } while ( Results.next() ); DataRequest.close(); } catch ( SQLException error ) { System.err.println( "Data display error." + error ); System.exit( 5 ); }