Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.

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.
JDBC - Java Database Connectivity The objectives of this chapter are: To describe the architecture of JDBC To outline the classes in the java.sql package.
Distributed Application Development B. Ramamurthy.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
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.
JDBC. In This Class We Will Cover: What SQL is What ODBC is What JDBC is JDBC basics Introduction to advanced JDBC topics.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
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.
Accessing Databases with JDBC. Introduction to JDBC JDBC provides a standard library for accessing databases by using the JDBC API. JDBC standardizes.
Java Database Connectivity (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
Think Possibility Integrating Web Applications With Databases.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
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.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
JDBC – Java Database Concentricity
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.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
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 CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
JDBC By 朱志興. Four driver types of JDBC (1) JDBC-ODBC bridge plus ODBC driver: Java Database Client JDBC- ODBC bridge ODBC driver Database Server A B C.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
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.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
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.
Web Programming MySql JDBC Web Programming.
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.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
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.
DEPTT. OF COMP. SC & APPLICATIONS
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
JDBC Database Management Database connectivity
Database JDBC Overview CS Programming Languages for Web Applications
JDBC.
Chapter 16 JAVA DATABASE CONNECTIVITY
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – ODBC DRIVERS.
MSIS 655 Advanced Business Applications Programming
Java Database Connectivity
Java API for Database Connectivity
JDBC Example.
Presentation transcript:

Java Database Connectivity (JDBC)

Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly translate between the world of the database, and the world of the Java application. The idea of a universal database access API is not a new one. – For example, Open Database Connectivity (ODBC) was developed to create a single standard for database access in the Windows environment. JDBC API aims to be as simple as possible while providing developers with maximum flexibility.

Understanding JDBC Drivers To connect to a database –, you first need a JDBC Driver. JDBC Driver: – set of classes that interface with a specific database engine. Java Application JDBC Driver Manager JDBC- ODBC Bridge Vendor Specific JDBC Driver Vendor Specific ODBC Driver Database Diagram Source: Marty Hall, Core Web Programming (Prentice Hall.)

JDBC Drivers JDBC drivers exist for every major database including: – Oracle, SQL Server, Sybase, and MySQL. We will use MSAccess Data base,Microsoft’s ODBC driver,Sun’s JDBC-ODBC bridge.

Java Program uses JDBC API ODBC Data Source JDBC-ODBC bridge converts these calls to ODBC Your tables in MSAcess } do manually } do in pgm

java.sql The package comprising the core JDBC API is called java.sql We use a simple MS Access database, which means that the inbuilt JDBC-ODBC bridge driver can be employed If you wish to experiment with other databases, place the appropriate JDBC driver within folder java\jre\lib\ext.

Creating an ODBC Data Source

1.First, it is necessary to register the database as an ODBC Data Source. 2.Once this has been done, the database can be referred to by its Data Source Name (DSN).

steps to set up your own ODBC Data Source (only for ODBC databases!)

Simple Database Access

Using JDBC to access a database requires several steps

1.Load the Database Driver class is used to hold methods that operate upon other classes in order to furnish details of their characteristics forName  method of class

Using JDBC to access a database requires several steps 2.Establish a Connection to the Database declare a Connection reference call static method getConnection of class DriverManager to return a Connection object Method getConnection takes three String arguments: 1.a URL-style address for the database; 2. a user name; 3. a password.

Using JDBC to access a database requires several steps 2.Establish a Connection to the Database Address format: jdbc: :  specifies a database connection service (i.e., a driver)  provides all the information needed by the service to locate the database

Using JDBC to access a database requires several steps 2.Establish a Connection to the Database Assuming that our Finances database is indeed local and that we did not set a user name or password for this database If this same database were remote

Using JDBC to access a database requires several steps 3.Create a Statement Object and Store its Reference Call createStatement () method of our Connection object Save the address of the object returned in a Statement reference.

Using JDBC to access a database requires several steps 4.Run a Query/Update and Accept the Result(s) Class Statement has methods to execute queries: 1. executeQuery ()  to retrieve data from a database 2. executeUpdate()  to change the contents of the database The former method returns a ResultSet object, latter returns an integer that indicates the number of database rows that have been affected by the updating operation.

Using JDBC to access a database requires several steps 4.Run a Query/Update and Accept the Result(s)

Using JDBC to access a database requires several steps 4.Run a Query/Update and Accept the Result(s)

Using JDBC to access a database requires several steps 5.Manipulate/Display/Check Result(s) The only method of ResultSet that we need to make use of at present is next, which moves the ResultSet cursor/pointer to the next row in the set of rows referred to by that object. We can retrieve data via either the field name or the field position using the appropriate getXXX () method

5.Manipulate/Display/Check Result(s)

6.Repeat Steps 4 and 5 as Required 7.Close the Connection

Modifying the Database Contents

Database Update Statements : INSERT DELETE UPDATE Update statements are submitted via the executeUpdate() method of statement interface Modifying the Database Contents

Example:

Transactions

one or more SQL statements that may be grouped together as a single processing entity We want either all the statements or none of them to be executed. Java provides the Connection interface methods 1. commit  used at the end of a transaction to commit/finalise the database changes 2. rollback  used to restore the database to the state it was in prior to the current transaction

Transactions By default, JDBC automatically commits each individual SQL statement that is applied to a database. In order to change this default behaviour so that transaction processing may be carried out, we must first execute Connection method setAutoCommit with an argument of false We can then use methods commit and rollback to effect transaction processing.

Transactions

Meta Data

'data about data‘ Two categories of meta data available through the JDBC API: data about the rows and columns returned by a query (i.e., data about ResultSet objects); data about the database as a whole. The first of these is provided by interface ResultSetMetaData, an object of which is returned by the ResultSet method getMetaData

Meta Data data about the rows and columns is provided by interface ResultSetMetaData, an object of which is returned by the ResultSet method getMetaData Data about the database as a whole is provided by interface DatabaseMetaData, an object of which is returned by the Connection method getMetaData

Meta Data Information available from a ResultSetMetaData object includes the following: the number of fields/columns in a ResultSet object; the name of a specified field; the data type of a field; the maximum width of a field; the table to which a field belongs.

Meta Data SQL types is represented in class java.sql.Types INTEGER and VARCHAR are particularly commonplace, the latter of these corresponding to string values.

Meta Data The example coming up makes use of the following ResultSetMetaData methods, which return properties of the database fields held in a ResultSetMetaData object.

Meta Data

Scrollable ResultSets in JDBC 2

With the emergence of JDBC 2 in Java 2, however, a great deal more flexibility was made available to Java programmers by the introduction of the following ResultSet methods:

Scrollable ResultSets in JDBC 2 results.relative(-3); //Move back 3 rows. results.absolute(3); // Move to row 3 (from start of ResultSet). An overloaded form of the Connection method createStatement is used to create scrollable ResultSets. This method that takes two integer arguments.

Scrollable ResultSets in JDBC 2 There are three possible values that can take: TYPE_SCROLL_SENSITIVE causes any changes made to the data rows to be reflected dynamically in the ResultSet object, while TYPE_SCROLL_INSENSITIVE does not.

Scrollable ResultSets in JDBC 2 can take two possible values the first means that we cannot make changes to the ResultSet rows, while the second will allow changes to be made

Scrollable ResultSets in JDBC 2

Modifying Databases via Java Methods

With of JDBC 2 we can modify ResultSet rows directly via Java methods Without having to send SQL statements the changes reflected in the database itself! To do this: 1.use the second version of createStatement 2.supply ResultSet.CONCUR_UPDATABLE as the second argument.

Modifying Databases via Java Methods The updateable ResultSet object does not have to be scrollable

Modifying Databases via Java Methods A set of updateXXX methods are used to update database. Each of these methods takes two arguments: a string specifying the name of the field to be updated; a value of the appropriate type that is to be assigned to the field

1. Updating There are three steps involved in the process of updating:

2. Insertion For an insertion, the new row is initially stored within a special buffer called the 'insertion row' and there are three steps involved in the process:

2. Insertion

3. Delete

Example :

Sample Output