
<!--Google 468*60橫幅廣告開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--Google 468*60橫幅廣告結(jié)束-->
首先,做一點(diǎn)說明。Flex是不能直接連接數(shù)據(jù)庫的,這一點(diǎn)大家需要知道,它只能間接地連接數(shù)據(jù)庫。Flex中提供了三種方式:HttpService,WebService 和 Remote Object 。其中HttpService可以直接獲取 XML 中的數(shù)據(jù),還可以通過JSP,ASP以及PHP讀取數(shù)據(jù)庫中的數(shù)據(jù),這個(gè)比較簡(jiǎn)單,而且網(wǎng)上也有很多 例子 ,我就不多說了。WebService我不懂,請(qǐng)自己查資料。我一直用的是JAVA 對(duì)象 連接數(shù)據(jù)庫,感覺這個(gè)挺方便,而且J2EE的技術(shù)已經(jīng)很成熟。今天的 教程 就是以 Flex + JAVA + SQLServer 獲取數(shù)據(jù)庫公告信息為例簡(jiǎn)單說一下Remote Object 的用法。
前提
1.確保你安裝了Flex Data Service。這個(gè)對(duì)于單個(gè)CUP無限APP是免費(fèi)的,可以去 Adobe 下載。如果只是讀取XML文件是不需要這個(gè)的,連接數(shù)據(jù)庫就需要它了。
2.安裝了Flex Builder或者有Flex SDK。我這里使用的是Flex Builder(IDE就是方便啊 ^_^)。
3.安裝了SQLServer數(shù)據(jù)庫。
4.安裝了JRUN或者tomcat或其它的J2EE容器,因?yàn)榘l(fā)布的時(shí)候我們的 程序 要運(yùn)行在J2EE平臺(tái)上。
5.安裝了JDK。
第一步:創(chuàng)建數(shù)據(jù)庫
這里我們有一個(gè)公告表,表名為Bulletin。結(jié)構(gòu)如下:
字段名稱 字段類型 說明
ID 自動(dòng)編號(hào) 自動(dòng)編號(hào)
title Nvarchar(100) 題目
date datatime 日期
author Nvarchar(20) 作者
contents ntext 內(nèi)容
在數(shù)據(jù)庫中創(chuàng)建這個(gè)表。保存之后進(jìn)入下一步。
第二步:在JAVA中編寫獲取公告的代碼
首先,我們要?jiǎng)?chuàng)建一個(gè)公告類來專門保存獲取的公告信息,代碼如下。
NoticeInfo.java
- package net.zhuoqun.connectDB;
- import java.util.Date;
- public class NoticeInfo{
- private Stringtitle; //標(biāo)題
- private Stringauthor; //作者
- private Stringcontent; //內(nèi)容
- private Datedates; //時(shí)間
- public StringgetAuthor(){
- return author;
- }
- public void setAuthor(Stringauthor){
- this .author=author;
- }
- ……………… //其它get和set方法。
- }
ArrayList noticeList = new ArrayList();
查詢數(shù)據(jù)庫之后,每讀取一條記錄就添加到 noticeList。
- while (rs.next()){
- NoticeInfotemp= new NoticeInfo();
- 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= new NoticeInfo[noticeList.size()];
- for ( int i= 0 ;i<noticeList.size();i++){
- notices=(NoticeInfo)noticeList.get(i);
- }
- return notices;
現(xiàn)在JAVA部分的代碼就寫好了。
DataServiceImpl.java 的全部代碼如下:
- package net.zhuoqun.connectDB;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Date;
- public class DataServiceImpl{
- private Connectionconn= null ;
- private Statementstmt= null ;
- //以下是數(shù)據(jù)庫以及驅(qū)動(dòng)信息
- public final static StringDRIVER= "com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
- public final static StringCONN_STR_PRE= "jdbc:microsoft:sqlserver://" ;
- public final static StringHOST_NAME= "localhost:1433;" ;
- public final static StringDATABASE_NAME= "DatabaseName=mydata" ;
- public final static StringUSERNAME= "aaa" ;
- public final static StringPASSWORD= "aaa" ;
- public DataServiceImpl(){
- }
- //查詢數(shù)據(jù)庫
- private ResultSetexecuteQuery(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);
- return rs;
- } catch (SQLExceptione){
- e.printStackTrace();
- }
- return null ;
- }
- //查詢公告.這個(gè)是本程序的關(guān)鍵代碼
- public NoticeInfo[]getNotices(){
- ArrayListnoticeList= new ArrayList();
- StringsqlText= "selectauthor,content,date,titlefromBulletin" ;
- ResultSetrs=executeQuery(sqlText);
- try {
- while (rs.next()){
- NoticeInfotemp= new NoticeInfo();
- 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= new NoticeInfo[noticeList.size()];
- for ( int i= 0 ;i<noticeList.size();i++){
- notices=(NoticeInfo)noticeList.get(i);
- }
- return notices;
- } catch (SQLExceptione){
- e.printStackTrace();
- return null ;
- }
- }
- }
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)于Flex Data Service 的配置文件,我們這里只看RemoteObject如何配置,其它配置信息請(qǐng)自己看幫助。現(xiàn)在我們打開里面的 remoting-config.xml 文件。向里面添加如下信息,作為<service>的子標(biāo)簽:
程序代碼
- < destination id = "dataService" >
- < properties >
- < source > net.zhuoqun.connectDB.DataServiceImpl </ source >
- </ properties >
- </ destination >
現(xiàn)在我們已經(jīng)配置好了后臺(tái)的 FDS,做完了整個(gè)程序的大部分工作,接下來就是前臺(tái)Flex程序調(diào)用的事情了。
第四步:創(chuàng)建Flex程序
打開Flex Builder,新建一個(gè)工程 ConnectDB。菜單欄中 File -> New -> Flex Project,這時(shí)會(huì)彈出一個(gè)對(duì)話框,選擇 Flex Data Service,創(chuàng)建了一個(gè)Flex工程。
第五步:通過 RemoteObject 訪問數(shù)據(jù)庫
打開工程中生成的主文件 ConnectDB.mxml,聲明一個(gè) RemoteObject :
程序代碼
- < mx:RemoteObject id = "getData" destination = "dataService" result = "proccessResult(event.result)" fault = "Alert.show(event.fault.faultString,'Error')" />
程序代碼
- < mx:DataGrid id = "myDG" >
- < mx:columns >
- < mx:DataGridColumn headerText = "標(biāo)題" dataField = "title" />
- < mx:DataGridColumn headerText = "發(fā)布日期" dataField = "dates" labelFunction = "formatDate" />
- </ mx:columns >
- </ mx:DataGrid >
接下來,在 <mx:Script> 標(biāo)簽中編寫proccessResult()方法和格式化日期的 formatDate方法:
程序代碼
- private functionproccessResult(result:Object): void
- {
- myDG.dataProvider=ArrayUtil.toArray(result);
- }
- private functionformatDate(item:Object,column:DataGridColumn):String
- {
- return df.format(item.dates);
- } //df是一個(gè)DateFormatter,在下面會(huì)給出。關(guān)于如何格式化DataGrid的顯示
- //以及DateFormatter這里就不討論了,幫助里寫得很清楚
最后,我們編寫調(diào)用 RemoteObject 的方法,使其在程序啟動(dòng)時(shí)就調(diào)用。
程序代碼
- privatefunctioninitApp():void
- {
- getData.getNotices();
- }
程序代碼
- < mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" fontSize = "12" creationComplete = "initApp()" >
程序代碼
- <? xml version = "1.0" encoding = "utf-8" ?>
- < mx:Application xmlns: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:DateFormatter id = "df" formatString = "YYYY-MM-DD" />
- < mx:RemoteObject id = "getData" destination = "dataService" result = "proccessResult(event.result)" fault = "Alert.show(event.fault.faultString,'Error')" />
- < mx:DataGrid id = "myDG" >
- < mx:columns >
- < mx:DataGridColumn headerText = "標(biāo)題" dataField = "title" />
- < mx:DataGridColumn headerText = "發(fā)布日期" dataField = "dates" labelFunction = "formatDate" />
- </ mx:columns >
- </ mx:DataGrid >
- </ mx:Application >
<!--新Google 468*60橫幅廣告開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 創(chuàng)建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468*60橫幅廣告結(jié)束-->
<!--新Google 468x15 橫鏈接單元開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 橫鏈接單元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468x15 橫鏈接單元結(jié)束-->
<!-- Google Reader shared發(fā)布代碼開始 --><script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script><script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D)%3Bnew%20GRC"></script><!-- Google Reader shared發(fā)布代碼結(jié)束 -->
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
