一个将SQL语句嵌入到Java应用程序中的实例

数据库
本文我们主要介绍了一个嵌入式SQL(SQLJ)的创建实例,通过实例让我们来一起了解一下嵌入式SQL(SQLJ)的应用,希望能够对您有所帮助。

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

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

格式: # SQL{ SQL语句 } 。

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

语法: SQLJ 源文件名。

实例如下:

 

  1. import java.sql.*;  
  2.  
  3. import sqlj.runtime.*;  
  4.  
  5. import sqlj.runtime.ref.*;  
  6.  
  7. #sql iterator App_Cursor1 (String empno, String firstnme) ;  
  8.  
  9. #sql iterator App_Cursor2 (String) ;  
  10.  
  11. class App  
  12.  
  13. {  
  14.  
  15. static  
  16.  
  17. {  
  18.  
  19. try  
  20.  
  21. {  
  22.  
  23. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();  
  24.  
  25. }  
  26.  
  27. catch (Exception e)  
  28.  
  29. {  
  30.  
  31. e.printStackTrace();  
  32.  
  33. }  
  34.  
  35. }  
  36.  
  37. public static void main(String argv[])  
  38.  
  39. {  
  40.  
  41. try  
  42.  
  43. {  
  44.  
  45. App_Cursor1 cursor1;  
  46.  
  47. App_Cursor1 cursor2;  
  48.  
  49. String str1 = null;  
  50.  
  51. String str2 = null;  
  52.  
  53. int count1;  
  54.  
  55. Connection con = null;  
  56.  
  57. String url = "jdbc:odbc:tese2";  
  58.  
  59. DefaultContext ctx = DefaultContext.getDefaultContext();  
  60.  
  61. if (ctx == null) {  
  62.  
  63. try {  
  64.  
  65. if (argv.length == 0) {  
  66.  
  67. String userid ="tdl";  
  68.  
  69. String passwd ="user";  
  70.  
  71. con = DriverManager.getConnection(url, userid, passwd);  
  72.  
  73. }  
  74.  
  75. else if (argv.length == 2) {  
  76.  
  77. // connect with default id/password  
  78.  
  79. con = DriverManager.getConnection(url);  
  80.  
  81. }  
  82.  
  83. else {  
  84.  
  85. System.out.println("Usage: java App [username password]");  
  86.  
  87. System.exit(0);  
  88.  
  89. }  
  90.  
  91. con.setAutoCommit(false);  
  92.  
  93. ctx = new DefaultContext(con);  
  94.  
  95. }  
  96.  
  97. catch (SQLException e) {  
  98.  
  99. System.out.println("Error: could not get a default context");  
  100.  
  101. System.err.println(e) ;  
  102.  
  103. System.exit(1);  
  104.  
  105. }  
  106.  
  107. DefaultContext.setDefaultContext(ctx);  
  108.  
  109. }  
  110.  
  111. #sql cursor1 = { SELECT empno, firstnme from db2admin.employee };  
  112.  
  113.  
  114. System.out.println("Received results:");  
  115.  
  116. while (cursor1.next()) {  
  117.  
  118. str1 = cursor1.empno();  
  119.  
  120. str2 = cursor1.firstnme();  
  121.  
  122. System.out.print (" empno= " + str1);  
  123.  
  124. System.out.print (" firstname= " + str2);  
  125.  
  126. System.out.print ("");  
  127.  
  128. }  
  129.  
  130. cursor1.close();  
  131.  
  132. #sql cursor2 = { SELECT firstnme from db2admin.employee where empno = :str1 };  
  133.  
  134. System.out.println("Received results:");  
  135.  
  136. while (true) {  
  137.  
  138. #sql { FETCH :cursor2 INTO :str2 };  
  139.  
  140. if (cursor2.endFetch()) break;  
  141.  
  142. System.out.print (" empno= " + str1);  
  143.  
  144. System.out.print (" firstname= " + str2);  
  145.  
  146. System.out.print ("");  
  147.  
  148. }  
  149.  
  150. cursor2.close();  
  151.  
  152. // rollback the update  
  153.  
  154. System.out.println("Rollback the update...");  
  155.  
  156. #sql { ROLLBACK work };  
  157.  
  158. System.out.println("Rollback done.");  
  159.  
  160. }  
  161.  
  162. catch( Exception e )  
  163.  
  164. {  
  165.  
  166. e.printStackTrace();  
  167.  
  168. }  
  169.  
  170. }  
  171.  

 

注:本程序采用JDBCODBC桥的方式访问数据库,必须配置ODBC数据源。

关于嵌入式SQL(SQLJ)的知识就介绍到这里了,希望本次的介绍能够给您带来一些收获!

【编辑推荐】

  1. Oracle数据库中Constraint约束的四对属性
  2. SQL Server 2005无法连接到本地服务器的解决
  3. Linux下重新配置MySQL数据库引擎innodb的过程
  4. Navicat MySQL连接Linux下MySQL的问题解决方案
  5. SQL Server 2000在Windows7 旗舰版中的安装配置
责任编辑:赵鹏 来源: CSDN博客
相关推荐

2011-06-09 09:31:40

Qt 实例

2012-11-01 11:34:31

IBMdw

2011-05-11 10:58:39

iOS

2011-03-10 10:45:47

Azure“Hello Worl

2011-03-15 19:45:27

Windows Azu

2012-12-07 10:15:53

IBMdW

2010-08-13 13:05:30

Flex应用程序

2011-07-26 13:23:14

iPhone 图片 相册

2023-05-19 08:49:58

SQLAlchemy数据库

2013-01-11 14:45:43

iOS开发移动应用iPhone

2013-05-13 09:31:29

Web App开发WebApp

2022-10-21 14:21:46

JavaScript笔记技能

2009-10-09 17:01:32

VB.NET多线程

2010-06-28 14:13:18

SQL Server实

2009-10-19 14:14:19

OSGi Web应用

2021-07-14 17:39:46

ReactRails API前端组件

2023-09-21 08:00:00

ChatGPT编程工具

2020-10-11 20:54:39

Python开发Docker

2011-03-14 15:10:10

AzureFacebook

2022-02-15 09:36:13

容器应用程序云服务
点赞
收藏

51CTO技术栈公众号