`
810364804
  • 浏览: 773355 次
文章分类
社区版块
存档分类
最新评论

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 from db2admin.employee where empno = :str1 };
System.out.println("Received results:");
while (true) {
#sql { FETCH :cursor2 INTO :str2 };
if (cursor2.endFetch()) break;
System.out.print (" empno= " + str1);
System.out.print (" firstname= " + str2);
System.out.print ("");
}
cursor2.close();
// rollback the update
System.out.println("Rollback the update...");
#sql { ROLLBACK work };
System.out.println("Rollback done.");
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
注:本程序采用JDBCODBC桥的方式访问数据库,必须配置ODBC数据源。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics