没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
Advanced database operations with
JDBC
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. About this tutorial....................................................... 2
2. Application design...................................................... 4
3. Database creation...................................................... 6
4. Prepared statements .................................................. 10
5. Callable statements.................................................... 12
6. Advanced datatypes ................................................... 18
7. Summary ................................................................ 24
Advanced database operations with JDBC Page 1 of 26
Section 1. About this tutorial
Should I take this tutorial?
This tutorial is designed to introduce you to several advanced database operations, including
stored procedures and advanced datatypes, that can be performed by a Java application
using JDBC. The example code in this tutorial was written to work with DB2 Universal
Database 7.2, but modifying the code to work with other databases is trivial due to the use of
a DataSource object.
This tutorial assumes that you are already familiar with the Java programming language and,
to some extent, JDBC. To fully leverage some of the material contained in this tutorial we
recommend that you complete the tutorial Managing database connections with JDBC. The
links in Resources on page 24 include referrals to additional information on JDBC.
What is this tutorial about?
This tutorial demonstrates how to perform advanced operations with a database using JDBC.
It focuses on more advanced database functionality, including database design, prepared
statements, stored procedures, and advanced datatypes.
The tutorial begins with a discussion of the design of a fictitious message board system.
Topics include creating the tables, populating them with data, and using a DataSource
object to ensure maximum portability between databases.
Next, the PreparedStatement object is introduced along with examples to show how it
simplifies certain types of application development, including the population of tables.
The topic of stored procedures is then broached, from how to create Java stored procedures,
to the proper handling of stored procedures from an application using the JDBC
CallableStatement.
After this, the Blob and Clob advanced datatypes are introduced along with several
examples that demonstrate their use.
Tools
Numerous code snippets are provided to help you make the transition from theory to
practical use. To actively work through these examples, the following tools need to be
installed and working correctly:
* A text editor: Java source files are simply text, so to create and read them, all you need
is a text editor. If you have access to a Java IDE, you can also use it, but sometimes
IDEs hide too many of the details.
* A Java development environment, such as the Java 2 SDK, which is available at
http://java.sun.com/j2se/1.4/. The Java 2 SDK, Standard Edition version 1.4, includes
the JDBC standard extensions as well as JNDI, which are both necessary for some of
the latter examples in the book.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Advanced database operations with JDBC Page 2 of 26
* An SQL-compliant database: The examples in this tutorial use a wide variety of different
databases to help demonstrate how database independent JDBC programming can be.
Resources on page 24 contains links to more information on both JDBC and databases.
In particular, the examples in this tutorial have been tested with DB2 running on a
Windows 2000 server, but because they are written using a DataSource object, they
should easily convert to other databases.
* A JDBC driver: Because the JDBC API is predominantly composed of interfaces, you
need to obtain an actual JDBC driver implementation to make the examples in this
tutorial actually work. The examples in this tutorial use advanced JDBC functionality,
and, therefore, they require an advanced JDBC driver. Most database and JDBC driver
vendors will supply you with an evaluation version of a particular JDBC driver.
About the author
Robert J. Brunner is an astrophysicist by day and a computer hacker by night. His principal
employer is the California Institute of Technology, where he works on knowledge extraction
and data-mining from large, highly distributed astronomical databases. He has provided
consulting to scientific data centers around the world, provided Java and XML training to a
variety of groups, and is currently working on the book Enterprise Java Database
Programming which will be published by Addison Wesley in 2002. Feel free to contact him
via e-mail at rjbrunner@pacbell.net.
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Advanced database operations with JDBC Page 3 of 26
Section 2. Application design
Database design
This tutorial uses examples based on a fictitious
message board application designed to allow
multiple authors to post messages, as well as a
searchable message digest that summarizes the
status of the message board. In a simplified
overview of the application, there are three distinct
entities:
* A message class
* An author class
* A digest class
Each of these entities will have its own distinct
table in the database. This schema is captured in
the class diagram to the left, which shows the three
classes and their respective attributes.
Initializing the database connection
The code examples in this tutorial use a DataSource object to obtain a connection to the
target database. By default, the target database is DB2 UDB. The first task is to initialize the
DB2 DataSource object and make it available to other applications, which is done using the
JNDI filesystem context provider (see Resources on page 24 for additional information on
DataSource objects and JNDI).
Ideally, you initialize a DataSource object using an administration tool, but it isn't overly
difficult to do manually as the following code snippets demonstrate. The full example code is
available from Resources on page 24 . To demonstrate how easy it is to change databases,
the parts of the code that are database dependent have been marked in boldface.
To begin the connection process, the relevant classes are imported. This includes the actual
DataSource implementation used (in this case, the DB2 JDBC DataSource provided by
IBM), as well as the relevant classes for the JNDI naming interface. To use the filesystem
context provider, download the appropriate jar file (again, see Resources on page 24 for
details).
package com.persistentjava;
import COM.ibm.db2.jdbc.DB2DataSource ;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;
import java.sql.* ;
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Advanced database operations with JDBC Page 4 of 26
import javax.sql.DataSource;
In the following code segment, one single class is used to create the DataSource object
and bind it to a name later used to materialize the object from the JNDI filesystem context
provider. In this example, the code to initialize the database is made dependent (only in that
db2 is part of the name), but in general the name doesn't need to change, only the actual
object bound to the name -- this is the power of name services. Think how DNS works; the
actual machine to which you connect might change, but the name stays the same. One last
point to note is the last line of code shown below; its purpose is to first unbind (or delete) any
object previously bound to the DataSource name. While not necessary, this is a good
practice because it is a form of resource deallocation.
public class InitializeDB2 {
public static void main(String argv[]) {
String fsName = "jdbc/pjtutorial/db2";
try{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
Context ctx = new InitialContext(env);
ctx.unbind(fsName) ;
Creating the DataSource object
To enable the magic of JNDI, we first need to create the DataSource object and set all
relevant properties. There is a standard list of properties that a DataSource object must
take, as well as several optional (non-essential) properties. A GUI tool could discover these
extra properties through reflection and query the user appropriately. Once the DataSource
object has been created and initialized, the next step is to bind it to the appropriate name.
DB2DataSource ds = new DB2DataSource();
ds.setDescription("DB2 DataSource");
ds.setServerName("persistentjava.com");
ds.setPortNumber(6789);
ds.setDatabaseName("jdbc");
ctx.bind(fsName, ds) ;
ctx.close() ;
} catch (Exception e) {
e.printStackTrace();
}
}
}
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Advanced database operations with JDBC Page 5 of 26
剩余25页未读,继续阅读
nanhai_li
- 粉丝: 3
- 资源: 151
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- database-project-massunaAi笔记
- 2%EF%BC%9A%E9%99%95%E8%A5%BF%E
- yyspdz62_944.apk
- SAP公司间采购EDI配置-如何触发自动MIRO.docx
- python197基于图像识别的仪表实时监控系统.rar
- python196基于循环神经网络的情感分类系统.rar
- I2C驱动SHT30温湿度传感器和LCD12864使用例程(RSCG12864B)
- python193中学地理-中国的江河湖泊教学网(django).rar
- python191基于时间序列分析的大气污染预测软件(django).rar
- python190基于人脸识别智能化小区门禁管理系统.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0