JDK6笔记(5)----JDBC4

简介: JDK6笔记(5)----JDBC4 1、Java开发者使用JDBC4 API可以做下面的事: 1) Connect to a data source 连接数据源 2) Execute complex SQL statements 执行复杂的SQL语句 ...

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

目录
相关文章
|
2月前
|
Java 关系型数据库 MySQL
自动化测试项目实战笔记(一):JDK、Tomcat、MySQL、Jpress环境安装和搭建
这篇文章是关于自动化测试项目实战笔记,涵盖了JDK、Tomcat、MySQL、Jpress环境的安装和搭建过程,以及测试用例和常见问题总结。
59 1
自动化测试项目实战笔记(一):JDK、Tomcat、MySQL、Jpress环境安装和搭建
|
7月前
|
Java
JDK动态代理笔记整理
JDK动态代理笔记整理
|
Java 程序员
终于不慌内卷了,多亏阿里内部的并发图册+JDK源码速成笔记
并发编程 Java并发在近几年的面试里面可以说是面试热点,每个面试官面试的时候都会跟你扯一下并发,甚至是高并发。面试前你不仅得需要弄清楚的是什么是并发,还得搞清什么是高并发! 在这里很多小白朋友就会很疑惑:我工作又不用,为啥面试总是问?真就内卷卷我呗!(手动狗头)互联网内卷已经是现在的行业趋势,而且是不可逆的,这个大家也知道;但LZ要说的是,虽然简单地增删改查并不需要并发的知识,但是业务稍微复杂一点,你的技术水平稍微提升一点的话你就会知道,并发是我们Java程序员绕不开的一道坎。
52 0
Alibaba新产:“Java并发笔记”闪耀来袭,JDK源码奥义尽在其中
JDK是Java语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。 没有JDK的话,无法编译Java程序(指java源码.java文件),如果想只运行Java程序(指class或jar或其它归档文件),要确保已安装相应的JRE。
直击灵魂!美团大牛手撸并发原理笔记,由浅入深剖析JDK源码
并发编程这四个字想必大家最近都在网上看到过有很多的帖子在讨论。我们都知道并发编程可选择的方式有多进程、多线程和多协程。在Java中,并发就是多线程模式。而多线程编程也一直是一个被广泛而深入讨论的领域。如果遇到复杂的多线程编程场景,大多数情况下我们就需要站在巨人的肩膀上利用并发编程框架——JDK Concurrent包来解决相关线程问题。
|
Java 开发者 容器
先到先学!Alibaba甩出第四次更新的JDK源码高级笔记(终极版)
作为Java开发者,面试肯定被问过多线程。对于它,大多数好兄弟面试前都是看看八股文背背面试题以为就OK了;殊不知现在的面试官都是针对一个点往深了问,你要是不懂其中原理,面试就挂了。可能你知道什么是进程什么是线程,但面试官要是问你进程之间是如何通讯的?ConcurrentHashMap 和 HashTable有什么区别?为什么wait和notify方法要在同步块代码中调用?你答不上来就只能等通知了。。。
|
Java Linux
【笔记03】CentOS 安装 jdk
(1) 创建 softwarezgq 目录,用于存放软件安装包
144 0
【笔记03】CentOS 安装 jdk
|
SQL Java 数据库连接
JDK6笔记(5)----JDBC4
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1554793 JDK6...
1005 0
|
SQL Java 数据库连接
JDK6笔记(5)----JDBC4(2)
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1555630 JDK6笔记(5)----JDBC4(2) 1、理解Statements有三种类型的Statements:1)Statements接口它通常用于只需不带参数的SQL语句。
664 0
|
SQL Java 数据库连接
JDK6笔记(5)----JDBC4(3)
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1556016 JDK6笔记(5)----JDBC4(3) 1、预准备语句的IN参数的pitfall当你用setter方法向IN参数传递值时要注意一些问题。
789 0