Flex是不能直接连接数据库的,它只能间接地连接数据库,所以你需要使用某种类型的服务来支持对数据的使用。Flex别指望能连Oracle,只有AIR能连sqllite,flex只是客户端,没权限去操作数据库的,只有借助JAVA等后台语言去连,而且只是间接连DB 。其实 一个项目可以抽象的分为两个部分,数据存储+数据表现 。flex只是客户端的一种数据表现形式(动画形式)与JavaScript和html在某种意义是一致的,没法直接连数据库。
Flex中提供了三种方式:HttpService(flex 与服务器交互),WebService(flex与webservice交互) 和RemoteObject(flex 与普通java类通信)。其中HttpService可以直接获取XML中的数据,还可以通过JSP,ASP以及PHP读取数据库中的数据。WebService我不懂,请自己查资料。JAVA对象连接数据库挺方便,而且J2EE的技术已经很成熟。Remote,就是所谓的遥控器(Remote) ,客户端直接遥控执行服务器端的方法。把get和post传值封装进去了,传值和调用一个方法用起来无分别,和ajax一样异步通讯 但却超越ajax。
上一篇:JAVA POJO类
数据库查询相关操作(实体类,查询后返回ArrayList noticeList = new ArrayList();类型数据)
创建好这个之后我们要创建一个数据查询类:DataServiceImpl.java来查询数据库,并将查询结果传给将要创建的Flex程序。由于我们不清楚有多少条记录,所以就借助一下JAVA中的ArrayList这个类,它位于java.util 包中。先创建一个ArrayList:
ArrayList noticeList = new ArrayList();
查询数据库之后,每读取一条记录就添加到 noticeList。
while(rs.next()){
NoticeInfo temp = 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);
}
查询完毕之后你就可以把这个noticeList传回去,你也可以传回去一个 NoticeInfo 数组:
NoticeInfo[] notices = new NoticeInfo[noticeList.size()];
for(int i=0;i<noticeList.size();i++){
notices = (NoticeInfo)noticeList.get(i);
}
return notices;
我这里用的是后一种方法。如果你直接把noticeList传回去的话,记住一点,JAVA的ArrayList类型的对象到了Flex中会变成ArrayCollection类型的。
普通Java类:
package com.flex.demo;
/**
* 功能描述:该类用来实现flex与普通java类中的方法通信
* @author Administrator
*/
public class SimpleService {
public String sayHello(String name){
return "Hello, "+name;
}
}
配置说明:配置remoting-config.xml
<destination id="myservice">
<properties>
<source>com.flex.demo.SimpleService</source>
</properties>
</destination>
调用
<!--flex 与普通java类通信-->
<s:RemoteObject id="serv" destination="myservice" fault="serv_faultHandler(event)" result="serv_resultHandler(event)">
</s:RemoteObject>
//代码来自参考文章http://www.blogjava.net/sxyx2008/archive/2010/08/31/326360.html
一种在加载页面时调用,一种点击事件发生时调用
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
creationComplete="service.send()"
>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
//错误处理函数
protected function serv_faultHandler(event:FaultEvent):void
{
Alert.show("调用失败了:"+event.fault.message as String,"提示1");
}
//成功调用函数
protected function serv_resultHandler(event:ResultEvent):void
{
Alert.show("调用成功了:"+event.result as String,"提示2");
}
protected function service_faultHandler(event:FaultEvent):void
{
Alert.show("调用失败了:"+event.fault.message as String,"提示3");
}
protected function service_resultHandler(event:ResultEvent):void
{
Alert.show("调用成功了:"+event.result as String,"提示4");
}
protected function ws_faultHandler(event:FaultEvent):void
{
Alert.show("调用失败了:"+event.fault.message as String,"提示5");
}
//调用成功了
protected function ws_resultHandler(event:ResultEvent):void
{
trace(event.result);
lbl.text=(String)(event.result);
}
//第一种调用webservice的方法
//此方法调用webservice
protected function btn_clickHandler(event:MouseEvent):void
{
//ws.getWeatherbyCityName(city.text);
//第二中调用webservice的方法
ws.getWeatherbyCityName.send();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<!--flex 与普通java类通信-->
<s:RemoteObject id="serv" destination="myservice" fault="serv_faultHandler(event)" result="serv_resultHandler(event)">
</s:RemoteObject>
<!-- flex 与服务器交互-->
<s:HTTPService id="service" fault="service_faultHandler(event)" result="service_resultHandler(event)" url="http://localhost:8080/">
</s:HTTPService>
<!--flex与webservice交互这里调用一个天气预报的webservice-->
<s:WebService id="ws"
wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"
fault="ws_faultHandler(event)"
result="ws_resultHandler(event)"
showBusyCursor="true">
<!-- 第二种调用webservice的方法<s:operation>-->
<!-- <s:operation name="getWeatherbyCityName"> webservice中的方法名-->
<s:operation name="getWeatherbyCityName">
<!--传递的参数-->
<s:request>
<!--参数名称必须与webservice中定义的参数名一致否则调用不成功报错-->
<theCityName>
{city.text}
</theCityName>
</s:request>
</s:operation>
</s:WebService>
</fx:Declarations>
<s:TextInput x="488" y="72" id="city"/>
<s:Button x="633" y="72" label="查看" id="btn" click="btn_clickHandler(event)"/>
<s:Label x="224" y="128" id="lbl" width="820" height="376"/>
</s:WindowedApplication>