要求
關於 Myeclipse10.6+Flash Builder 4.6外掛安裝配置 請閱讀:
Myeclipse10.6+Flash Builder 4.6安裝配置
程式截圖:
程式部分程式碼
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
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。