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

一個將SQL語句嵌入到Java應(yīng)用程序中的實(shí)例

數(shù)據(jù)庫
本文我們主要介紹了一個嵌入式SQL(SQLJ)的創(chuàng)建實(shí)例,通過實(shí)例讓我們來一起了解一下嵌入式SQL(SQLJ)的應(yīng)用,希望能夠?qū)δ兴鶐椭?/div>

我們在將SQL語句嵌入應(yīng)用程序時,必須按以下的兩個步驟預(yù)編譯應(yīng)用程序并將其與數(shù)據(jù)庫聯(lián)編,步驟如下:

1.創(chuàng)建源文件,以包含帶嵌入式SQL語句的程序。

格式: # SQL{ SQL語句 } 。

2.連接數(shù)據(jù)庫,然后預(yù)編譯每個源文件。

語法: SQLJ 源文件名。

實(shí)例如下:

 

  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橋的方式訪問數(shù)據(jù)庫,必須配置ODBC數(shù)據(jù)源。

關(guān)于嵌入式SQL(SQLJ)的知識就介紹到這里了,希望本次的介紹能夠給您帶來一些收獲!

【編輯推薦】

  1. Oracle數(shù)據(jù)庫中Constraint約束的四對屬性
  2. SQL Server 2005無法連接到本地服務(wù)器的解決
  3. Linux下重新配置MySQL數(shù)據(jù)庫引擎innodb的過程
  4. Navicat MySQL連接Linux下MySQL的問題解決方案
  5. SQL Server 2000在Windows7 旗艦版中的安裝配置
責(zé)任編輯:趙鵬 來源: CSDN博客
相關(guān)推薦

2011-06-09 09:31:40

Qt 實(shí)例

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應(yīng)用程序

2023-05-19 08:49:58

SQLAlchemy數(shù)據(jù)庫

2011-07-26 13:23:14

iPhone 圖片 相冊

2012-12-07 10:15:53

IBMdW

2013-01-11 14:45:43

iOS開發(fā)移動應(yīng)用iPhone

2013-05-13 09:31:29

Web App開發(fā)WebApp

2022-10-21 14:21:46

JavaScript筆記技能

2009-10-09 17:01:32

VB.NET多線程

2020-10-11 20:54:39

Python開發(fā)Docker

2023-09-21 08:00:00

ChatGPT編程工具

2021-07-14 17:39:46

ReactRails API前端組件

2009-10-19 14:14:19

OSGi Web應(yīng)用

2010-06-28 14:13:18

SQL Server實(shí)

2009-07-28 10:11:06

ASP.NET應(yīng)用程序

2022-02-15 09:36:13

容器應(yīng)用程序云服務(wù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 欧美va大片 | 午夜久久久久久久久久一区二区 | 国产精品久久久久久52avav | 69性欧美高清影院 | 国产亚洲精品91 | 中文字幕在线视频一区二区三区 | 久久黄色精品视频 | 欧美日韩亚洲视频 | 狠狠av| 色综合色综合色综合 | 欧美精品在线免费观看 | 中文字幕av色 | 中文字幕av中文字幕 | 国产美女自拍视频 | 欧美大片一区 | 中文字幕成人av | 一级女毛片| 免费成人高清 | 亚洲欧美在线一区 | 亚洲美女网站 | 欧美8一10sex性hd| 欧美午夜一区 | 日韩欧美视频免费在线观看 | 中文字幕欧美一区 | 久在线视频播放免费视频 | 欧美精品91 | 色婷婷av777 av免费网站在线 | 色www精品视频在线观看 | wwwww在线观看| 国产乱肥老妇国产一区二 | 亚洲一区黄色 | 亚洲午夜精品一区二区三区他趣 | 国产日韩欧美 | 99热热热热| 日韩一区二区三区在线看 | 国产视频一区在线观看 | 久久宗合色 | 欧美久久一区二区三区 | 日韩一区二区av | 亚洲精彩视频在线观看 | 亚洲精品日日夜夜 |