首頁 > ColdFusion, FLEX > FLEX AS3與ColdFusion進行Remoting = 資料庫篇

FLEX AS3與ColdFusion進行Remoting = 資料庫篇

2009年2月11日  瀏覽次數 : 4,199

在前面的文章中 FLEX AS3與ColdFusion進行Remoting = 傳值篇 ,我們已經可以送出資料到 SERVER 端,也可以在 SERVER 端處理好字串內容,接下來我們要將 SERVER 端取得的資料庫的資料內容顯示在 FLEX 內。

image

首先我們先來了解一下 ColdFusion 與 FLEX 之間不對等的資料型態關係

ColdFusion 資料型態 ActionScript 資料型態
String String
Array Array
Query ArrayCollection
Struct Object
CFC instance Strongly typed VO
Date Date
Numeric Number
XML Object XML Object

 

瞭解了以上的資料轉換後,因為我們要讀取的是資料庫的資料因此我們要先安裝 MySQL 資料,不過那不是本篇的重點,我們在安裝資料庫後建立資料表格。

image

接下來我們到 CF ADMIN 控制台設定資料庫名稱,我們建立資料庫名稱 finpo與連線資料庫為 MySQL ,並且連線到資料庫 finpo ,輸入好帳號密碼。

image

建立完成後你應該可以正確的看到您的資料庫寫著 OK

 image

建立完成資料庫的名稱後,我們可以開始打開上次的 helloworld.cfc 來增加第二個方法,並且搜尋出資料庫的客戶資料庫,並且將整個搜尋結果傳到 FLEX 內。

helloworld.cfc

?View Code COLDFUSION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<cfcomponent>
	<cffunction name="sayHellow" access="remote" returntype="String">
    	<cfargument name="firstname" type="string" required="yes">
        <cfargument name="lastname" type="string" required="yes">
		<cfreturn "#firstname# #lastname# 您好 ! 這是你的 sayHellow 方法">
	</cffunction>
 
    <cffunction name="getCustomer" access="remote" returntype="query">
    	<cfquery name="customerQuery" datasource="finpo">
        	select *
            from customer
        </cfquery>
		<cfreturn customerQuery>
	</cffunction>
</cfcomponent>

修改好我們的 CFC 元件後,接下來要開始寫 FLEX 的程式碼,我決定另外開一個新的 Application 並取名為 remotingDB.mxml  來取得 SERVER 端回傳的客戶資料。

remotingDB.mxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
	 fontSize="16">
	<mx:Script>
		<![CDATA[
			import mx.rpc.events.ResultEvent;
 
			private function clickHandler():void{
				this.roDB.getCustomer();
			}
 
			private function resultHandler(event:ResultEvent):void{
				this.customerResultData.dataProvider = event.result ;
			}
		]]>
	</mx:Script>
	<mx:Button label="查詢資料" click="clickHandler()"/>
	<mx:DataGrid id="customerResultData">
		<mx:columns>
			<mx:DataGridColumn headerText="客戶編號" dataField="cust_id"/>
			<mx:DataGridColumn headerText="客戶名稱" dataField="cust_company_name"/>
			<mx:DataGridColumn headerText="客戶地址" dataField="cust_company_addr"/>
		</mx:columns>
	</mx:DataGrid>
	<mx:RemoteObject id="roDB" destination="ColdFusion" source="comp.helloworld" result="resultHandler(event)"/>
</mx:Application>

最後執行程式,我們便可得到資料庫內的客戶資料。

image

我想到這邊,資料庫的新增修改刪除應該都難不倒你了吧 ! 當然前提是你要對 ColdFusion 這個可恨又可愛的 SERVER 端語言能夠操作資料庫新增修改與刪除。

Random Posts

Loading…

:: 把這篇好文推到書籤網站與更多人分享吧 ::
  • funp
  • Hemidemi
  • YahooKimo
  • Google
  • udn
  • Haohao
  • Live

相關文章 :

Ausir ColdFusion, FLEX , , , ,

  1. akillerbear
    2009年4月13日20:36 | #1

    請問一下版主
    如果要加入charts要怎麼把data base傳過來的值轉成他讀的格式阿@@
    看了中文書的範例都是寫死在裡面 沒有看到範例是利用傳過來的資料作繪圖

  2. 2009年4月13日22:00 | #2

    @akillerbear

    你是說圓餅圖嗎??

    資料庫的部分用 ZENDAMF OK嗎 ?~

  3. killerbear
    2009年4月14日09:16 | #3

    恩恩~我先用coldfusion 跑OK
    後來用amfphp 跑也OK
    後來改換ZENDAMF因為從官方網站下載的壓縮檔解壓縮失敗就還沒試過@@
    小弟主要想問的是把資料庫裡的值倒入data grid 很容易 不過要怎麼把那些值取出來
    讓charts讀取@@ 是要把它們轉型嗎?

  4. 2009年4月14日09:44 | #4

    圓餅圖吃得是數值
    你資料庫讀出來的資料要轉換成次數
    或是哪些數值是你要顯示出來的

    資料庫讀出來的資料如果已經是圓餅圖的資料
    整理一下應該就可以顯示出來了

    不過圓餅圖到是還沒玩過 XD
    順便來摸摸看

  5. akillerbear
    2009年4月19日18:32 | #5

    後來我試出來了
    我少給他一個dataprovider指到我要給予的位置XD

  6. 2009年4月19日21:56 | #6

    @akillerbear

    OPPPS ^^ 先恭喜喔~
    但是下次要記得檢查程式碼有沒有漏掉呢~~

  1. 本篇文章目前尚無任何 trackbacks 和 pingbacks。