首页 > 软件测试/ 正文
一个将SQL语句嵌入Java应用程序的实例
2012-09-14 09:18:56 ℃我们在将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数据源。
- 上一篇:Java的内部类和匿名类剖析
- 下一篇:Java线程:线程的调度-让步
更多郑州北大青鸟2021年开班是什么时候,郑州java软件开发培训机构正规的,郑州北大青鸟java培训机构怎么样呢,郑州软件编程培训机构哪家专业靠谱,郑州java专业培训学校机构专业靠谱的,郑州java培训班哪家好相关文章
- 游戏测试是什么
- 什么是软件测试架构
- 从瀑布开发走向敏捷开发模式下的自动化测试(3)
- 找到测试的敏捷点
- 学习让测试更精彩,测试让生命更精彩
- 探秘软件质量保证的五大法则
- 软件质量保证的误区
- 质量体系建立的步骤
- 自动化界面测试脚本质量保障
- SuperOneClick2.3.3使用教程,SuperOneClick2.3.3教程下载
- 什么是水货笔记本
- 计算机发展简史
- 关闭QoS可令网速提升20%是荒谬之说
- 教你怎么看笔记本电脑配置!
- BIOS和CMOS的联系与区别
- 什么是镜像,及其用途?
- 一键还原和重装系统有什么不同
- 一体机电脑好不好?贵吗?
- 打开任务管理器快捷键
- 快播是什么软件,快播是干嘛的
- IT产业中的摩尔定律
- 机械键盘和薄膜式键盘的区别
- 郑州有没有软件工程师培训呢
- 软件测试工程师从业者需要走哪些路?
- 初中生学软件开发专业能改变命运吗
- 软件测试适合初中毕业的男生去学吗
- 郑州电脑学校介绍软件开发工程师专业前景展望
- 2016年最热门 IT 职业岗位有哪些呢?
- 郑州2018最新Java培训机构排名
- 漯河有没有java培训机构呢
- 搜索
-
- 热门标签