[Bindable] public var users:ArrayCollection=new ArrayCollection();
private function click():void {
var remoteOperation:RemoteObject = new RemoteObject(); remoteOperation.destination = "getposdata"; remoteOperation.endpoint = "http://localhost:8080/flexpos/messagebroker/amf"; var op0:AbstractOperation = remoteOperation.getOperation("getAllUser"); op0.send(); op0.addEventListener(ResultEvent.RESULT,customOperationHandleResult); op0.addEventListener(FaultEvent.FAULT,customOperationHandleFault);
}
/*public function onResult(event:ResultEvent):void{ users=ArrayCollection(event.result); info.text="get data sucessful"; } public function onFault():void{ info.text="Error"; }*/
private function customOperationHandleResult(event:ResultEvent) : void { users=ArrayCollection(event.result); //info.text="get data sucessful"; //Alert.show(event.result.toString(), "OK"); }
2、代码如下:
(1)连数据库及获取数据库数据的JAVA文件GetData.java
package org.pos;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;public class GetData {
public static Connection getConnection() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException ce) {
System.out.print(ce.getMessage());
}
try{
String url=" jdbc:postgresql://localhost:5432/openbravopos";
String username ="postgres";
String password="tad";
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
} catch(Exception e){
System.out.println(e.getMessage());
}
return conn;
}
public List getAllUser() throws SQLException{
Connection c=getConnection();
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from products");
ArrayList userList=new ArrayList<GetSet>();
while(rs.next()){
GetSet u = new GetSet();
u.setName(rs.getString("name"));
userList.add(u);
}
return userList;
}}
get及set文件GetSet.java
package org.pos;public class GetSet {private String name;
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
}mxml文件<?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">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:DataGrid x="50" y="50" dataProvider="{users}">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="用户名"/>
</mx:columns>
</mx:DataGrid>
<mx:Label id="info"/>
<fx:Script>
<![CDATA[
import mx.rpc.remoting.RemoteObject;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.AbstractOperation;
import mx.collections.ArrayCollection;
[Bindable]
public var users:ArrayCollection=new ArrayCollection();
private function click():void
{
var remoteOperation:RemoteObject = new RemoteObject();
remoteOperation.destination = "getposdata";
remoteOperation.endpoint = "http://localhost:8080/flexpos/messagebroker/amf";
var op0:AbstractOperation = remoteOperation.getOperation("getAllUser");
op0.send();
op0.addEventListener(ResultEvent.RESULT,customOperationHandleResult);
op0.addEventListener(FaultEvent.FAULT,customOperationHandleFault);
}
/*public function onResult(event:ResultEvent):void{
users=ArrayCollection(event.result);
info.text="get data sucessful";
}
public function onFault():void{
info.text="Error";
}*/
private function customOperationHandleResult(event:ResultEvent) : void
{
users=ArrayCollection(event.result);
//info.text="get data sucessful";
//Alert.show(event.result.toString(), "OK");
}
private function customOperationHandleFault(event:FaultEvent) : void
{
Alert.show(event.fault.toString(), "OK");
trace("customOperationHandleFault: " + event.fault.message);
}
// 用于接收服务器返回数据的回调函数
/*private function result(event:ResultEvent):void
{
Alert.show(event.result.toString(), "OK");
}*/
]]>
</fx:Script>
<mx:Button label="发送" click="click()" x="50" y="20"/>
</s:WindowedApplication>注意数据库的jar文件要拷贝到项目\WebContent\WEB-INF\lib目录下就可以了。