Top Banner
Java DataBase Connectivity Matt Driver JDBC calls Database commands Database
23

Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

Dec 13, 2015

Download

Documents

Blaze Clark
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

Java DataBase Connectivity

Matt

DriverJDBC calls

Database commands

Database

Page 2: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

2

What is JDBC

Page 3: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

3

JDBC• JDBC is an API developed by Sun Microsystems that provides a standar

d way to access data using the Java programming language.

• Using JDBC, an application can access a variety of databases and run on any platform with a Java Virtual Machine.

• The JDBC API defines a set of Java interfaces that encapsulate major database functionality.

• JDBC is a standard interface for connecting to relational databases from Java.

• The JDBC classes and interfaces are in the java.sql package.

Page 4: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

4

Types of JDBC Drivers• JDBC DBC bridge driver plus ODBC driver: The Sun Microsystems bridge product provi

des JDBC access via ODBC drivers. Note that ODBC binary code, and in many cases database client code, must be loaded on each client machine that uses this driver. As a result, this kind of driver is most appropriate on a corporate network where client installations are not a major problem or for application server code written in Java in a three-tier architecture.

• Native-API partly Java driver: This kind of driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, IBM DB2, or other DBMSs. Note that, like the bridge driver, this style of driver requires that some operating system-specific binary code be loaded on each client machine.

• JDBC-Net pure Java driver: This driver translates JDBC calls into a DBMS-independent net protocol, which is then translated to a DBMS protocol by a server. This net server middleware is able to connect its pure Java clients to many different databases. The specific protocol used depends on the vendor.

• Native-protocol pure Java driver: This kind of driver converts JDBC calls directly into the network protocol used by DBMSs. This allows a direct call from the client machine to the DBMS server and is an excellent solution for intranet access. Several that are now available include Oracle, Sybase, IBM DB2, Borland InterBase, and Microsoft SQL Server.

Page 5: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

5

Types of JDBC Drivers • Type 1

JDBC-ODBC bridge

• Type 2 partial Java driver

type1 type2 type4 type3

• Type 3 pure Java driver for database

middleware

• Type 4 pure Java driver for direct-to-

database

Page 6: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

6

Page 7: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

7

Connection Pooling• Connection pooling is a framework for caching database connection

• reuse of physical connections. reduce overhead of creating new connections.

• Connection pool data source Factory to creating pooled connections

• Pooled connection represents a physical connection to a data source. Factory for the Connection Objects.

• Connection CacheConnection

PooledConnection

ConnectionEvent

ConnectionPoolDataSource

ConnectionEventListener

getPooledConnection

getConnection

( close or error event)

Page 8: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

8

JDBC Architecture

Page 9: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

9

JDBC Overall Architecture

DriverJDBC calls

Database commands

Database

Page 10: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

10

JDBC Three-tier Model

Page 11: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

11

Page 12: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

12

Page 13: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

13

JDBC Architecture Conceptual Diagram

Page 14: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

14

Basic JDBC class diagram

Page 15: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

15

JDBC API

Page 16: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

16

Overview of Querying a Database With JDBC

Query

Close

Connect

Processresults

Page 17: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

17

Page 18: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

18

JDBC API

• The JDBC 3.0 API includes all of the API in the java.sql package (sometimes called the core API) and the javax.sql package (the Optional Package API, formerly called the Standard Extension API).

Page 19: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

19

• DriverManager 負責載入各種不同驅動程序( Driver ),並根據不同的請求,向調用者返回相應的資料

庫連接( Connection )。

• Driver 驅動程序,會將自身載入到 DriverManager 中去,並處理相應的請求並返回相應的資料

庫連接( Connection )。

• Connection 資料庫連接,負責與進行資料庫間通訊, SQL 執行以及事務處理都是在某個特定 Conne

ction 環境中進行的。可以產生用以執行 SQL 的 Statement 。

• Statement 用以執行 SQL 查詢和更新(針對靜態 SQL 語句和單次執行)。

• PreparedStatement 用以執行包含動態參數的 SQL 查詢和更新(在伺服器端編譯,允許重複執行以提高效

率)。

• CallableStatement 用以調用資料庫中的存儲過程

• SQLException 代表在資料庫連接的建立和關閉和 SQL 語句的執行過程中發生了例外情況(即錯誤)。

Page 20: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

20

Package java.sql

Page 21: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

21

Package java.sql

Page 22: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

22

資料型態

Page 23: Java DataBase Connectivity Matt Driver JDBC calls Database commands Database.

23

JDBC Example Code (Application)import java.sql.*;public class SimpleDBConnection {

public static void main(String[] args) {try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");try {

Connection conn = DriverManager.getConnection("jdbc:odbc:dandelion", "sa", "password");

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("SELECT ticker, shares, price FROM trades");

while (rs.next()) {System.out.println(rs.getString("ticker") +

"\t" + rs.getInt("shares") +"\t" + rs.getFloat("price"));

}rs.close();stmt.close();conn.close();

}catch (SQLException se) {

System.out.println("SqlException: " + se.getMessage());se.printStackTrace(System.out);

}}catch (ClassNotFoundException e) {

System.out.println("ClassNotFound: " + e.getMessage());}

} //main} //class