设为首页 加入收藏

TOP

DB2数据库、事务控制语言、系统控制语言、函数、嵌入式SQL(SQLJ) (一)
2014-11-23 21:58:54 来源: 作者: 【 】 浏览:8
Tags:DB2 数据库 事务 控制 语言 系统 函数 嵌入式 SQL SQLJ

事务控制语言

1.事务提交命令: Commit;

2.事务回退命令: Rollback;

系统控制语言

1. 取消自动提交:

Update command options using c off;

2. 连接数据库

Connect to 数据库名user 用户using 密码

3. 断开数据库连接:

Connect reset

Disconnect 数据库名

4. 列出数据库中的所有表:

List tables for all

5. 列出数据库中的模式名为schema_name的所有表:

List tables for schema schema_name

6.查看表结构

Describe table 模式名.表名

Describe select * from 模式名.表名

7.查看表的索引

Describe indexes for table 模式名.表名

函数

(一) 列函数

列函数对列中的一组值进行运算以得到单个结果值。

1.AVG

返回某一组中的值除以该组中值的个数的和

2.COUNT (*)

返回非空列值的行数。

3.MAX

返回一组值中的最大值

4.MIN

返回一组值中的最小值

5.MOD

求余

(二) 标量函数

标量函数对值进行某个运算以返回另一个值。下列就是一些由DB2通用数据库提供的标量函数的示例。

1.ABS

返回数的绝对值

2.HEX

返回值的十六进制表示

3.LENGTH

返回自变量中的字节数(对于图形字符串则返回双字节字符数。)

4.YEAR

抽取日期时间值的年份部分

5.NULLIF(a,b)

如果a=b则值为空,否则值为a

6.COALESCE(a,b,c)

:返回第一个具有非空值的参数的值

7.UCASE(str)

小写字符转换成大写字符

8.ICASE(str)

大写字符转换成小写字符

9.LOCAT(str1,str2,n)

返回从第n个字符起,在str1中str2第一次出现的位置

10.SUBSTR(str,m,n)

返回从第m个字符起,,在str中的n个字符串

嵌入式SQL(SQLJ)

将SQL语句嵌入应用程序时,必须按以下步骤预编译应用程序并将其与数据库联编:

1.创建源文件,以包含带嵌入式SQL 语句的程序

格式: # SQL{ SQL语句} 。

2.连接数据库,然后预编译每个源文件。

语法: SQLJ 源文件名

例:

import java.sql.*;

import sqlj.runtime.*;

import sqlj.runtime.ref.*;

#sql iterator App_Cursor1 (String empno, String firstnme) ;

#sql iterator App_Cursor2 (String) ;

class App

{

static

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

}

catch (Exception e)

{

e.printStackTrace();

}

}

public static void main(String argv[])

{

try

{

App_Cursor1 cursor1;

App_Cursor1 cursor2;

String str1 = null;

String str2 = null;

int count1;

Connection con = null;

String url = "jdbc:odbc:tese2";

DefaultContext ctx = DefaultContext.getDefaultContext();

if (ctx == null) {

try {

if (argv.length == 0) {

String userid ="tdl";

String passwd ="user";

con = DriverManager.getConnection(url, userid, passwd);

}

else if (argv.length == 2) {

// connect with default id/password

con = DriverManager.getConnection(url);

}

else {

System.out.println("Usage: java App [username password]");

System.exit(0);

}

con.setAutoCommit(false);

ctx = new DefaultContext(con);

}

catch (SQLException e) {

System.out.println("Error: could not get a default context");

System.err.println(e) ;

System.exit(1);

}

DefaultContext.setDefaultContext(ctx);

}

#sql cursor1 = { SELECT empno, firstnme from db2admin.employee };

System.out.println("Received results:");

while (cursor1.next()) {

str1 = cursor1.empno();

str2 = cursor1.firstnme();

System.out.print (" empno= " + str1);

System.out.print (" firstname= " + str2);

System.out.print ("");

}

cursor1.close();

#sql cursor2 = { SELECT firstnme f

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇性能比较工具runstats 下一篇DB2在线备份

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: