成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一個將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

2010-08-13 13:05:30

Flex應用程序

2011-07-26 13:23:14

iPhone 圖片 相冊

2023-05-19 08:49:58

SQLAlchemy數據庫

2012-12-07 10:15:53

IBMdW

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多線程

2009-10-19 14:14:19

OSGi Web應用

2020-10-11 20:54:39

Python開發Docker

2021-07-14 17:39:46

ReactRails API前端組件

2023-09-21 08:00:00

ChatGPT編程工具

2010-06-28 14:13:18

SQL Server實

2009-07-28 10:11:06

ASP.NET應用程序

2022-02-15 09:36:13

容器應用程序云服務
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲成人播放器 | 久久婷婷色 | 一区二区在线 | 欧洲av在线 | www.夜夜骑.com | 国产精品96久久久久久 | 理论片免费在线观看 | 欧美一级大片免费看 | 国产激情在线观看视频 | 亚洲一区二区在线 | 国产小视频在线观看 | 精品国产欧美一区二区三区成人 | 亚洲精品欧美一区二区三区 | 国产午夜精品久久久久免费视高清 | 91国产在线播放 | 欧美日韩视频在线第一区 | 天天综合久久 | 操网站| 午夜羞羞 | 中文字幕不卡 | 欧美激情综合五月色丁香小说 | 天天玩天天干天天操 | 精品乱子伦一区二区三区 | 爱爱无遮挡 | 久久9视频| 亚洲+变态+欧美+另类+精品 | 欧美成人精品一区二区三区 | 色天天综合| 日韩视频在线免费观看 | 欧美精品91 | 亚洲成年在线 | 中文字幕av在线 | 一区视频在线 | 天堂va在线观看 | 日本国产精品视频 | 亚洲一区影院 | 亚洲av毛片 | 人人干超碰 | 视频一区在线观看 | 日韩成人av在线 | 狠狠干美女 |