【个人笔记】基于AMF-RPC实现Flex与JAVA的交互实例【上】

(此为本人学习笔记,原创)

说明:此例为flex通过免费的Java EE 服务器端组件-BlazeDS连接到java,并利用java与MySQL交互返回一个ArrayList类集,并再返回到Flex端进行解析。本例代码中省略了大量注释。


1.MyEclipse中新建一个WEB工程“FlexDao”,新建包com.sxy.demo 和2个类。
注意:)
     1.  StudentDao类中一定要有
Setter&Getter方法!!!(不管用不用得到)

          没有的话,Flex端是绝对无法正常解析的。(很奇怪)

     2.  StudentDao类不一定要implements Serializable
 



package com.sxy.demo;

public class StudentDao {

	private int stu_id;
	private String stu_name;
	private String stu_sex;
	private int stu_age;
	private String stu_class;
	private String stu_major;
	private  String stu_department;
	private String stu_type;
	
	public StudentDao(int stu_id, String stu_name, String stu_sex, int stu_age,
			String stu_class, String stu_major, String stu_department,
			String stu_type) {
		super();
		this.stu_id = stu_id;
		this.stu_name = stu_name;
		this.stu_sex = stu_sex;
		this.stu_age = stu_age;
		this.stu_class = stu_class;
		this.stu_major = stu_major;
		this.stu_department = stu_department;
		this.stu_type = stu_type;
	}
	public int getStu_id() {
		return stu_id;
	}
	public void setStu_id(int stu_id) {
		this.stu_id = stu_id;
	}
	public String getStu_name() {
		return stu_name;
	}
	public void setStu_name(String stu_name) {
		this.stu_name = stu_name;
	}
	public String getStu_sex() {
		return stu_sex;
	}
	public void setStu_sex(String stu_sex) {
		this.stu_sex = stu_sex;
	}
	public int getStu_age() {
		return stu_age;
	}
	public void setStu_age(int stu_age) {
		this.stu_age = stu_age;
	}
	public String getStu_class() {
		return stu_class;
	}
	public void setStu_class(String stu_class) {
		this.stu_class = stu_class;
	}
	public String getStu_major() {
		return stu_major;
	}
	public void setStu_major(String stu_major) {
		this.stu_major = stu_major;
	}
	public String getStu_department() {
		return stu_department;
	}
	public void setStu_department(String stu_department) {
		this.stu_department = stu_department;
	}
	public String getStu_type() {
		return stu_type;
	}
	public void setStu_type(String stu_type) {
		this.stu_type = stu_type;
	}
	
	
}
package com.sxy.demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class Student {
	
	public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
	public static final String DBURL = "jdbc:mysql://localhost:3306/sxy" ;
	public static final String DBUSER = "root" ;
	public static final String DBPASS = "123" ;
	
	
	public static Connection getConnection(){   
        Connection conn=null;   
        try{      
            Class.forName(DBDRIVER);   
            conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);   
        }catch(Exception e){   
            System.err.println(e.getMessage());   
        }   
        return conn;   
    }   

public List<StudentDao> getInfo(String key)
	
	{
		List<StudentDao> list = new ArrayList<StudentDao>();  
		PreparedStatement stat = null;
		Connection conn=getConnection();
		try{ 	
			Statement stmt = conn.createStatement(); 
         	stat=conn.prepareStatement("select * from STUDENT where STU_ID  LIKE ?");
            stat.setString(1, "%"+key+"%");
            ResultSet rs=stat.executeQuery();
			
			while( rs.next()) 
			{
				 list.add(new StudentDao( 
						Integer.parseInt(rs.getString("STU_ID")),
						rs.getString("STU_NAME"),
						rs.getString("STU_SEX"),
						Integer.parseInt(rs.getString("STU_AGE")),
						rs.getString("STU_CLASS"),
						rs.getString("STU_MAJOR"),
						rs.getString("STU_DEPARTMENT"),
						rs.getString("STU_TYPE"))); 
			} 
			
			rs.close();
         	stmt.close(); 
			conn.close(); 

			}catch(Exception e){ }	
		
		return list;
		
	}	
}

2.解压缩blazeds.war文件,将解压后的WEB-INF目录覆盖WEB工程FlexDao的WebRoot目录下,并在WebRoot\WEB-INF\flex\remoting-config.xml中定义一个远程服务。



3.将该工程部署到Tomcat服务器目录中,并启动。



4.新建Flex项目-FlexDao




5.编写FlexDao.mxml文件

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:reflect="flex.lang.reflect.*">
	<fx:Declarations>
		<mx:RemoteObject id="sxy" destination="Student" result="resultHandler(event)" endpoint="/FlexDao/messagebroker/amf"/>
	</fx:Declarations>
	<fx:Script>        
		<![CDATA[ 
			
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;  
			
			[Bindable]
			public var arr:ArrayCollection = new ArrayCollection();
			
			private function hello():void 
			{      
				sxy.getInfo(t1.text);
			}
			
			private function resultHandler(event:ResultEvent):void 
			{      
		               arr = ArrayCollection(event.result)
			} 
		]]>    
	</fx:Script> 
          <s:Panel width="1060" height="422"  
			 title="学生信息查询" x="144.55" y="123">
		<s:VGroup x="22" y="14" width="102" height="73" >  
			<s:TextInput id="t1" width="98" height="35"/>
			<s:Button label="查询" id="Button" click="hello()" width="55" height="26"/>
		</s:VGroup>  
		<mx:DataGrid x="139" y="14" width="900" height="344" dataProvider="{arr}" id="datagrid"  showDataTips="true" verticalAlign="middle" >
			<mx:columns>
				
				<mx:DataGridColumn headerText="学号" dataField="stu_id"  resizable="true"/>
				<mx:DataGridColumn headerText="姓名" dataField="stu_name"  resizable="true"/>
				<mx:DataGridColumn headerText="性别" dataField="stu_sex"  resizable="true"/>
				<mx:DataGridColumn headerText="年龄" dataField="stu_age"  resizable="true"/>
				<mx:DataGridColumn headerText="班级" dataField="stu_class"  resizable="true"/>
				<mx:DataGridColumn headerText="专业" dataField="stu_major"  resizable="true"/>
				<mx:DataGridColumn headerText="院系" dataField="stu_department"  resizable="true"/>
				<mx:DataGridColumn headerText="类型" dataField="stu_type"  resizable="true"/>
				
			</mx:columns>
		</mx:DataGrid>
	</s:Panel>
</s:Application>



6.编译&运行


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值