在我们先前一个例子的基础上改写helloworld的例子
package com.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.database.util.DataBase;
public class HelloWorld {
//我们返回的是一个包含Accountbean的 数组
public Account[] sayHello()
{
Connection con = DataBase.getConnection();
//DataBase是我们写的连接数据库的工具类,在此我们不再贴出来。
String sql = "select dicAccountID,name,info from account";
ResultSet rs = null;
Account[] arr = null;
try {
PreparedStatement pstm = con.prepareStatement(sql);
rs = pstm.executeQuery();
List<Account> list = new ArrayList<Account>();
while(rs.next())
{
Account ac = new Account();
ac.setDicAccountID(rs.getString("dicAccountID"));
ac.setName(rs.getString("name"));
ac.setInfo(rs.getString("info"));
list.add(ac);
}
arr = new Account[list.size()];
for(int i=0;i<list.size();i++)
{
arr[i] = (Account)list.get(i);
System.out.println(arr[i].getDicAccountID());
}
} catch (Exception e) {
e.printStackTrace();
}
return arr;
}
}
然后是我们配置和写的FirstFlex.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="remotingSayHello()">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
import mx.utils.ArrayUtil;
function gg(event:Object):void{
//var ff:String = evnet.result as String;
//ggg.text = ff;
myDG.dataProvider =ArrayUtil.toArray(event);
}
function remotingSayHello():void{
//var sname:String = nameInput.text;
h.sayHello();
}
]]>
</mx:Script>
<mx:RemoteObject destination="hello" id="h"
result="gg(event.result)" endpoint="http://localhost:8080/FirstFlex/messagebroker/amf" >
</mx:RemoteObject>
<mx:DataGrid id="myDG">
<mx:columns>
<mx:DataGridColumn headerText="名字" dataField="dicAccountID"/>
<mx:DataGridColumn headerText="备注" dataField="info"/>
在这里的dicaccountid.和 info都是和我们account中的字段对应,我们把他理解成从数组中拿出信息
</mx:columns>
</mx:DataGrid>
</mx:Application>