热门文章
阿标在线 动力3.62HTML生成3.62网站文件说明
动力3.62整合动网7.0 SP2插
MDAC2.8 下载!
动力3.62版 防止垃圾留言
动力3.6全方位改动方法
让3.62不同频道实现不同风
把3.62首页登陆为横向代码
动易3.6首页随机FLASH修改
362首页和文章频道页图文幻
个性化修改3.6宝典
3.62轻易实现网摘功能
如何正确统计中文字数?
弹出JAVASCRIPT语法错误对
后台使“网站顶部LOGO地址
最新图片文章横向移动的修
html 生成艺术字
3.6 Sp2 Logo和Banner及广
日期值的计算
汉字转拼音
首页“图片更新”图片滚动
简体中文转换为繁体中文的
如何在css中定义链接的下划
DB2中SQLJ应用示例
[ 录入:阿标 | 点击数: | 更新时间:2005-3-18 12:56:00]
JSPCN主页 | 虚拟主机 | 技术文章 | 代码下载 | 技术论坛 | 我要留言 | 关于我们 | 友情链接 | 联系我们
做最专业的JSP中文网站 当前位置:首页--文章分类--JSP技术--数据库
文章搜索:
关键字 标题 内容
JSPCN文章目录分类
上传问题 [8] 时间相关 [3]
文件操作 [58] STRUTS [61]
中文问题 [16] 数据库 [115]
JAVAMAIL [36] 开发工具 [8]
JSP基础 [127] JSP其他 [40]
教程系列 [87] JAVABEAN [5]
图片声音 [8] JSP实例 [22]
JSP配置 [54] Java API [20]
考试相关 [38] APPLET [57]
JAVA类 [113] Application [6]
Swing [11] J2EE [108]
异常处理 [11] Servlet [33]
JAVA基础 [166] JAVA实例 [56]
JAVA网络 [38] EJB [145]
声音图片 [27] XML [80]
JAVA线程 [38] J2ME [65]
SUN [89] B/S其他 [3]
B/S开发 [12] 原子代码 [3]
代码收集 [6] RESIN [21]
JBOSS [11] weblogic [34]
eclipse [12] Tomcat [60]
Jbuilder [29] MYSQL [19]
ORACLE [46] sql server [4]
WWW服务 [2] 邮件服务 [4]
热点文章链接
JSP数据库连接池的必要性 [5543]
Tomcat4的数据库连接池配置 [5371]
数据库连接池Java实现小结 [4668]
jsp连接数据库大全 [3441]
一种简单JDBC连接池的实现 [2447]
通过JDBC连接oracle数据库的十大技巧 [2243]
用连接池提高Servlet访问数据库的效率 [1681]
RESIN/TOMCAT和MYSQL的连接解决方法 [1294]
JSP数据库操作例程 [865]
JSP数据库连接大全 [603]
利用weblogic的POOL(连接池)连接数据库 [316]
通过JDBC连接oracle数据库的十大技巧 [277]
Jsp中调用Oracle存储过程的小例子 [195]
用Java实现数据库应用系统 [11]
全面解析JDBC(一) [9]
相关文章链接
DB2中SQLJ应用示例
作者:未知 文章来源:www.jspcn.net
访问次数: 次 加入时间:2005-01-19
// 本例展示怎样写用JDBC应用驱动程序存取DB2数据库的SQLJ应用。
// 其中bank为DB2中的数据库,customer为bank中的表
import java.sql.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
#sql iterator App_Cursor1 (String customer_id, String customer_name) ;
#sql iterator App_Cursor2 (String) ;
class App
{
static
{
try
{
// 用 DriverManager 注册驱动程序
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String argv[])
{
try
{
App_Cursor1 cursor1;
App_Cursor2 cursor2;
String str1 = null;
String str2 = null;
int count1;
Connection con = null;
// URL的格式为:jdbc:db2:数据库名
String url = "jdbc:db2:bank";
DefaultContext ctx = DefaultContext.getDefaultContext();
if (ctx == null) {
try {
if (argv.length == 0) {
// 用默认的用户名和口令连接
con = DriverManager.getConnection(url);
}
else if (argv.length == 2) {
String userid = argv[0];
String passwd = argv[1];
// 用用户提供的用户名和口令连接
con = DriverManager.getConnection(url, userid, passwd);
}
else {
System.out.println(" 用法: java SqljApp [用户名 口令] ");
System.exit(0);
}
con.setAutoCommit(false);
ctx = new DefaultContext(con);
}
catch (SQLException e) {
System.out.println("错误: 不能得到默认内容");
System.err.println(e) ;
System.exit(1);
}
DefaultContext.setDefaultContext(ctx);
}
// 从数据库提取数据
System.out.println("从数据库提取一些数据...");
#sql cursor1 = { SELECT customer_id, customer_name from customer };
// 显示结果集
// cursor1.next() 当无更多的行时返回 false
System.out.println("得到的结果:");
while (cursor1.next()) {
str1 = cursor1.customer_id();
str2 = cursor1.customer_name();
System.out.print (" 客户编号= " + str1);
System.out.print (" 客户姓名= " + str2);
System.out.print (" ");
}
cursor1.close();
// 从数据库中获取客户数
System.out.println(" 获取客户表的行数...");
#sql { SELECT count(*) into :count1 from customer };
System.out.println ("客户表中有 " + count1 + " 行.");
// 更新数据
str1 = " 1";
System.out.println(" 更新数据... ");
#sql { UPDATE customer set customer_name = ´黄亮辉´ where customer_id = :str1 };
// 从数据库中提取更新数据
System.out.println(" 从数据库中提取更新数据...");
#sql cursor2 = { SELECT customer_name from customer where customer_id = :str1 };
// 显示结果集
// cursor2.next() 当无更多行时返回 false
System.out.println("得到的结果:");
while (true) {
#sql { FETCH :cursor2 INTO :str2 };
if (cursor2.endFetch()) break;
System.out.print (" 客户编号= " + str1);
System.out.print (" 客户姓名= " + str2);
System.out.print (" ");
}
cursor2.close();
// 回滚更新
System.out.println(" 回滚更新...");
#sql { ROLLBACK work };
System.out.println("回滚完成.");
}
catch( Exception e )
{
e.printStackTrace();
}
}
}