Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4130916
  • 博文数量: 626
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 11080
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-23 13:08
文章分类

全部博文(626)

文章存档

2015年(72)

2014年(48)

2013年(506)

分类: Java

2013-09-18 09:35:18

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);

阅读(459) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~