精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-04-27
最后修改:2009-09-03
这个小例子通过remoteobject的通讯方法。实现flex与mySQL数据库的交互。 BlazeDS 是一个基于服务器的 Java 远程控制 (remoting) 和 Web 消息传递 (messaging) 技术,它能够使得后端的 Java 应用程序和运行在浏览器上的 Adobe Flex 应用程序相互通信。使用Blazeds可以很方便的连接java后台,同时他也提供HttpService、webservice方式,不过在Blazeds中可以通过配置文件中对其进行设置的,这样提高了应用的灵活性。其中最重要的还是RemotingObject技术,他可以直接远程远程调用java后台提供的公共接口,使其效率大大提升,一般是采用HTTPService方式的10倍左右。 ConnextionHelper.java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionHelper { private String url; private static ConnectionHelper instance; private ConnectionHelper() { try { Class.forName("com.mysql.jdbc.Driver"); url = "jdbc:MySQL://localhost/test"; } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { if (instance == null) { instance = new ConnectionHelper(); } try { return DriverManager.getConnection(instance.url,"root","root"); } catch (SQLException e) { throw e; } } public static void close(Connection connection) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } DAOException.java public class DAOException extends RuntimeException { static final long serialVersionUID = -1881205326938716446L; public DAOException(String message) { super(message); } public DAOException(Throwable cause) { super(cause); } public DAOException(String message, Throwable cause) { super(message, cause); } } 以上是数据库连接所需方法。然后根据数据库中表的结构写下所需的方法类。
Admin.java import java.io.Serializable; public class Admin implements Serializable { static final long serialVersionUID = 103844514947365244L; private int id; private String username; private String userpwd; public Admin() { } public Admin(int id,String username,String userpwd) { this.id = id; this.username=username; this.userpwd=userpwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } } AdminService.java import java.util.ArrayList; import java.util.List; import java.sql.*; import flex.jie.ConnectionHelper; import flex.jie.DAOException; public class AdminService { public List getAdmins() throws DAOException { List list = new ArrayList(); Connection c = null; try { c = ConnectionHelper.getConnection(); Statement s = c.createStatement(); ResultSet rs = s.executeQuery("SELECT * FROM admin ORDER BY id"); while (rs.next()) { list.add(new Admin(rs.getInt("id"), rs.getString("username"), rs.getString("userpwd"))); } } catch (SQLException e) { e.printStackTrace(); throw new DAOException(e); } finally { ConnectionHelper.close(c); } return list; } } 重要一步:
在remoting-config.xml文件中 添加(source处根据实际的建包名填写) <destination id="admin"> <properties> <source>flex.jie.user.AdminService</source> </properties> </destination> 至此。myEclipse工程中所需代码以及配置已经完成。将工程部署并发布在Tomacat下面。 接下来,flex端的步骤。 首先 新建Flex Project ,输入工程名。在server technology处选择J2EE,点击next 填写刚刚发布在tomcat下的目录名。点击完成。 <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#FFFFFF"> <mx:RemoteObject id="srv" destination="admin"/> <mx:DataGrid dataProvider="{srv.getAdmins.lastResult}" width="100%" height="100%"/> <mx:Button label="Get Data" click="srv.getAdmins()"/> </mx:Application> 在tomcat启动的前提下,点击运行即可。 或者想显示指定的列。只需要稍作修改即可 <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#FFFFFF" creationComplete="srv.getAdmins()"> <mx:Script> <![CDATA[ import mx.rpc.events.ResultEvent; import mx.collections.ArrayCollection; [Bindable]public var ad:ArrayCollection; private function adminHandler(event:ResultEvent):void { ad= event.result as ArrayCollection } ]]> </mx:Script> <mx:RemoteObject id="srv" destination="admin"> <mx:method name="getAdmins" result="adminHandler(event)"/> </mx:RemoteObject> <mx:DataGrid dataProvider="{ad}"> <mx:columns> <mx:DataGridColumn headerText="用户名" dataField="username"/> <mx:DataGridColumn headerText="密码" dataField="userpwd"/> </mx:columns> </mx:DataGrid> </mx:Application> 显示为形式可为: 说明:此时的destination对应remote-config.xml中的id号 。建立连接。从而flex端所用到的java方法可以找到。返回数据库中所建表的信息
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-12-10
你的文章我看了,都很棒
|
|
返回顶楼 | |
浏览 4653 次