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

公司主營業(yè)務(wù):網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出攸縣免費(fèi)做網(wǎng)站回饋大家。
1.創(chuàng)建源文件,以包含帶嵌入式SQL語句的程序。
格式: # SQL{ SQL語句 } 。
2.連接數(shù)據(jù)庫,然后預(yù)編譯每個(gè)源文件。
語法: SQLJ 源文件名。
實(shí)例如下:
- 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橋的方式訪問數(shù)據(jù)庫,必須配置ODBC數(shù)據(jù)源。
關(guān)于嵌入式SQL(SQLJ)的知識就介紹到這里了,希望本次的介紹能夠給您帶來一些收獲!
【編輯推薦】
- Oracle數(shù)據(jù)庫中Constraint約束的四對屬性
- SQL Server 2005無法連接到本地服務(wù)器的解決
- Linux下重新配置MySQL數(shù)據(jù)庫引擎innodb的過程
- Navicat MySQL連接Linux下MySQL的問題解決方案
- SQL Server 2000在Windows7 旗艦版中的安裝配置
分享名稱:一個(gè)將SQL語句嵌入到Java應(yīng)用程序中的實(shí)例
轉(zhuǎn)載源于:http://m.fisionsoft.com.cn/article/djogoge.html


咨詢
建站咨詢
