Flex用blazeDs与后台mysql数据交互

这个小例子通过remoteobject的通讯方法。实现flex与mySQL数据库的交互。

BlazeDS 是一个基于服务器的 Java 远程控制 (remoting) 和 Web 消息传递 (messaging) 技术,它能够使得后端的 Java 应用程序和运行在浏览器上的 Adobe Flex 应用程序相互通信。使用Blazeds可以很方便的连接java后台,同时他也提供HttpService、webservice方式,不过在 Blazeds中可以通过配置文件中对其进行设置的,这样提高了应用的灵活性。其中最重要的还是RemotingObject技术,他可以直接远程远程调 用java后台提供的公共接口,使其效率大大提升,一般是采用HTTPService方式的10倍左右。 

 

 
1  使用mySQL中的test数据库,建表名为admin.表中字段为:  id username userpwd
(最终目的:将数据库表中的记录最终显示在flex端的DateGrid组件中显示出来)
2  建立myEclipse的工程。新建->web project 输入工程名后。需要将事先下载的Blazeds包粘贴至工程目录下。并且将mysql的驱动放到工程目录lib的文件夹下
(我使用版本blazeds_turnkey_3-0-0-544.zip。将解压后的文件夹中的blazeds.war改成rar的拓展名,并解压,将得到的META-INF和WEB-INF两个文件夹拷入刚建成的工程下webroot下)
3  编写所需的java方法

Java代码
  1. ConnextionHelper.java  
  2. import  java.sql.Connection;  
  3. import  java.sql.DriverManager;  
  4. import  java.sql.SQLException;  
  5.   
  6. public   class  ConnectionHelper  
  7. {  
  8.     private  String url;  
  9.   
  10.     private   static  ConnectionHelper instance;  
  11.   
  12.     private  ConnectionHelper()  
  13.     {  
  14.         try  {  
  15.             Class.forName("com.mysql.jdbc.Driver" );  
  16.             url = "jdbc:MySQL://localhost/test" ;  
  17.               
  18.         } catch  (Exception e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22.   
  23.     public   static  Connection getConnection()  throws  SQLException {  
  24.         if  (instance ==  null ) {  
  25.             instance = new  ConnectionHelper();  
  26.         }  
  27.         try  {  
  28.             return  DriverManager.getConnection(instance.url, "root" , "root" );  
  29.         } catch  (SQLException e) {  
  30.             throw  e;  
  31.         }  
  32.     }  
  33.       
  34.     public   static   void  close(Connection connection)  
  35.     {  
  36.         try  {  
  37.             if  (connection !=  null ) {  
  38.                 connection.close();  
  39.             }  
  40.         } catch  (SQLException e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.     }  
  44. }  
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();
        }
    }
}
Java代码
  1. DAOException.java  
  2. public   class  DAOException  extends  RuntimeException  
  3. {  
  4.     static   final   long  serialVersionUID = -1881205326938716446L;  
  5.   
  6.     public  DAOException(String message)  
  7.     {  
  8.         super (message);  
  9.     }  
  10.   
  11.     public  DAOException(Throwable cause)  
  12.     {  
  13.         super (cause);  
  14.     }  
  15.   
  16.     public  DAOException(String message, Throwable cause)  
  17.     {  
  18.         super (message, cause);  
  19.     }  
  20.   
  21. }  
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);
    }

}
 以上是数据库连接所需方法。然后根据数据库中表的结构写下所需的方法类。
 
Java代码
  1. Admin.java  
  2. import  java.io.Serializable;  
  3.   
  4. public   class  Admin  implements  Serializable {  
  5.   
  6.     static   final   long  serialVersionUID = 103844514947365244L;  
  7.       
  8.     private   int  id;  
  9.     private  String username;  
  10.     private  String userpwd;  
  11.      
  12.       
  13.     public  Admin() {  
  14.           
  15.     }  
  16.       
  17.     public  Admin( int  id,String username,String userpwd) {  
  18.         this .id = id;  
  19.         this .username=username;  
  20.         this .userpwd=userpwd;  
  21.     }  
  22.   
  23.     public   int  getId() {  
  24.         return  id;  
  25.     }  
  26.   
  27.     public   void  setId( int  id) {  
  28.         this .id = id;  
  29.     }  
  30.   
  31.     public  String getUsername() {  
  32.         return  username;  
  33.     }  
  34.   
  35.     public   void  setUsername(String username) {  
  36.         this .username = username;  
  37.     }  
  38.   
  39.     public  String getUserpwd() {  
  40.         return  userpwd;  
  41.     }  
  42.   
  43.     public   void  setUserpwd(String userpwd) {  
  44.         this .userpwd = userpwd;  
  45.     }  
  46.   
  47. }  
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;
    }

}
 
 
Java代码
  1. AdminService.java  
  2. import  java.util.ArrayList;  
  3. import  java.util.List;  
  4. import  java.sql.*;  
  5.   
  6. import  flex.jie.ConnectionHelper;  
  7. import  flex.jie.DAOException;  
  8.   
  9. public   class  AdminService {  
  10.   
  11.     public  List getAdmins()  throws  DAOException {  
  12.   
  13.         List list = new  ArrayList();  
  14.         Connection c = null ;  
  15.   
  16.         try  {  
  17.             c = ConnectionHelper.getConnection();  
  18.             Statement s = c.createStatement();  
  19.             ResultSet rs = s.executeQuery("SELECT * FROM admin ORDER BY id" );  
  20.             while  (rs.next()) {  
  21.                 list.add(new  Admin(rs.getInt( "id" ),  
  22.                         rs.getString("username" ),  
  23.                         rs.getString("userpwd" )));  
  24.             }  
  25.         } catch  (SQLException e) {  
  26.             e.printStackTrace();  
  27.             throw   new  DAOException(e);  
  28.         } finally  {  
  29.             ConnectionHelper.close(c);  
  30.         }  
  31.         return  list;  
  32.   
  33.     }  
  34. }  
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处根据实际的建包名填写)

Xml代码
  1. < destination   id = "admin" >   
  2.         < properties >   
  3.             < source > flex.jie.user.AdminService </ source >   
  4.         </ properties >   
  5. </ destination >   
 

至此。myEclipse工程中所需代码以及配置已经完成。将工程部署并发布在Tomacat下面。

接下来,flex端的步骤。

首先 新建Flex Project ,输入工程名。在server technology处选择J2EE,点击next

填写刚刚发布在tomcat下的目录名。点击完成。
在默认的mxml中的代码如下:

Mxml代码
  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  backgroundColor= "#FFFFFF" >  
  3.       
  4.     <mx:RemoteObject id="srv"  destination= "admin" />  
  5.       
  6.     <mx:DataGrid dataProvider="{srv.getAdmins.lastResult}"  width= "100%"  height= "100%" />   
  7.   
  8.     <mx:Button label="Get Data"  click= "srv.getAdmins()" />      
  9.           
  10. </mx:Application>   

在tomcat启动的前提下,点击运行即可。

或者想显示指定的列。只需要稍作修改即可 

Mxml代码
  1. <?xml version= "1.0"  encoding= "utf-8" ?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  backgroundColor= "#FFFFFF"  creationComplete= "srv.getAdmins()" >  
  3.   <mx:Script>  
  4.       <![CDATA[  
  5.         
  6.       import mx.rpc.events.ResultEvent;      
  7.       import mx.collections.ArrayCollection;  
  8.       [Bindable]public var ad:ArrayCollection;  
  9.             
  10.           private function adminHandler(event:ResultEvent):void  
  11.             {  
  12.                 ad= event.result as ArrayCollection  
  13.             }      
  14.               
  15.       ]]>  
  16.   </mx:Script>    
  17.    <mx:RemoteObject id="srv"  destination= "admin" >  
  18.         <mx:method name="getAdmins"  result= "adminHandler(event)" />  
  19.     </mx:RemoteObject>  
  20.     <mx:DataGrid dataProvider="{ad}" >  
  21.         <mx:columns>  
  22.             <mx:DataGridColumn headerText="用户名"  dataField= "username" />  
  23.             <mx:DataGridColumn headerText="密码"  dataField= "userpwd" />  
  24.         </mx:columns>  
  25.     </mx:DataGrid>  
  26.           
  27. </mx:Application>  
 

显示为形式可为:

说明:此时的destination对应remote-config.xml中的id号 。建立连接。从而flex端所用到的java方法可以找到。返回数据库中所建表的信息

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值