jdbc

19
JDBC Java API for Database Connectivity

Upload: roary-brennan

Post on 30-Dec-2015

34 views

Category:

Documents


1 download

DESCRIPTION

JDBC. Java API for Database Connectivity. Layout of this recitation. Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API Classes and relations among them. What is an API?. a pplication p rogram i nterface - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: JDBC

JDBC

Java API for Database Connectivity

Page 2: JDBC

Layout of this recitation

• Introduction to JDBC API

• JDBC Architecture

• Understanding the design of JDBC API– Classes and relations among them

Page 3: JDBC

What is an API?

• application program interface

• a set of routines, protocols, and tools for building software applications

• provides all the building blocks

Page 4: JDBC

What you expect in DB API

1. Open a Connection

2. Send a statement

3. Retrieve results

4. Close a connection 1. Create a connection Session

2. Execute statement

3. Send results

4. Close the session

Java Application

DBMS Engine

Java DB API

Page 5: JDBC

Big picture

Java Application

JDBC Manager

JDBC Drivers

JDBC API

JDBC Driver API

Data Sources

Grey Area

Page 6: JDBC

JDBC driver type-1

• Uses the existing API

• Not the fastest

JDBC-ODBC Bridge

ODBC Driver

Native Driver

Net Driver

Java

Non Java

Page 7: JDBC

JDBC driver type-2

• Uses vendor-provided local libraries

• Most efficient

JDBC Driver

Native Driver

Net Driver

Java

Non Java

Page 8: JDBC

JDBC driver type-3 & 4

• 100% java.

JDBC – Net Driver Java

JDBC Driver Implements a proprietary

Protocol in javaJava

Page 9: JDBC

Complete JDBC Architecture

Java Application

JDBC Driver Manager

JDBC Net

Driver A

JDBC–ODBC Bridge B

JDBC Driver C

JDBC Driver D

ODBC Driver

Native Driver B

Native Driver C

JDBC Interface

JDBC Driver Interface

A B DC

Page 10: JDBC

On Different Platforms

DBMS

PC

MAC

Unix

JDBC Driver

Classes

JDBC API

Page 11: JDBC

JDBC Interface classes

• Java.sql package– DriverManager– Connection– Statement– CallableStatement– PreparedStatement– Resultset– ResultSetMetaData– DatabaseMetatData

Page 12: JDBC

JDBC Interfaces

Driver Manager

Connection Connection Connection

StatementPrepared Statement

ResultSet

Page 13: JDBC

Simple ExampleImport java.sql;class SimpleExample {

public static void main(String args[]) {String url = “jdbc:odbc:mysource”;try {

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);Connection myConnection = DriverManager.getConnection(url,”Bond”,”TopSecret”);myConnection.close();

}catch(java.lang.Exception) {

ex.printStackTrace();}

}}

Page 14: JDBC

Sending SQL statements

……

String query= “Select name,id,salary FROM employees ORDER By salary”;

Connection myConnection = DriverManager.getConnection(…..);

Statement myStatement = myConnection.createStatement();

ResultSet rs = myStatement.executeQuery(query);

While(rs.next){

String empName = rs.getString(1);

String empId = rs.getString(2);

String empSalary = rs.getString(3);

System.out.println(“Employee ” + empName + “ with id ” + empId

+ “ earns ” + empSalary);

}

myStatement.close();

myConnection.close();

…….

Page 15: JDBC

Statement’s Execute Methods

• ResultSet executeQuery()– For SQL selects

• int executeUpdate()– For Update,Delete,Insert and Create etc.

• boolean execute()– For either type– Returns resultSetisAvailable

Page 16: JDBC

Statements

• Statement

• PreparedStatement

• CallableStatement

Page 17: JDBC

Additional stuff

• Datatypes: basic to CLOB, BLOB

• Transaction Management

• DDA with/without cursors

• Batch updates

• Metadata – DatabaseMetaData– ResultSetMetaData– ParameterMetaData

Page 18: JDBC

Advanced Techniques

• Connection Pooling

• Dynamic SQL with Prepared Statements

• Auto-generated keys

Page 19: JDBC

References

• www.google.com

• http://java.sun.com/j2se/1.4/docs/api/

• JDBC 3.0: Java Database Connectivity.– Bernard Van Haecke