flex显示数据库表中的所有数据,我通过调用远程对象来实现。
flex调用远程对象,服务器端我使用的是BlazeDS,我的另一篇文章讲到了如何搭建BlazeDS环境,
文章地址是:http://blog.csdn.net/friendan/article/details/16887679
在此不再敖述BlazeDS.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
看下数据库表截图:
------------------------------------------------------------------------------------------------------------------
flex程序效果截图
----------------------------------------------------------------------------------------------------------------------------------
服务器端java代码:
public class AdminDAOImpl implements AdminDAO{
@Override
public List<Admin> GetAllAdmin() { //获取数据库表中的所有用户信息
try {
List<Admin> list=new ArrayList();
String sql="select * from tbAdmin";
ResultSet rs=DbCRUD.Select(sql);
while(rs.next())
{
Admin admin=new Admin();
admin.setId(rs.getInt("id"));
admin.setUsrName(rs.getString("usrName"));
admin.setUsrPass(rs.getString("usrPass"));
list.add(admin);
//System.out.println(rs.getString(1)+"---"+rs.getString(2));
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public Admin GetAdmin(int id) {
try {
String sql="select * from tbAdmin where id='1'";
sql=sql.replace("1",String.valueOf(id));
//System.out.println(sql);
ResultSet rs=DbCRUD.Select(sql);
int row=0;
Admin admin=new Admin();
while(rs.next())
{
admin.setId(rs.getInt("id"));
admin.setUsrName(rs.getString("usrName"));
admin.setUsrPass(rs.getString("usrPass"));
//System.out.println(rs.getString(1)+"---"+rs.getString(2));
}
return admin;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
remoting-config.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="adminDAOImpl">
<properties>
<source>dao.impl.AdminDAOImpl</source>
</properties>
</destination>
</service>
以上主要是配置远程对象adminDAOImpl,这样flex才能调用它。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
flex代码:
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:RemoteObject id="roAdmin" destination="adminDAOImpl">
<s:method name="GetAllAdmin" result="rstGetAllAdmin(event)" fault="fltGetAllAdmin(event)"/>
</s:RemoteObject>
</fx:Declarations>
[Bindable]
var ac:ArrayCollection=new ArrayCollection();//定义数据绑定对象ac
//远程对象调用成功时
private function rstGetAllAdmin(evt:ResultEvent):void
{
ac=evt.result as ArrayCollection;
dgUser.dataProvider=ac;
}
//远程对象调用失败时
private function fltGetAllAdmin(evt:FaultEvent):void
{
Alert.show(evt.message as String);
}
//显示所有
protected function btnViewAll_clickHandler(event:MouseEvent):void
{
roAdmin.GetAllAdmin();//调用远程对象
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
再贴一下效果截图:
---------------------------------------------------------------------------------------------------------------------------------
您的十分满意是我追求的宗旨。
您的一点建议是我后续的动力。