开发者社区> 数据库> 正文


简介: 版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1554793 JDK6...
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1554793


1、Java开发者使用JDBC4 API可以做下面的事:
1) Connect to a data source
2) Execute complex SQL statements
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.
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.
1) 通过DriverManager类
String sURL="jdbc:derby//localhost:9527/wrox";
String sUsername="sa";
String sPassword="password";

 //Obtain a connection
 Connection cConn=DriverManager.getConnection(sURL, sUsername, sPassword);
   cConn.close(); //Close the connection

2) 通过执行DataSource接口
当你的应用程序首要考虑的是性能时,连接池时一个great idea。
DataSource接口利用JNDI(the Java Naming and Directory Interface)来存储数据源的逻辑名。
VendorDataSource vdsDataSource new VendorDataSource();
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);

版权声明:本文首发在云栖社区,遵循云栖社区版权声明:本文内容由互联网用户自发贡献,版权归用户作者所有,云栖社区不为本文内容承担相关法律责任。云栖社区已升级为阿里云开发者社区。如果您发现本文中有涉嫌抄袭的内容,欢迎发送邮件至:developer2020@service.aliyun.com 进行举报,并提供相关证据,一经查实,阿里云开发者社区将协助删除涉嫌侵权内容。

+ 订阅