最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
DB2中SQLJ应用示例
时间:2022-07-02 18:17:17 编辑:袖梨 来源:一聚教程网
// 本例展示怎样写用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();
// 其中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();
相关文章
- 完美世界异环最新消息 2024年上线时间及测试动态 05-14
- 异环鬼火坐骑怎么获取 异环鬼火摩托获取方法与技巧详解 05-14
- 异环粉爪积分获取方法大全 异环粉爪积分快速提升技巧与途径 05-14
- 异环海月 异环海月角色背景、技能解析与实战玩法指南 05-14
- 异环伊洛伊怎么样 异环伊洛伊强度解析与实战表现评价 05-14
- 王者荣耀世界角色强度排行 王者荣耀世界当前版本强势英雄推荐 05-14