事务控制语言
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数据源。
分享到:
相关推荐
连接db2数据库的jdbc驱动jar包,用于开发java应用程序或web应用程序
Java中的嵌入式SQL技术:SQLJ.pdf
CALL SQLJ.INSTALL_JAR('file:/tmp/avalanche/db2_regex/lib/db2_regex.jar', db2_regex); 5- In a DB2 command line processor window, run : \>db2 connect to <my_db> user <uid> using \>db2 set ...
there is guide line for develpoer for how to use the complex sqlj sentense.
db2的简单jdbc连接,嵌入式SQLJ的使用,存储过程,UDF,触发器,大对象的调用,db2锁,序列的使用.
db2jcc4.jar-v11.5.6_jdbc_sqlj.tar.gz
SQL server 数据库驱动 SQL server 数据库驱动
基于Oracle数据库的JDBC与SQLJ的研究.pdf
jdbc_sqlj , 自己去IBM官方注册下载下来的,绝对正版。。可以使用,快来下载吧! db2_db2driver_for_jdbc_sqlj版本官网安装程序包
JAVA SQLJ 开发指南
2.1 数据库系统 2.1.1 SQL数据库数据库基本操作语句 2.1.2 Microsoft SQL Server数据库设计及ODBC配置 2.1.3 Oracle 数据库的基本说明及其使用简介 2.1.4 MySQL的安装及使用简介 2.2 使用JDBC连接数据库 2.2.1...
SQLJ:是一个JAVA预编译器,它可以将内嵌的SQL语句转化为JAVA语句.SQLJ的使用与运行机理与其它ORACLE的与编译器类似。实际上,为了使我们形象的记住SQLJ提供的功能,我们也可以直接将SQLJ改名为Pro*Java。
NULL 博文链接:https://lspgdut.iteye.com/blog/1931861
4) SQLJ: 很新的一种用Java访问Oracle数据库的方法,会的人不多; 5) JDBC 6) PL/SQL: 存储在数据库内运行, 其他方法为在数据库外对数据库访问,只适合ORACLE; 2. PL/SQL 1) PL/SQL(Procedual language/SQL)...
SQLJ知识演示文稿
:star: 特征支持用分号分隔的多个sql语句支持选择,删除,更新和插入类型支持放置,截断和重命名命令输出具有相应权限的sql访问的表和列列表支持各种数据库引擎 :party_popper: 安装来自npmjs npm install node-sql...
它支持运行使用了嵌入式 SQL、ODBC、CLI、JDBC、SQLJ、.NET、OLE DB、PHP、Ruby、CLPPLus 和 CLP 接口的应用程序。对于全面了解客户机程序包的内容并且需要其他功能部件的客户,此客户机程序包是其首选 IBM 数据...