天天看點

JDK6筆記(5)----JDBC4

版權聲明:本文為部落客chszs的原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/chszs/article/details/1554793

JDK6筆記(5)----JDBC4

1、Java開發者使用JDBC4 API可以做下面的事:

1) Connect to a data source

連接配接資料源

2) Execute complex SQL statements

執行複雜的SQL語句

3) Persist changes to a data source

對資料源的持續操作(改變)

4) Retrieve information from a data source

從資料源取回資訊

5) Interact with legacy file systems

和遺留的檔案系統互動

2、The JDBC API is based on the specification X/Open SQL Call Level Interface(CLI), which provides an application with an alternative method for accessing databases with embedded SQL calls.

JDBC API基于X/Open SQL Call Level Interface(CLI)規範,該規範提供了一種基于選擇方法的應用,它通過内嵌的SQL調用來通路資料庫。

ODBC is also based on this standard, and the JDBC API can interface with ODBC through JDBC-ODBC bridge dirvers.

ODBC也是基于該标準,JDBC API可通過JDBC-ODBC橋驅動來通路ODBC的接口。

1) JDBC-ODBC Bridge Driver

JDBC driver used to bridge the gap between JDBC and ODBC. It allows them to communicate and is mostly used in three-tier architectures. This is not a pure Java solution.

2) Native API/Part Java Driver

Specific to a DBMS (Database Management System) and converts JDBC calls to specific client calls for the DBMS being used. This type of driver is usually operating-system specific and is also not a pure Java solution.

3) JDBC-Net Pure Java Driver

Uses net server middleware for connecting Java clients to DBMS. It converts the JDBC calls into an independent protocol that can then be used to interface with the DBMS. This is a pure Java solution with the main drawback being security.

4) Native-Protocol Pure Java Driver

Provided by the database vendor, and its main purpose is to convert JDBC calls into the network protocol understood by the DBMS. This is the best solution to use and is pure Java.

前兩類通常是一種臨時的解決方案,用于某些DBMS還沒有JDBC驅動的情況。

第三、四類是正常的解決方案。

3、The JDBC API is contained in two Java packages-- java.sql and javax.sql.

1) java.sql package, it contains the original core APIs for JDBC.

2) javax.sql package, it contains optional, more advanced features such as row sets, connection pooling, and distributed transaction management.

It is important to determine your application's data access needs and architecture ahead of time to properly assess which packages you need to import.

4、The JDBC API is most commonly used by applications to access data in two main models: the two-tier model and three-tier model, both of which are covered in the following paragraphs.

1) The two-tier model is the simplest of the models. It comprises a client layer and a server layer. The client layer interacts directly with the server layer, and no middleware is used.

The business logic, application/presentation layer, transaction management, and connection management are all handled by the client layer.

The server layer contains only the data source and doesn't manage anything that the client is doing, except for user access and rights.

This is a good design for small applications but would present a scalability dilemma for larger applications requiring more robust connection and transaction management.

2) The three-tier model is the most complex and the most scalable of the models.

It removes the business logic and adds a layer of abstraction to the data sources.

2.1) The client layer is this model is a thin client layer that contains only very lightweight presentation layers that will run on web browsers, Java Programs, PDAs, Tablet PCs, and so forth.

It does not handle business logic, methods of accessing the data sources, the drivers used to provide access, or the methods in which data is saved.

2.2) The middle layer is where the core of the functionality exists in the three-tier model.

The thin clients interact with applications that support the business logic and interactopms wotj data spirces.

Connection pools, transaction management, and JDBC drivers can all be found here.

This is the layer that adds increased performance and scalability compared to the two-tier model.

2.3) The server layer is where the data sources such as database management systems and files exist. The only interaction that occurs here is from the middle layer to the server layer through a JDBC driver.

The main benefit of the three-tier model is the fact that it adds layers of abstraction that can be scaled, removed, added, and improved upon.

It also adds extra performance benefits when simultaneously accessing multiple data sources.

5、JDBC4.0 has associated with it a number of performance enhancements targeted at the Driver level to be taken advantage of in a three-tier or enterprise environment.

A Java application can hava multiple connections to multiple data sources at the same time using multiple Connection objects.

獲得Connection對象的兩種方法:

1) 通過DriverManager類

這是傳統的方法。以Derby為例:

Class.forName("org.apache.derby.jdbc.ClientDriver");

然後:

String sURL="jdbc:derby//localhost:9527/wrox";

String sUsername="sa";

String sPassword="password";

try{

 //Obtain a connection

 Connection cConn=DriverManager.getConnection(sURL, sUsername, sPassword);

 }catch(...){

 }finally{

  if(cConn!=null){

   cConn.close(); //Close the connection

  }

 }

2) 通過執行DataSource接口

這是更好的方法。因為它使代碼更portable,使應用程式更易于maintenance,它允許Connection對象既參與分布式事務管理,又對連接配接池透明。

當你的應用程式首要考慮的是性能時,連接配接池時一個great idea。

DataSource接口利用JNDI(the Java Naming and Directory Interface)來存儲資料源的邏輯名。

使用JNDI,java應用程式能通過邏輯名來找到遠端資料庫服務。

要使用邏輯名,必須首先在JNDI命名伺服器中注冊其邏輯名。

現在,大多數應用伺服器在啟動時就注冊了它們配置的資料源。

VendorDataSource vdsDataSource new VendorDataSource();

vdsDataSource.setServerName("localhost");

vdsDataSource.setDatabaseName("mydb");

vdsDataSource.setDescription("example mydb database");

//Get the initial context

Context ctx=new InitialContext();

//Create the logical name for the data source

ctx.bind("jdbc/mydb", vdsDataSource);