Download presentation
Presentation is loading. Please wait.
Published byBlaze Clark Modified over 9 years ago
1
Java DataBase Connectivity Matt Driver JDBC calls Database commands Database
2
2 What is JDBC
3
3 JDBC JDBC is an API developed by Sun Microsystems that provides a standard 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.
4
4 Types of JDBC Drivers JDBC DBC bridge driver plus ODBC driver: The Sun Microsystems bridge product provides 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.
5
5 Types of JDBC Drivers Type 1 JDBC-ODBC bridge Type 2 partial Java driver type1type2type4 type3 Type 3 pure Java driver for database middleware Type 4 pure Java driver for direct-to- database
6
6
7
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 Cache Connection PooledConnection ConnectionEvent ConnectionPoolDataSource ConnectionEventListener getPooledConnection getConnection ( close or error event)
8
8 JDBC Architecture
9
9 JDBC Overall Architecture Driver JDBC calls Database commands Database
10
10 JDBC Three-tier Model
11
11
12
12
13
13 JDBC Architecture Conceptual Diagram
14
14 Basic JDBC class diagram
15
15 JDBC API
16
16 Overview of Querying a Database With JDBC Query Close Connect Process results
17
17
18
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).
19
19 DriverManager 負責載入各種不同驅動程序( Driver ),並根據不同的請求,向調用者返回相應的資料 庫連接( Connection )。 Driver 驅動程序,會將自身載入到 DriverManager 中去,並處理相應的請求並返回相應的資料 庫連接( Connection )。 Connection 資料庫連接,負責與進行資料庫間通訊, SQL 執行以及事務處理都是在某個特定 Connection 環境中進行的。可以產生用以執行 SQL 的 Statement 。 Statement 用以執行 SQL 查詢和更新(針對靜態 SQL 語句和單次執行)。 PreparedStatement 用以執行包含動態參數的 SQL 查詢和更新(在伺服器端編譯,允許重複執行以提高效 率)。 CallableStatement 用以調用資料庫中的存儲過程 SQLException 代表在資料庫連接的建立和關閉和 SQL 語句的執行過程中發生了例外情況(即錯誤)。
20
20 Package java.sql
21
21 Package java.sql
22
22 資料型態
23
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.