• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

DB2中SQLJ应用示例

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网 // 本例展示怎样写用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();
}
}
} 



延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网