flex 与后台数据库连接 (RemoteObject)

来源:互联网 发布:centos查看监听端口 编辑:程序博客网 时间:2024/05/29 09:47

      经过几天的折腾,终于可以实现flex与后台的数据库连接,通过RemoteObject方法,配置确实比较麻烦。现在总结一下:

  安装工具:myeclipse

                    flex

                    blazeds.war

                    mysql-connector-java-5.1.7-bin.jar

   配置过程:

      1.myeclipse中安装flex插件(参考:http://www.blogjava.net/rainwindboys/archiv/2009/06/24/232914.html)

     2.在myeclipse中创建web工程

     3.在web工程中引入java和mysql的数据库驱动mysql-connector-java-5.1.7-bin.jar

         在properties 中的java Build Path 的 Libraries 选中 add external jars 引入包

     4.在flex 视图中按项目上右键,采用 add web project capablity 取消create web.xml的复选框默认

        在项目上右键,向Flex项目中加入Web支持(作用:为了让TomCat可以加载此项目,因为   Tomcat只可以加载Web项目)

     5.改properties中的flex server 改Root URL为http://localhost:8080 

                                                  Context Root 为/Login 

                                                  并在Deploy Flex WAR 中导入     blazeds.war(lcds的精简版)

     6.配置WebRoot->flex->remoting-config.xml 中加入

      <destination id="remoteLogin">
        <properties>
            <source>com.test.Login</source>
               </properties>
            </destination>

     com.test.Login是我们放在src目录下引用的java文件,起了个id叫remoteLogin

    在.mxml文件中对应

  <mx:RemoteObject id="firstRO" destination="remoteLogin"  result="getROResult(event)"/>

    然后我们就可以在button的触发事件(FirstRO)中调用java方法通过上面的RemoteObject组件的id(firstRO)实例如下:

   private function FirstRO(str1:String,str2:String):void{
       firstRO.checkLogin(str1,str2);//用这个方法将两个参数传递给方法
  
    }

     7.要点总结

      RemoteObject的后台连接方法:在返回事件处理过程中有两种方法:事件监听,直接返回

      (1)先介绍直接返回,就是下面示例中展示的:

<mx:RemoteObject id="firstRO" destination="remoteLogin"  result="getROResult(event)"/>

       private function getROResult(e:ResultEvent):void{
       var infos:String;
       infos = e.result.toString();//这个是从方法中返回的参数,根据参数判断  
      if(infos=="登陆"){

        Alert.show("登陆");//登陆处理

      }else{

        Alert.show("未登陆");//登陆处理

    }

      (2) 介绍事件监听法

    <mx:RemoteObject destination="remoteLogin" id="remoteHello"></mx:RemoteObject>
    <mx:Button x="192" y="255" label="Button" click="TestLogin()"/>

 public function TestLogin():void{
   var logNam: String = username.text;
   var logpassword: String = userpassword.text;
   remoteHello.checkLogin(logNam,logpassword);
   remoteHello.addEventListener(ResultEvent.RESULT, getRoHelloRes); //在button事件中监听服务器端得java程序是否完成并返回了结果
  }
 
  private function getRoHelloRes(e: ResultEvent) :void {        //当返回结果时处理
//   Alert.show(e.result.toString());
  show.visible="true";
  show.text=e.result.toString();
 
  }

     8.遇到的问题:

       缓存:不是大问题,每次只要修改正确就可以运行

       数据显示问题:Alert.show("输出的字符串")(show不能少)

                            或者用组件输出:

                            <mx:Label x="25" y="16" text=""  id="show" fontSize="15"width="66"/>

                                                    show.visible="true";
                                                    show.text=e.result.toString();

      9.示例展示

 

//login.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
      import mx.rpc.events.ResultEvent;
      import mx.controls.Alert;
      import mx.collections.ArrayCollection ;
     private function FirstRO(str1:String,str2:String):void{
       firstRO.checkLogin(str1,str2);//用这个方法将两个参数传递给方法
  
    }
     private function getROResult(e:ResultEvent):void{
       var infos:String;
       infos = e.result.toString();//这个是从方法中返回的参数,根据参数判断
       // show.visible="true";
       //show.text=e.result.toString();
       //Alert.show(infos);
       //show.text=infos;
      if(infos=="登陆"){

        Alert.show("登陆");//登陆处理

      }else{

        Alert.show("未登陆");//登陆处理

    }
  }
  ]]>
   </mx:Script>
 
<mx:RemoteObject id="firstRO" destination="remoteLogin"  result="getROResult(event)"/>
<mx:Canvas x="148" y="157" width="200" height="200" fontSize="12">
<mx:Label x="25" y="48" text="user" />
<mx:Label x="25" y="86" text="passwd"/>
<mx:TextInput x="81" y="46" width="109" id="name1" />
<mx:TextInput x="81" y="84" width="109" id="passwd" displayAsPassword="true"/>
<mx:Button x="25" y="142" label="登陆" click="FirstRO(name1.text.toString

(),passwd.text.toString())"/>
<mx:Button x="125" y="142" label="Button"/>
<mx:Label x="25" y="16" text=""  id="show" fontSize="15" width="66"/>

</mx:Canvas>
</mx:Application>


//login.java

package com.test;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public  class Login {
   
          TestDao test=new TestDao();
           public String checkLogin(String name,String password){
            //System.out.println(name);
            String temp="未登陆";
              String sql="select* from t_user where userName='"+name+"' and

userPassword='"+password+"'";
               ResultSet rs=test.selectSql(sql);
              try {
                if(rs.next()){
                    temp="登陆";
                   
                }
           } catch (SQLException e) {
                e.printStackTrace();
            }
           //System.out.println(temp);
           return temp;
           }
    }

//TestDao.java

package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDao {
    private static String username="root";
    private  static String password="123456";
    private  static String database="web";
    private  Statement stmt=null;
    private  ResultSet  rs=null;
    private static Connection con=null; 
    private  static String url="jdbc:mysql://localhost:3306/"+database+"?

useUnicode=true&characterEncoding=utf8";
     
          static{
        try{
               Class.forName("com.mysql.jdbc.Driver").newInstance();
               con=DriverManager.getConnection(url,username,password);
           }catch(Exception e){
               e.printStackTrace();
           }      
          }
   
             public ResultSet selectSql(String sql){
                 
                 try {
                     stmt=con.createStatement();
                    rs=stmt.executeQuery(sql);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return rs;
             }
             //insert delete update
             public  void updateSql(String sql){
                
                 try {
                     stmt=con.createStatement();
                  stmt.executeUpdate(sql);
              } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
             }
             public void getClose(){
                 try {
                     if(con!=null){
                         con.close();
                     }
                     if(rs!=null){
                         rs.close();
                     }
                     } catch (SQLException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                     }
                 }                 
         }


//remoteing-config.xml

  <destination id="remoteLogin">
        <properties>
            <source>com.test.Login</source>
               </properties>
            </destination>

原创粉丝点击