`
wushanlang
  • 浏览: 45306 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

flex LCDS 整合 java 增删改查

阅读更多

1,建一个userdb库,再建userinfo表,字段:id(int),username(varchar),password(varchar)。


2,DBConnection.java


    package com.datainfo;  
 
  import java.sql.Connection;  
 
import java.sql.DriverManager;  
  import java.sql.SQLException;  
 

public class DBConnection {  
    public static Connection
getConnection() throws ClassNotFoundException,  
            SQLException
{  
        Connection conn = null;  
        String driver =
"com.mysql.jdbc.Driver";  
        String url =
"jdbc:mysql://localhost:3306/userdb";      //不同数据库不同JDBC
        String
username = "sa";  
        String password = "sasa";  
       
Class.forName(driver);  
        conn = DriverManager.getConnection(url,
username, password);  
        return conn;  
    }  


3,User.java


    package com.datainfo;  
 
public class User {  
    private
int id;  
    private String username;  
    private String password;  

 
    public User() {  
          
    }  
 
    /** 

     * @return the id 
     */ 
    public int getId() {  

        return id;  
    }  
 
    /** 
     * @param id 

     *            the id to set 
     */ 
    public void setId(int
id) {  
        this.id = id;  
    }  
 
    /** 
     *
@return the username 
     */ 
    public String getUsername() {  

        return username;  
    }  
 
    /** 
     * @param
username 
     *            the username to set 
     */ 
    public
void setUsername(String username) {  
        this.username = username;  

    }  
 
    /** 
     * @return the password 
     */ 

    public String getPassword() {  
        return password;  
   
}  
 
    /** 
     * @param password 
     *            the
password to set 
     */ 
    public void setPassword(String password)
{  
        this.password = password;  
    }  


4,UserDAO.java


   DAO层实现增、删、改、查方法


  package com.datainfo;  
 
import java.sql.Connection;  
import
java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import
java.sql.SQLException;  
import java.sql.Statement;  
import
java.util.ArrayList;  
 
import com.datainfo.DBConnection;  
 


//查询
public class UserDAO {  
    public ArrayList getUserList()
throws ClassNotFoundException, SQLException {  
        Connection conn =
DBConnection.getConnection();  
        Statement stmt =
conn.createStatement();  
        ResultSet rs = stmt.executeQuery("select *
from userinfo");  
        ArrayList userList = null;  
        try {  

            userList = new ArrayList();  
            while (rs.next())
{  
                User user = new User();  
               
user.setId(rs.getInt("id"));  
               
user.setUsername(rs.getString("username"));  
               
user.setPassword(rs.getString("password"));  
               
userList.add(user);  
            }  
            rs.close();  

            stmt.close();  
            conn.close();  
        }
catch (Exception e) {  
            // TODO Auto-generated catch block  

            e.printStackTrace();  
        }  
        return
userList;  
    }  



  //增加
    public void addUser(User user) throws
ClassNotFoundException, SQLException {  
        Connection conn =
DBConnection.getConnection();  
        String sql = "insert into userinfo
(username,password) values (?,?)";  
        try {  
           
PreparedStatement pstmt = conn.prepareStatement(sql);  
           
pstmt.setString(1, user.getUsername());  
            pstmt.setString(2,
user.getPassword());  
            pstmt.executeUpdate();  
           
pstmt.close();  
            conn.close();  
        } catch (Exception
e) {  
            // TODO Auto-generated catch block  
           
e.printStackTrace();  
        }  
    }  
 


//修改
    public void updataUser(User user) throws
ClassNotFoundException,  
            SQLException {  
       
Connection conn = DBConnection.getConnection();  
        String sql =
"update userinfo set username=?,password=? where id=?";  
        try {  

            PreparedStatement pstmt = conn.prepareStatement(sql);  

            pstmt.setString(1, user.getUsername());  
           
pstmt.setString(2, user.getPassword());  
            pstmt.setInt(3,
user.getId());  
            pstmt.executeUpdate();  
           
pstmt.close();  
            conn.close();  
        } catch (Exception
e) {  
            // TODO Auto-generated catch block  
           
e.printStackTrace();  
        }  
    }  
 


//删除
    public void deleteUser(User user) throws
ClassNotFoundException,  
            SQLException {  
       
Connection conn = DBConnection.getConnection();  
        String sql =
"delete from userinfo where id =?";  
        try {  
           
PreparedStatement pstmt = conn.prepareStatement(sql);  
           
pstmt.setInt(1, user.getId());  
            pstmt.executeUpdate();  

            pstmt.close();  
            conn.close();  
        }
catch (Exception e) {  
            // TODO Auto-generated catch block  

            e.printStackTrace();  
        }  
    }  


5,配置文件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> 
 


/*指定blazeds所使用的java类*/
    <destination id="userDao"> 
       
<properties> 
           
<source>com.datainfo.UserDAO</source> 
       
</properties> 
    </destination> 
</service> 


   


6,以下是flex端,UserFlex.as


  package com.flex  
{  
    [Bindable]  
   
[RemoteClass(alias="com.datainfo.User")]  
    public class UserFlex  

    {  
//      public function UserFlex{  
//            

//      }  
        public var id:int;  
        public var
username:String;  
        public var password:String;  



//      如果用private,就需要写上get/set  

//        
//      public function get id():int{  

//          return this.id;  
//      }  
//      public function
set id():int{  
//          this.id = id;  
//      }  
//        

//      public function get username():String{  
//          return
this.username;  
//      }  
//      public function set
username():String{  
//          this.username = username;  
//      }  

//        
//      public function get password():String{  

//          return this.password;  
//      }  
//      public
function set password():String{  
//          this.password = password;  

//      }  
    }  


7,自定义的DataGrid组件,用以双击修改数据DoubleClickDataGrid.as


   Flex所使用的与数据进行交互的组件


 package com.flex  
{  
    import flash.events.MouseEvent;  
   
import mx.controls.DataGrid;  
    import
mx.controls.dataGridClasses.DataGridColumn;  
    import
mx.controls.listClasses.IDropInListItemRenderer;  
    import
mx.controls.listClasses.IListItemRenderer;  
    import
mx.core.EventPriority;  
    import mx.events.DataGridEvent;  
      

      
    public class DoubleClickDataGrid extends DataGrid  
   
{  
        public function DoubleClickDataGrid()  
        {  

            //TODO: implement function  
            super();  

            doubleClickEnabled = true;  
        }  
       
override protected function mouseDoubleClickHandler(event:MouseEvent):void 

        {  
            var dataGridEvent:DataGridEvent;  

            var r:IListItemRenderer;  
            var
dgColumn:DataGridColumn;  
              
            r =
mouseEventToItemRenderer(event);  
            if (r && r !=
itemEditorInstance)  
            {  
                var
dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);  
               
if (columns[dilr.listData.columnIndex].editable)  
                {  

                    dgColumn = columns[dilr.listData.columnIndex];  

                    dataGridEvent = new
DataGridEvent(DataGridEvent.ITEM_EDIT_BEGINNING, false, true);  

                    // ITEM_EDIT events are cancelable  

                      
                    dataGridEvent.columnIndex =
dilr.listData.columnIndex;  
                    dataGridEvent.dataField =
dgColumn.dataField;  
                    dataGridEvent.rowIndex =
dilr.listData.rowIndex + verticalScrollPosition;  
                   
dataGridEvent.itemRenderer = r;  
                   
dispatchEvent(dataGridEvent);  
                }  
            }  

              
            super.mouseDoubleClickHandler(event);  

        }  
          
        override protected function
mouseUpHandler(event:MouseEvent):void 
        {  
            var
r:IListItemRenderer;  
            var dgColumn:DataGridColumn;  

              
            r = mouseEventToItemRenderer(event);  

            if (r)  
            {  
                var
dilr:IDropInListItemRenderer = IDropInListItemRenderer(r);  
               
if (columns[dilr.listData.columnIndex].editable)  
                {  

                    dgColumn = columns[dilr.listData.columnIndex];  

                    dgColumn.editable = false;  
                }  

            }  
              
           
super.mouseUpHandler(event);  
              
            if
(dgColumn)  
                dgColumn.editable = true;  
        }  

          
    }  


8,页面显示,Userflex.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:locdg="com.flex.*" 
              
creationComplete="initApp()" 
               minWidth="955"
minHeight="600">  
    <fx:Declarations>  
        <!--
将非可视元素(例如服务、值对象)放在此处 -->  
        <s:RemoteObject id="userdata"
destination="userDao" result="onResult(event)" fault="onFault(event)" />  

        <!--<s:method name="getUserList" result="onResult(event)"
fault="onFault(event)" />-->  
        <s:RemoteObject
id="adduserdata" destination="userDao" result="addResult(event)"
fault="addFault(event)" />  
        <s:RemoteObject
id="updatauserdata" destination="userDao" result="updataResult(event)"
fault="updataFault(event)" />  
        <s:RemoteObject
id="deleteuserdata" destination="userDao" result="deleteResult(event)"
fault="deleteFault(event)" />  
    </fx:Declarations>  
   
<fx:Script>  
        <!--[CDATA[  
            import
com.flex.UserFlex;  
              
            import
mx.collections.ArrayCollection;  
            import mx.controls.Alert;  

            import mx.managers.CursorManager;  
            import
mx.rpc.events.FaultEvent;  
            import mx.rpc.events.ResultEvent;  

              
            public var userFlex:UserFlex = new
UserFlex();  
            public var result:Object = new Object();  

            [Bindable]  
            public var users:ArrayCollection =
new ArrayCollection();  
              
            public function
initApp():void{  
//              userdata.getUserList();  
           
}  
              
            public function
click_handler(event:Event):void{  
               
CursorManager.setBusyCursor();  
                info.text = "正在查询...";  

                userdata.getUserList();  
            }  

            public function adduser_handler(event:Event):void{  

                if(username.text != "" && password.text != ""){  

                    CursorManager.setBusyCursor();  
                   
addinfo.text = "正在添加...";  
                    userFlex.username =
username.text;  
                    userFlex.password = password.text;  

                    adduserdata.addUser(userFlex);  
               
}else{  
                    addinfo.text = "用户名和密码不能为空";  

                }  
            }  
            public function
updataUser(event:Event):void{  
               
if(userlist.selectedItem.username != "" &&
userlist.selectedItem.password != ""){  
                   
CursorManager.setBusyCursor();  
                    info.text =
"正在更新...";  
                    userFlex.id = userlist.selectedItem.id;  

                    userFlex.username = userlist.selectedItem.username;  

                    userFlex.password = userlist.selectedItem.password;  

                    updatauserdata.updataUser(userFlex);  

                }  
//              Alert.show("确认修改?");  

            }  
            public function
deleteUser(event:Event):void{  
               
CursorManager.setBusyCursor();  
                info.text = "正在正在删除...";  

                userFlex.id = userlist.selectedItem.id;  

                deleteuserdata.deleteUser(userFlex);  
            }  

              
            public function
onResult(event:ResultEvent):void{  
                users = event.result as
ArrayCollection;  
                CursorManager.removeBusyCursor();  

                if(users.length==0){  
                   
info.text="取数据成功,为空";  
                }else{  
                   
info.text = "取数据成功";  
                }  
            }  

            public function onFault(event:FaultEvent):void{  

                CursorManager.removeBusyCursor();  
               
info.text="取数据失败";  
            }  
            public function
addResult(event:ResultEvent):void{  
               
CursorManager.removeBusyCursor();  
               
userdata.getUserList();  
                username.text = "";  

                password.text = "";  
                addinfo.text =
"添加成功";  
                //              info.text = "取数据成功";  

            }  
            public function
addFault(event:FaultEvent):void{  
               
CursorManager.removeBusyCursor();  
                addinfo.text="添加失败";  

            }  
            public function
updataResult(event:ResultEvent):void{  
               
CursorManager.removeBusyCursor();  
               
userdata.getUserList();  
                info.text = "修改数据成功";  

            }  
            public function
updataFault(event:FaultEvent):void{  
               
CursorManager.removeBusyCursor();  
                info.text="修改数据失败";  

            }  
            public function
deleteResult(event:ResultEvent):void{  
               
CursorManager.removeBusyCursor();  
               
userdata.getUserList();  
                info.text = "刪除数据成功";  

            }  
            public function
deleteFault(event:FaultEvent):void{  
               
CursorManager.removeBusyCursor();  
                info.text="刪除数据失败";  

            }  
        ]]-->  
    </fx:Script>  
   
<locdg:DoubleClickDataGrid x="61" y="189" id="userlist"
dataProvider="{users}" editable="true">  
        <locdg:columns>  

            <mx:DataGridColumn headerText="ID号" dataField="id"
editable="false"/>  
            <mx:DataGridColumn headerText="账号"
dataField="username" editable="true"/>  
           
<mx:DataGridColumn headerText="密码" dataField="password"
editable="true"/>  
            <mx:DataGridColumn headerText="修改"
width="50" editable="false" >  
                <mx:itemRenderer>  

                    <fx:Component>  
                       
<mx:LinkButton toolTip="修改" click="outerDocument.updataUser(event)"
icon="@Embed('/image/update.gif')">  
                       
</mx:LinkButton>  
                    </fx:Component>  

                </mx:itemRenderer>  
           
</mx:DataGridColumn>  
            <mx:DataGridColumn
headerText="删除" width="50" editable="false" >  
               
<mx:itemRenderer>  
                    <fx:Component>  

                        <mx:LinkButton toolTip="删除"
click="outerDocument.deleteUser(event)"
icon="@Embed('/image/delete.gif')">     
                       
</mx:LinkButton>  
                    </fx:Component>  

                </mx:itemRenderer>  
           
</mx:DataGridColumn>  
        </locdg:columns>  
   
</locdg:DoubleClickDataGrid>  
    <s:Button x="491" y="189"
label="取数据" click="click_handler(event)" />  
    <s:RichText x="606"
y="189" id="info" text="点击按钮取数据"/>  
    <mx:Form x="163" y="72"
borderStyle="solid" borderVisible="true">  
        <mx:FormItem
label="用户名" borderStyle="solid" borderVisible="true">  
           
<s:TextInput id="username" />  
        </mx:FormItem>  

        <mx:FormItem label="密码" borderStyle="solid"
borderVisible="true">  
            <s:TextInput id="password"/>  

        </mx:FormItem>  
    </mx:Form>  
   
<s:Button x="444" y="100" label="添加" click="adduser_handler(event)"/>  

    <s:RichText x="544" y="100" id="addinfo" text="请添加"/>  

</s:Application> 

2
2
分享到:
评论

相关推荐

    \Flex.LCDS.Java入门教程\Flex + LCDS + Java 入门教程

    【Flex + LCDS + Java 入门教程】 本教程旨在帮助初学者理解并掌握如何建立Flex与LCDS(LiveCycle Data Services)结合的项目,并通过RemoteObject实现与Java的通信。Flex是一种富互联网应用程序(RIA)开发框架,...

    Flex+LCDS+JAVA入门

    掌握Flex、LCDS和Java的整合,开发者可以构建出高性能、用户体验优秀的RIA,广泛应用于企业级应用、数据可视化、实时监控等多种场景。在学习过程中,除了文档,还可以参考官方文档、在线教程、社区论坛等资源,不断...

    Flex_LCDS_Java.rar_flex_flex java

    标题中的“Flex_LCDS_Java.rar_flex_flex_java”揭示了本教程主要关注的是Flex、LCDS(LiveCycle Data Services)与Java的结合应用。Flex是一种用于创建富互联网应用程序(RIA)的开发框架,而LCDS是Adobe提供的一个...

    flex lcds java配置

    【Flex + LCDS + Java配置详解】 Flex 是一种用于创建富互联网应用程序(RIA)的开源框架,它允许开发者构建交互性强、用户体验丰富的Web应用。它使用MXML和ActionScript编程语言,结合Adobe Flash Player或Adobe ...

    flex java整合案例(使用BlazeDS和LCDS整合)

    - "Flex+LCDS+Java+整合实例.doc":可能包含了一个详细的整合步骤指南,包括配置、代码示例和屏幕截图。 - "Java平台下基于Flex3开发的实例.doc":可能详细讲解了如何在Java平台上使用Flex3开发应用程序,可能包括...

    Flex-LCDS-Java.rar_LCDS_flex_flex java

    Flex、LCDS(LiveCycle Data Services)和Java的结合是Adobe Flex开发中常见的技术栈,主要用于构建富互联网应用程序(Rich Internet Applications,RIAs)。本文将深入探讨这些技术及其相互之间的协同工作方式。 ...

    Flex + LCDS + Java

    【Flex + LCDS + Java 入门教程】 Flex 是一种基于 Adobe Flash 技术的开源框架,用于构建富互联网应用程序(RIA)。它提供了一个强大的图形用户界面组件库,允许开发者创建交互性强、视觉效果丰富的Web应用程序。...

    Flex + LCDS + Java 入门教程

    Flex + LCDS + Java 入门教程旨在帮助初学者了解如何构建使用Flex、LCDS(LiveCycle Data Services)和Java的集成应用。Flex是一种富互联网应用程序(RIA)开发框架,用于创建交互式用户界面,而LCDS是Adobe提供的...

    Flex + LCDS + Java 入门教程.pdf

    ### Flex + LCDS + Java 入门教程精要 #### 引言 随着技术的发展,Flex、LCDS以及Java成为了构建高效、交互性强的应用程序的重要工具。本文将深入解析Flex + LCDS + Java的核心概念与实践技巧,为初学者提供一个...

    Flex LCDS Java 简单教程

    Flex LCDS Java 简单教程 源码

    Flex + LCDS + Java 利用Eclipse Plugin开发Flex入门教程

    【Flex + LCDS + Java 利用Eclipse Plugin开发Flex入门教程】 Flex是一种基于Adobe Flash技术的开源框架,用于构建富互联网应用程序(RIA)。它允许开发者创建交互式、响应式的用户界面,通常与后端数据服务配合...

    Flex+LCDS+Java+入门教程

    10. **案例分析**:通过实际案例来演示Flex、LCDS和Java的整合,帮助你更好地理解和应用这些技术。 这个入门教程将引导你逐步掌握Flex、LCDS和Java的综合运用,为开发高质量的RIA应用打下坚实的基础。通过深入学习...

    Flex_LCDS_Java.rar_flex

    Flex LCDS Java是一款用于构建富互联网应用程序(RIA)的技术组合,它将Adobe Flex前端与Java后端集成,提供高效的数据传输和丰富的用户交互体验。这个压缩包“Flex_LCDS_Java.rar_flex”显然是一个关于如何使用Flex...

    Flex+LCDS+Java入门教程

    本教程旨在帮助初学者了解如何创建Flex与LCDS相结合的工程项目,并通过整合和部署J2EE项目与LCDS项目来实现简单功能。教程通过图文并茂的方式,详细介绍了创建项目的步骤以及如何使用RemoteObject进行简单的请求和...

    Flex + LCDS + Java 入门教程.doc

    Flex + LCDS + Java 入门教程 Flex是一种开源的客户端富互联网应用程序(RIA)开发框架,由Adobe提供,主要用于创建交互式、图形丰富的Web应用。它基于ActionScript编程语言和MXML标记语言,允许开发者构建用户界面...

Global site tag (gtag.js) - Google Analytics