Flex4+BlazeDS+JAVA+MySql 构建J2EE工程 对用户信息进行管理实例

要求

    • 必备知识

      本文要求基本了解 Adobe Flex编程知识和JAVA基础知识。

    • 开发环境

      MyEclipse10/Flash Builder4.6/Flash Player11及以上

    • 演示地址

      演示地址 资料下载

 
 

关于 Myeclipse10.6+Flash Builder 4.6插件安装配置 请阅读:

Myeclipse10.6+Flash Builder 4.6安装配置

 

程序截图:

2014-03-04_022451

2014-03-04_024307

程序部分代码

UserDao.java

package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import tool.ConvertJson;
import tool.JDBCUtilSingle;



import bean.User;

public class UserDao {
    
    /**
     * 初始化10条数据
     */
    
    private void initData(){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
        String sql="truncate usermanagement_user";
        String sql2="INSERT INTO `usermanagement_user` VALUES (1,'licheng','test@qq.com','189********','湖南长沙'),(2,'张三','test@gmail.com','150********','北京'),(3,'李四','test@sou.com.cn','188********','上海'),(4,'王五','test@sina.com','135********','南京'),(5,'赵六','test@163.com','138********','成都');";
        try {
            statement=connection.prepareStatement(sql);
            statement.executeUpdate();
            statement=connection.prepareStatement(sql2);
            statement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
        }
    }
    
    
    
    
    /**
     * 获取用户信息
     * @return 装载用户信息的集合列表
     */
    public ArrayList<User> getList(){
        
        ArrayList<User> users=new ArrayList<User>();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
        String sql="select * from usermanagement_user";
        try {
             statement=connection.prepareStatement(sql);
             rs=statement.executeQuery();
             while(rs.next()){
                 users.add(new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5)));
             }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
        }
        
        if(users.size()==0){
            initData();
            users=getList();
        }
        
        
        return users;
    }
    
    
    /**
     * 添加一条用户数据
     * @param name 名称
     * @param email 邮箱
     * @param tel 电话
     * @param area 区域
     * @return 装载用户信息的集合
     */
    public ArrayList<User> addUser(String name,String email,String tel,String area){
        
        
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
        String sql="insert into usermanagement_user(name,email,tel,area) values(?,?,?,?)";
        try {
            statement=connection.prepareStatement(sql);
            statement.setString(1, name);
            statement.setString(2, email);
            statement.setString(3, tel);
            statement.setString(4, area);
            statement.executeUpdate();  //返回1表示插入成功
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
        }
        
        return getList(); //获取集合信息
        
    }
    
    
    
    /**
     * 根据ID删除指定的用户
     * @param id 要删除的ID号
     * @return 用户集合列表
     */
    public ArrayList<User> delUser(int id){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
        String sql="delete from usermanagement_user where id=?";
        try {
            statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            statement.executeUpdate();  //返回1表示删除成功
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
        }
        return getList();
    }
    
    
    public ArrayList<User> updUser(int id,String name,String email,String tel,String area){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
        String sql="update usermanagement_user set name=?,email=?,tel=?,area=? where id=?";
        try {
            statement=connection.prepareStatement(sql);
            statement.setString(1, name);
            statement.setString(2, email);
            statement.setString(3, tel);
            statement.setString(4, area);
            statement.setInt(5, id);
            statement.executeUpdate();  
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
        }
        return getList();
    }
    
        
    
}

UserServer.java

package service;

import java.util.ArrayList;
import bean.User;
import Dao.UserDao;

public class UserServer {

    public ArrayList<User> getList(){
        return new UserDao().getList();
    }
    
    public ArrayList<User> addUser(String name,String email,String tel,String area){
        return new UserDao().addUser(name, email, tel, area);
    }
    
    public ArrayList<User> delUser(int id){
        return new UserDao().delUser(id);
    }
    
    public ArrayList<User> updUser(int id,String name,String email,String tel,String area){
        return new UserDao().updUser(id, name, email, tel, area);
    }
    
    
}

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="UsererverTaget">
        <properties>
            <source>service.UserServer</source>
        </properties>
    </destination>
</service>

主程序代码 UserManagement.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" 
               verticalCenter="0" horizontalCenter="0"
               creationComplete="init()">

    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        <s:RemoteObject id="userDist" destination="UsererverTaget"
                        fault="userDist_faultHandler(event)">
            <s:method name="getList" result="getList_resultHandler(event)" />
            <s:method name="addUser" result="user_resultHandler(event)" />
            <s:method name="delUser" result="user_resultHandler(event)" />
            <s:method name="updUser" result="user_resultHandler(event)" />
        </s:RemoteObject>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import components.GridItemRenderer;
            
            import mx.collections.ArrayCollection;
            import mx.containers.Grid;
            import mx.controls.Alert;
            import mx.managers.PopUpManager;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.utils.StringUtil;    
            import spark.events.GridEvent;
            

            [Bindable]
            public var personList:ArrayCollection;
            
            public var idColumn:GridColumn;
            public var nameColumn:GridColumn;
            public var emailColumn:GridColumn;
            public var telColumn:GridColumn;
            public var areaColumn:GridColumn;

            public var userItem:Object;
            
            
            /**
             * 程序初始化
             */
            private function init():void{
            
                userDist.getList(); //调用 getList方法获取数据
                //绑定按钮的监听事件
                add.addEventListener(MouseEvent.CLICK,addClick);
                mod.addEventListener(MouseEvent.CLICK,modClick);
                del.addEventListener(MouseEvent.CLICK,delClick);
                usersDG.addEventListener(GridEvent.GRID_CLICK,usersDG_clickHandler);
                adduser.addEventListener(MouseEvent.CLICK,addUser);
                upduser.addEventListener(MouseEvent.CLICK,updUser);
            }

            
            /**
             * getList方法调用结果处理函数
             */
            protected function getList_resultHandler(event:ResultEvent):void
            {
                //获取用户信息列表
                personList=event.result as ArrayCollection;
                
                idColumn=usersDG.columns.getItemAt(0) as GridColumn;
                nameColumn=usersDG.columns.getItemAt(1) as GridColumn;
                emailColumn=usersDG.columns.getItemAt(2) as GridColumn;
                telColumn=usersDG.columns.getItemAt(3) as GridColumn;
                areaColumn=usersDG.columns.getItemAt(4) as GridColumn;
                
                
                //渲染器貌似要到这里定义 需要等数据初始化完毕在定义渲染器 直接到组件定义 报错
                idColumn.itemRenderer=new ClassFactory(GridItemRenderer);
                nameColumn.itemRenderer=new ClassFactory(GridItemRenderer);
                emailColumn.itemRenderer=new ClassFactory(GridItemRenderer);
                telColumn.itemRenderer=new ClassFactory(GridItemRenderer);
                areaColumn.itemRenderer=new ClassFactory(GridItemRenderer);
                
                //设置渲染器
                usersDG.columns=new ArrayList([idColumn,nameColumn,emailColumn,telColumn,areaColumn]);
                
                //禁用按钮
                mod.enabled=false;
                del.enabled=false;
            }
            
            /**
             * userDist 错误处理函数
             */
            protected function userDist_faultHandler(event:FaultEvent):void
            {
                // 弹出错误信息
                Alert.show(event.message.toString());
            }
                
            /**
             * 单击添加按钮
             */
            protected function addClick(event:MouseEvent):void{
                 adduser.visible=true;
                upduser.visible=false;
                popup.visible=true; //popup层可见
                popmsg.text="新增用户"; 
            }
            
            /**
             * 单击修改按钮
             */
            protected function modClick(event:MouseEvent):void{
                adduser.visible=false;
                upduser.visible=true;
                popup.visible=true; //popup层可见
                popmsg.text="更新用户"; 
                
                myname.text=userItem.name;
                email.text=userItem.email;
                tel.text=userItem.tel;
                area.text=userItem.area;     
            }
        
            /**
             * 关闭弹出层
             */
            protected function close_clickHandler():void
            {
                popup.visible=false; //popup层可见
                myname.text=null;email.text=null;tel.text=null;area.text=null;
            }
            
            /**
             * 数据列表的单击事件
             */
            protected function usersDG_clickHandler(event:GridEvent):void
            {
                if(usersDG.selectedIndex!=-1){
                    mod.enabled=true;
                    del.enabled=true;
                    userItem=usersDG.selectedItem;
                }
            }
            
           /**
           *  添加用户 
           */    
             protected function addUser(event:MouseEvent):void{
                var _name:String=StringUtil.trim(myname.text);
                var _email:String=StringUtil.trim(email.text);
                var _tel:String=StringUtil.trim(tel.text);
                var _area:String=StringUtil.trim(area.text);
                   if(_name!=""&&_email!=""&&_tel!=""&&_area!=""){
                    close_clickHandler(); //关闭弹出层
                    userDist.addUser(_name,_email,_tel,_area);
                    usersDG.selectedIndex=-1;
                }else{
                    Alert.show("输入的各项内容不能为空!!");
                }
           }
        
            /**
             * 删除用户
             */
            protected function delClick(event:Event):void{
            
                if(usersDG.selectedIndex!=-1){
                    userDist.delUser(int(userItem.id));
                    usersDG.selectedIndex=-1;
                    mod.enabled=false;
                    del.enabled=false;
                }
            }
            
            
            /**
             * 修改用户
             */
            protected function updUser(event:MouseEvent):void{
                var _name:String=StringUtil.trim(myname.text);
                var _email:String=StringUtil.trim(email.text);
                var _tel:String=StringUtil.trim(tel.text);
                var _area:String=StringUtil.trim(area.text);
                if(_name!=""&&_email!=""&&_tel!=""&&_area!=""){
                    close_clickHandler(); //关闭弹出层
                    usersDG.selectedIndex=-1;
                    userDist.updUser(userItem.id,_name,_email,_tel,_area);
                }else{
                    Alert.show("输入的各项内容不能为空!!");
                }
                
            }
            
            /**
             * 结果处理函数
             */
            protected function user_resultHandler(event:ResultEvent):void
            {
                
                //获取用户信息列表
                personList=event.result as ArrayCollection;
                if(usersDG.selectedIndex==-1){
                    mod.enabled=false;
                    del.enabled=false;
                }
                
            }
            
        ]]>
    </fx:Script>
    <s:Rect width="100%" height="100%">
        <s:fill>
            <s:SolidColor color="#333333"/>
        </s:fill>
    </s:Rect>
    
    <s:Group width="800" height="500" top="80" horizontalCenter="0">
        <s:filters>
            <s:DropShadowFilter alpha="0.3" angle="90" blurX="5.0" blurY="5.0" color="#000000"
                                distance="2" hideObject="false" inner="false" knockout="false"
                                quality="1" strength="5"/>
        </s:filters>
        
        <!--白色大背景-->
        <s:Rect  width="100%" height="100%">
            <s:fill>
                <s:SolidColor color="#FFFFFF"/>
            </s:fill>
        </s:Rect>
        
        
        <!--按钮-->
        <s:Group width="800" height="50" horizontalCenter="0" >
            
            <s:Button id="add" x="10" y="10" width="60" height="30" label="新增" color="#FFFFFF" fontFamily="微软雅黑"
                      fontSize="13" skinClass="skinks.AddButtonSkin" buttonMode="true"/>
            
            <s:Button id="mod" x="80" y="10" width="60" height="30" label="修改" color="#FFFFFF" fontFamily="微软雅黑"
                      fontSize="13" skinClass="skinks.ModButtonSkin" buttonMode="true"/>
            
            <s:Button id="del" x="150" y="10" width="60" height="30" label="删除" buttonMode="true"
                      color="#FFFFFF"  fontFamily="微软雅黑" fontSize="13"
                      skinClass="skinks.DelButtonSkin"/>
            
        </s:Group>
        
        <!--数据表-->
        <s:DataGrid id="usersDG" y="50" left="10" right="10" color="#FFFFFF" fontFamily="微软雅黑" fontSize="13"
                    horizontalCenter="0" requestedRowCount="10" rowHeight="40" skinClass="skinks.DG"
                    dataProvider="{personList}"
                    
                    >
            <s:columns>
                <s:ArrayList>
                <s:GridColumn dataField="id" width="85"   headerText="#"  headerRenderer="components.GridHeaderRenderer"   ></s:GridColumn>
                <s:GridColumn dataField="name" width="150"  headerText="名称" headerRenderer="components.GridHeaderRenderer" ></s:GridColumn>
                <s:GridColumn dataField="email" width="250"  headerText="邮箱" headerRenderer="components.GridHeaderRenderer" ></s:GridColumn>
                <s:GridColumn dataField="tel" headerText="电话" headerRenderer="components.GridHeaderRenderer" ></s:GridColumn>
                <s:GridColumn dataField="area" headerText="地区" headerRenderer="components.GridHeaderRenderer" ></s:GridColumn>            
                </s:ArrayList>
            </s:columns>
        </s:DataGrid>
        
    </s:Group>
    
    <!--锁屏 弹出层-->
    
    <s:Group id="popup" width="100%" height="100%" visible="false">
        <s:Rect width="100%" height="100%" alpha="0.4">
            <s:fill>
                <s:SolidColor color="#000000"/>
            </s:fill>
        </s:Rect>
        <!---->
        <s:Group   width="400" height="320" horizontalCenter="0" top="200" >
            <s:Rect width="100%" height="100%">
                <s:fill>
                    <s:SolidColor color="#FFFFFF"/>
                </s:fill>
            </s:Rect>
            <!--label条-->
            <s:Group width="100%" height="40">
                <s:Rect width="100%" height="100%">
                    <s:fill>
                        <s:SolidColor color="#3DAFE3"/>
                    </s:fill>
                </s:Rect>
                <s:Label id="popmsg" x="10" color="#FFFFFF" fontFamily="微软雅黑" fontSize="14" text="新增用户"
                         verticalAlign="middle" verticalCenter="0"/>
                <s:Label right="10" width="25" height="25" color="#FFFFFF" fontFamily="Verdana"
                         fontSize="18" fontWeight="bold" text="X" textAlign="center"
                         verticalAlign="middle" verticalCenter="0" buttonMode="true"
                         click="close_clickHandler()"/>
            </s:Group>
            <s:HGroup horizontalCenter="0" top="60">
                <s:Label width="80" height="30" color="#666666" fontFamily="微软雅黑" fontSize="13"
                         text="名称:" textAlign="center" verticalAlign="middle" trackingLeft="3" trackingRight="3"/>
                <s:TextInput id="myname" width="250" height="30" borderAlpha="0.5" 
                             borderVisible="true" focusColor="#FFFFFF" 
                             skinClass="skinks.TextInputSkin"/>
            </s:HGroup>
            <s:HGroup x="30" y="230" horizontalCenter="0" top="110">
                <s:Label width="80" height="30" color="#666666" fontFamily="微软雅黑" fontSize="13"
                         text="邮箱:" textAlign="center" verticalAlign="middle" trackingLeft="3" trackingRight="3"/>
                <s:TextInput id="email" width="250" height="30" borderAlpha="0.5" 
                             borderVisible="true" focusColor="#FFFFFF" 
                             skinClass="skinks.TextInputSkin"/>
            </s:HGroup>
            <s:HGroup x="30" y="110" horizontalCenter="0" top="160">
                <s:Label width="80" height="30" color="#666666" fontFamily="微软雅黑" fontSize="13"
                         text="电话:" textAlign="center" verticalAlign="middle" trackingLeft="3" trackingRight="3"/>
                <s:TextInput id="tel" width="250" height="30" borderAlpha="0.5" 
                             borderVisible="true" focusColor="#FFFFFF" 
                             skinClass="skinks.TextInputSkin"/>
            </s:HGroup>
            <s:HGroup x="30" y="170" horizontalCenter="0" top="210">
                <s:Label width="80" height="30" color="#666666" fontFamily="微软雅黑" fontSize="13"
                         text="地区:" textAlign="center" verticalAlign="middle" trackingLeft="3" trackingRight="3"/>
                <s:TextInput id="area" width="250" height="30" borderAlpha="0.5" 
                             borderVisible="true" focusColor="#FFFFFF" 
                             skinClass="skinks.TextInputSkin"/>
            </s:HGroup>    
            
            <s:Group x="30" y="270" top="260">
                <s:Button id="adduser" left="85" label="新增" color="#FFFFFF" fontFamily="微软雅黑" fontSize="13"
                          skinClass="skinks.MyButton"/>
                <s:Button id="upduser" left="85" label="更新" color="#FFFFFF" fontFamily="微软雅黑" fontSize="13"
                          skinClass="skinks.MyButton"/>
            </s:Group>
                
            
            
        </s:Group>
    </s:Group>
    
    
</s:Application>

SQL代码:

CREATE TABLE `usermanagement_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '',
  `email` varchar(50) NOT NULL DEFAULT '',
  `tel` varchar(14) NOT NULL DEFAULT '',
  `area` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
作者:Li-Cheng
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2014-03-05 15:39  Li-Cheng  阅读(1991)  评论(12编辑  收藏  举报