本文通过调用远程对象的方法来操作数据库,关于如何搭建BlazeDS,我就不多说了,
网上有很多关于BlazeDS的文章,百度一下即可。
----------------------------------------------------------------------------------------------------------------------------
flex往数据库写入数据大致步骤:
1.定义数据库表。
2.编写与数据库表对应的java vo类(也称java bean类?)。
3.编写插入数据的方法。
4.定义flex vo类,该类要与java vo类对应。
5.flex调用远程方法。
-------------------------------------------------------------------------------------------------
看下数据库表(有3列):
--------------------------------------------------------------------------------------------------------------------------------------
java vo类如下:
package bean; public class Admin { //对应数据库表tbAdmin int id; String usrName; String usrPass; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsrName() { return usrName; } public void setUsrName(String usrName) { this.usrName = usrName; } public String getUsrPass() { return usrPass; } public void setUsrPass(String usrPass) { this.usrPass = usrPass; } }
服务器端插入数据方法:
@Override public int AddAdmin(Admin admin) { //添加管理员 String sql="insert into tbAdmin(id,usrName,usrPass)" + " values (0?,'1?','2?')"; sql=sql.replace("0?",String.valueOf(admin.getId())); sql=sql.replace("1?",admin.getUsrName()); sql=sql.replace("2?",admin.getUsrPass()); System.out.println(sql); return DbCRUD.Add(sql); }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
配置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>
flex vo类(注意要与服务器端的java vo类对应起来):
package vo { [RemoteClass(alias="bean.Admin")] //这句不能少 public class Admin { public var id:int; public var usrName:String; public var usrPass:String; public function Admin()//构造函数 { } } }
flex远程调用服务器的添加数据方法:
1.声明远程对象:
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:RemoteObject id="roAdmin" destination="adminDAOImpl">
<s:method name="GetAllAdmin" result="rstGetAllAdmin(event)" fault="fltGetAllAdmin(event)"/>
<s:method name="AddAdmin" result="rstAddAdmin(event)" fault="fltAddAdmin(event)"/>
</s:RemoteObject>
</fx:Declarations>
2.调用远程方法:
//增加用户 import vo.Admin;//导入包 protected function btnAdd_clickHandler(event:MouseEvent):void { //创建对象 var admin:Admin=new Admin(); admin.id=int(txbId.text); admin.usrName=txbUsrName.text; admin.usrPass=txbUsrPass.text; roAdmin.AddAdmin(admin);//调用远程对象 }---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
效果截图:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
您的十分满意是我追求的宗旨。
您的一点建议是我后续的动力。