Flex數(shù)據(jù)庫連接的方法集錦
本文和大家重點(diǎn)討論一下連接Flex數(shù)據(jù)庫三種方法,F(xiàn)lex是不能直接連接數(shù)據(jù)庫的,這一點(diǎn)大家需要知道,它只能間接地連接數(shù)據(jù)庫。Flex中提供了三種方式:HttpService,WebService和RemoteObject。
連接Flex數(shù)據(jù)庫三種方法
首先,做一點(diǎn)說明。Flex是不能直接連接數(shù)據(jù)庫的,這一點(diǎn)大家需要知道,它只能間接地進(jìn)行連接Flex數(shù)據(jù)庫。Flex中提供了三種方式:HttpService,WebService和RemoteObject。其中HttpService可以直接獲取XML中的數(shù)據(jù),還可以通過JSP,ASP以及PHP讀取數(shù)據(jù)庫中的數(shù)據(jù),這個(gè)比較簡單,而且網(wǎng)上也有很多例子,我就不多說了。WebService我不懂,請(qǐng)自己查資料。我一直用的是JAVA對(duì)象連接數(shù)據(jù)庫,感覺這個(gè)挺方便,而且J2EE的技術(shù)已經(jīng)很成熟。今天的教程就是以Flex+JAVA+SQLServer獲取數(shù)據(jù)庫公告信息為例簡單說一下RemoteObject的用法。
前提
1.確保你安裝了FlexDataService。這個(gè)對(duì)于單個(gè)CUP無限APP是免費(fèi)的,可以去Adobe下載。如果只是讀取XML文件是不需要這個(gè)的,連接數(shù)據(jù)庫就需要它了。
2.安裝了FlexBuilder或者有FlexSDK。我這里使用的是FlexBuilder(IDE就是方便啊^_^)。
3.安裝了SQLServer數(shù)據(jù)庫。
4.安裝了JRUN或者tomcat或其它的J2EE容器,因?yàn)榘l(fā)布的時(shí)候我們的程序要運(yùn)行在J2EE平臺(tái)上。
5.安裝了JDK。
第一步:創(chuàng)建Flex數(shù)據(jù)庫
這里我們有一個(gè)公告表,表名為Bulletin。結(jié)構(gòu)如下:
字段名稱字段類型說明
ID自動(dòng)編號(hào)自動(dòng)編號(hào)
titleNvarchar(100)題目
datedatatime日期
authorNvarchar(20)作者
contentntext內(nèi)容
在數(shù)據(jù)庫中創(chuàng)建這個(gè)表。保存之后進(jìn)入下一步。#p#
第二步:在JAVA中編寫獲取公告的代碼
首先,我們要?jiǎng)?chuàng)建一個(gè)公告類來專門保存獲取的公告信息,代碼如下。
- NoticeInfo.java
- packagenet.zhuoqun.connectDB;
- importjava.util.Date;
- publicclassNoticeInfo{
- privateStringtitle;//標(biāo)題
- privateStringauthor;//作者
- privateStringcontent;//內(nèi)容
- privateDatedates;//時(shí)間
- publicStringgetAuthor(){
- returnauthor;
- }
- publicvoidsetAuthor(Stringauthor){
- this.author=author;
- }
- ………………//其它get和set方法。
- }
創(chuàng)建好這個(gè)之后我們要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)查詢類:DataServiceImpl.java來查詢Flex數(shù)據(jù)庫,并將查詢結(jié)果傳給將要?jiǎng)?chuàng)建的Flex程序。由于我們不清楚有多少條記錄,所以就借助一下JAVA中的ArrayList這個(gè)類,它位于java.util包中。先創(chuàng)建一個(gè)ArrayList:
ArrayListnoticeList=newArrayList();
查詢數(shù)據(jù)庫之后,每讀取一條記錄就添加到noticeList。
- while(rs.next()){
- NoticeInfotemp=newNoticeInfo();
- temp.setAuthor(rs.getString("author"));
- temp.setContent(rs.getString("content"));
- temp.setDates(rs.getDate("date"));
- temp.setTitle(rs.getString("title"));
- noticeList.add(temp);
- }
查詢完畢之后你就可以把這個(gè)noticeList傳回去,你也可以傳回去一個(gè)NoticeInfo數(shù)組:
- NoticeInfo[]notices=newNoticeInfo[noticeList.size()];
- for(inti=0;i<noticeList.size();i++){
- notices=(NoticeInfo)noticeList.get(i);
- }
- returnnotices;
我這里用的是后一種方法。如果你直接把noticeList傳回去的話,記住一點(diǎn),JAVA的ArrayList類型的對(duì)象到了Flex中會(huì)變成ArrayCollection類型的。
現(xiàn)在JAVA部分的代碼就寫好了。
DataServiceImpl.java的全部代碼如下:
- packagenet.zhuoqun.connectDB;
- importjava.sql.*;
- importjava.util.ArrayList;
- importjava.util.Date;
- publicclassDataServiceImpl{
- privateConnectionconn=null;
- privateStatementstmt=null;
- //以下是數(shù)據(jù)庫以及驅(qū)動(dòng)信息
- publicfinalstaticStringDRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";
- publicfinalstaticStringCONN_STR_PRE="jdbc:microsoft:sqlserver://";
- publicfinalstaticStringHOST_NAME="localhost:1433;";
- publicfinalstaticStringDATABASE_NAME="DatabaseName=mydata";
- publicfinalstaticStringUSERNAME="aaa";
- publicfinalstaticStringPASSWORD="aaa";
- publicDataServiceImpl(){
- }
- //查詢數(shù)據(jù)庫
- privateResultSetexecuteQuery(StringsqlText){
- try{
- Class.forName(DRIVER);
- }catch(ClassNotFoundExceptione){
- e.printStackTrace();
- }
- try{
- conn=DriverManager.getConnection(CONN_STR_PRE+HOST_NAME+DATABASE_NAME,USERNAME,PASSWORD);
- stmt=conn.createStatement();
- ResultSetrs=stmt.executeQuery(sqlText);
- returnrs;
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- returnnull;
- }
- //查詢公告.這個(gè)是本程序的關(guān)鍵代碼
- publicNoticeInfo[]getNotices(){
- ArrayListnoticeList=newArrayList();
- StringsqlText="selectauthor,content,date,titlefromBulletin";
- ResultSetrs=executeQuery(sqlText);
- try{
- while(rs.next()){
- NoticeInfotemp=newNoticeInfo();
- temp.setAuthor(rs.getString("author"));
- temp.setContent(rs.getString("content"));
- temp.setDates(rs.getDate("date"));
- temp.setTitle(rs.getString("title"));
- noticeList.add(temp);
- }
- NoticeInfo[]notices=newNoticeInfo[noticeList.size()];
- for(inti=0;i<noticeList.size();i++){
- notices=(NoticeInfo)noticeList.get(i);
- }
- returnnotices;
- }catch(SQLExceptione){
- e.printStackTrace();
- returnnull;
- }
- }
- }
#p#第三步:配置FlexDataService
1,把剛才寫的JAVA文件編譯。打開FDS的安裝文件夾,將編譯的文件拷貝到\jrun4\servers\default\flex\WEB-INF\classes文件夾中,進(jìn)行下面的配置。
2.打開FDS的安裝文件夾。進(jìn)入jrun4\servers\default\flex\WEB-INF\flex目錄。里面是關(guān)于FlexDataService的配置文件,我們這里只看RemoteObject如何配置,其它配置信息請(qǐng)自己看幫助。現(xiàn)在我們打開里面的remoting-config.xml文件。向里面添加如下信息,作為<service>的子標(biāo)簽:
程序代碼
- <destinationiddestinationid="dataService">
- <properties>
- <source>net.zhuoqun.connectDB.DataServiceImpl</source>
- </properties>
- </destination>
當(dāng)你設(shè)定了destination的時(shí)候,你就引用了了可以用來連接相應(yīng)類的信息通道(messagingchannel)。它的id必須在文件中是獨(dú)一無二的。source屬性是指你編譯的JAVA類在classes文件夾中的路徑。由于我的DataServiceImpl類在classes\net\zhuoqun\connectDB中,所以source的值為net.zhuoqun.connectDB.DataServiceImpl。記住,不要寫.class后綴。<properties>標(biāo)簽還可以有一個(gè)<scope>子標(biāo)簽,其作用我在這里就不說了,大家自己看相關(guān)文檔(關(guān)于FDS的配置其實(shí)有很多東西,這些在幫助文檔里都有,我這里不多說了,也說不過來,自己看吧)。
現(xiàn)在我們已經(jīng)配置好了后臺(tái)的FDS,做完了整個(gè)程序的大部分工作,接下來就是前臺(tái)Flex程序調(diào)用的事情了。
第四步:創(chuàng)建Flex程序
打開FlexBuilder,新建一個(gè)工程ConnectDB。菜單欄中File->New->FlexProject,這時(shí)會(huì)彈出一個(gè)對(duì)話框,選擇FlexDataService,創(chuàng)建了一個(gè)Flex工程。
#p#第五步:通過RemoteObject訪問Flex數(shù)據(jù)庫
打開工程中生成的主文件ConnectDB.mxml,聲明一個(gè)RemoteObject:
程序代碼
- <mx:RemoteObjectidmx:RemoteObjectid="getData"destination="dataService"
- result="proccessResult(event.result)"fault="Alert.show(event.fault.faultString,'Error')"/>
其中destination的值是剛才我們?cè)谂渲肍DS的時(shí)候設(shè)定的destination。result表示在這個(gè)RemoteObject成功返回之后所要做的動(dòng)作,這里我們調(diào)用一個(gè)方法proccessResult()來處理返回的數(shù)據(jù),它的參數(shù)event.result就是從服務(wù)器段獲得的數(shù)據(jù),數(shù)據(jù)是作為一個(gè)對(duì)象傳過來的。fault表示在這個(gè)RemoteObject請(qǐng)求失敗時(shí)要做的處理,這里我們會(huì)彈出一個(gè)顯示錯(cuò)誤信息的對(duì)話框。
接下來我們要聲明一個(gè)DataGrid控件來顯示公告的標(biāo)題和發(fā)布日期:
程序代碼
- <mxataGrididmxataGridid="myDG">
- <mx:columns>
- <mxataGridColumnheaderTextmxataGridColumnheaderText="標(biāo)題"dataField="title"/>
- <mxataGridColumnheaderTextmxataGridColumnheaderText="發(fā)布日期"dataField="dates"labelFunction="formatDate"/>
- </mx:columns>
- </mx:DataGrid>
其中headerText是顯示在上方的表頭,dataField表示要顯示的數(shù)據(jù)域,為什么數(shù)據(jù)域是title和dates呢?因?yàn)槲覀儌骰氐氖且粋€(gè)NoticeInfo對(duì)象數(shù)組,雖然它是作為一個(gè)對(duì)象傳回來的,但是其中的數(shù)據(jù)結(jié)構(gòu)并沒有變,那些數(shù)據(jù)域的名字也沒有變,所以我們可以根據(jù)NoticeInfo中的變量設(shè)定dataField。labelFunction屬性是用來格式化顯示的,因?yàn)閭骰貋淼氖歉窳滞螘r(shí)間,所以我們需要將其格式化然后顯示出來。注意,這里只是顯示兩個(gè)數(shù)據(jù)域,并不代表其它的數(shù)據(jù)都沒有了,它們?nèi)匀淮嬖冢皇菦]有顯示出來。
接下來,在<mx:Script>標(biāo)簽中編寫proccessResult()方法和格式化日期的formatDate方法:
程序代碼
- privatefunctionproccessResult(result:Object):void
- {
- myDG.dataProvider=ArrayUtil.toArray(result);
- }
- privatefunctionformatDate(item:Object,column:DataGridColumn):String
- {
- returndf.format(item.dates);
- }//
df是一個(gè)DateFormatter,在下面會(huì)給出。關(guān)于如何格式化DataGrid的顯示
//以及DateFormatter這里就不討論了,幫助里寫得很清楚
這個(gè)函數(shù)只是簡單地將獲得的數(shù)據(jù)傳給myDG的dataProvider。result的類型是Object,因?yàn)閿?shù)據(jù)是作為一個(gè)對(duì)象傳過來的。之所以調(diào)用ArrayUtil.toArray()這個(gè)方法,是因?yàn)榉祷氐挠涗浛赡苤挥幸粭l,而myDG的dataProvider顯示單個(gè)對(duì)象的時(shí)候可能會(huì)出錯(cuò),所以安全起見先將其轉(zhuǎn)換成數(shù)組。
最后,我們編寫調(diào)用RemoteObject的方法,使其在程序啟動(dòng)時(shí)就調(diào)用。
程序代碼
- privatefunctioninitApp():void
- {
- getData.getNotices();
- }
其中g(shù)etData是RemoteObject的id,getNotices()是DataServiceImpl.java中的方法。在這里可以直接調(diào)用它。當(dāng)然,如果DataServiceImpl.java有其它方法,也可以通過這種方式直接調(diào)用。
接下來設(shè)定組件創(chuàng)建完畢時(shí)調(diào)用initApp()方法,在<mx:Application>中添加一個(gè)creationComplete屬性:
程序代碼
- <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"
- fontSize="12"creationComplete="initApp()">
ConnectDB.mxml的全部代碼:
程序代碼
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"
- fontSize="12"creationComplete="initApp()">
- <mx:Script>
- <![CDATA[
- importmx.controls.Alert;
- importmx.utils.ArrayUtil;
- privatefunctioninitApp():void
- {
- getData.getNotices();
- }
- privatefunctionproccessResult(result:Object):void
- {
- myDG.dataProvider=ArrayUtil.toArray(result);
- }
- privatefunctionformatDate(item:Object,column:DataGridColumn):String
- {
- returndf.format(item.dates);
- }//df是一個(gè)DateFormatter,在下面會(huì)給出。
- 關(guān)于如何格式化DataGrid的顯示
- //以及DateFormatter這里就不討論了,幫助里寫得很清楚
- ]]>
- </mx:Script>
- <mx:DateFormatteridmx:DateFormatterid="df"formatString="YYYY-MM-DD"/>
- <mx:RemoteObjectidmx:RemoteObjectid="getData"destination="dataService"
- result="proccessResult(event.result)"fault="Alert.show(event.fault.faultString,'Error')"/>
- <mx:DataGrididmx:DataGridid="myDG">
- <mx:columns>
- <mx:DataGridColumnheaderTextmx:DataGridColumnheaderText="標(biāo)題"dataField="title"/>
- <mx:DataGridColumnheaderTextmx:DataGridColumnheaderText="發(fā)布日期"dataField="dates"labelFunction="formatDate"/>
- </mx:columns>
- </mx:DataGrid>
- </mx:Application>
整個(gè)工程終于完成,啟動(dòng)JRUN,然后運(yùn)行程序,查看程序結(jié)果。如果是其他數(shù)據(jù)庫,只需要改一下數(shù)據(jù)庫驅(qū)動(dòng)信息就可以了
【編輯推薦】
- 連接Flex數(shù)據(jù)庫行之有效的辦法
- 連接Flex數(shù)據(jù)庫三種方法
- Flex及FlexBuilder2.0開發(fā)環(huán)境詳解
- FlexBuilder3.0與Eclipse3.4的完美結(jié)合
- 學(xué)習(xí)筆記 FlexBuilder2.0中如何使用基于Lists的控件