浏览 3685 次
锁定老帖子 主题:Flex与JAVA对数据的读 , 写 操作
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-05-08
最后修改:2009-05-11
CREATE TABLE `t_user` ( `I_ID` int(11) NOT NULL AUTO_INCREMENT, `V_UserName` varchar(50) NOT NULL, `V_UserPassword` varchar(50) DEFAULT NULL, `D_UserBirthday` varchar(200) DEFAULT NULL, `I_UserDep` varchar(50) DEFAULT NULL, `V_UserEmail` varchar(200) DEFAULT NULL, `V_UserAddress` varchar(500) DEFAULT NULL, PRIMARY KEY (`I_ID`) ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=gbk; 数据库连接类,查询数据,添加数据 package net.zhuoqun.connectDB; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class DataServiceImpl { public static Connection getConnection() { Connection c = null; try { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/mytest"; String username = "root"; String password = ""; Class.forName(driver); c = DriverManager.getConnection(url, username, password); } catch (Exception e) { System.out.println(e.getMessage()); } return c; } // 查询用户 public List getAllUser() throws SQLException { Connection c = getConnection(); Statement st = c.createStatement(); String sqlText = "select v_username,v_userpassword,d_userbirthday,v_useremail from t_user"; ResultSet rs = st.executeQuery(sqlText); ArrayList userList = new ArrayList(); while (rs.next()) { User temp = new User(); temp.setUsername(rs.getString("v_username")); temp.setUserpassword(rs.getString("v_userpassword")); temp.setUserbirthday(rs.getString("d_userbirthday")); temp.setUseremail(rs.getString("v_useremail")); userList.add(temp); } return userList; } // 添加用户 public void addUser(User user) throws SQLException { Connection c = getConnection(); String sqlText = "insert into t_user(V_UserName,V_UserPassword,D_UserBirthday,I_UserDep,V_UserEmail,V_UserAddress) " +"values(?,?,?,?,?,?)"; try { PreparedStatement pstmt = c.prepareStatement(sqlText); pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getUserpassword()); pstmt.setString(3, "1983-05-16"); pstmt.setString(4, user.getDep()); pstmt.setString(5, user.getUseremail()); pstmt.setString(6, user.getAddress()); pstmt.execute(); } catch (Exception e) { e.printStackTrace(); } } } User类 package net.zhuoqun.connectDB; import java.io.Serializable; import java.util.Date; public class User implements Serializable{ private String username; //姓名 private String userpassword; //密码 private String useremail; //邮箱 private String userbirthday; //生日 private String dep; //部门 private String address; //地址 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpassword() { return userpassword; } public void setUserpassword(String userpassword) { this.userpassword = userpassword; } public String getUseremail() { return useremail; } public void setUseremail(String useremail) { this.useremail = useremail; } public String getUserbirthday() { return userbirthday; } public void setUserbirthday(String userbirthday) { this.userbirthday = userbirthday; } public String getDep() { return dep; } public void setDep(String dep) { this.dep = dep; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } User.as package com.gt.flex { //与JAVA类中的 User.java 对应 包括 属性名 和 类型 //如果为private的话,要想访问,就要写出对应的setter/getter方法 [RemoteClass(alias="net.zhuoqun.connectDB.User")] public class User { public function User() { } public var username:String; public var userpassword:String; public var useremail:String; public var userbirthday:String public var dep:String; public var address:String; } } register.mxml <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:ns1="*"> <ns1:RegisterForm2 x="30" y="10"> </ns1:RegisterForm2> </mx:Application> RegisterForm2.mxml 是我自定义的控件 <?xml version="1.0" encoding="utf-8"?> <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="522" layout="vertical" verticalAlign="middle" backgroundColor="white" pageTitle="用户注册表单" xmlns:local="*"> <mx:Script> <![CDATA[ import com.gt.flex.User; import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import mx.controls.Alert; import mx.utils.ArrayUtil; //User.as var user:User=new User(); private function resultHandler(event:ResultEvent):void { Alert.show("注册成功"); } private function faultHandler(event:FaultEvent):void { Alert.show(event.fault.message, "失败"); } private function adduser():void { // user 为 User.as 的对象 user.username=names.text; //姓名 user.userpassword=password.text; //密码 user.userbirthday=birthday.text; //生日 user.address=address.text; //地址 //user.dep=dep.selectedItem.@label; //user.dep=dep.text; user.dep=dep.selectedItem.@data; //部门 @data CA/OR 表示<mx:XMLList id="statesXMLList">中的data数据 user.useremail=email.text; //邮箱 userinfo.addUser(user); //调用DataServiceImpl.java中的addUser(User user)方法 } ]]> </mx:Script> <!--dataService 为 remoting-config.xml中的 id--> <mx:RemoteObject id="userinfo" destination="dataService" result="resultHandler(event)" fault="faultHandler(event)"/> <mx:XMLList id="statesXMLList"> <state label="生产中心" data="CA"/> <state label="运营中心" data="OR"/> </mx:XMLList> <mx:Form width="380" height="482"> <mx:FormHeading label="员工注册" width="319" textAlign="center"/> <mx:FormItem required="true" label="姓名"> <mx:TextInput id="names" maxChars="96" width="281"/> </mx:FormItem> <mx:FormItem required="true" label="密码"> <mx:TextInput id="password" maxChars="128" width="281" displayAsPassword="true"/> </mx:FormItem> <mx:FormItem label="生日"> <mx:TextInput id="birthday" maxChars="128" width="281"/> </mx:FormItem> <mx:FormItem required="true" label="地址"> <mx:TextInput id="address" maxChars="128" width="281"/> </mx:FormItem> <mx:FormItem required="true" label="部门"> <mx:ComboBox id="dep" prompt="选择部门" selectedIndex="-1" dataProvider="{statesXMLList}" labelField="@label"/> <!--@label表示mx:XMLList id="statesXMLList"中的label--> </mx:FormItem> <mx:FormItem required="true" label="邮箱"> <mx:TextInput id="email" maxChars="20" width="281"/> </mx:FormItem> <mx:FormItem width="319"> <mx:HBox> <mx:Button label="注册" click="adduser()"/> <mx:Button label="重置"/> </mx:HBox> </mx:FormItem> </mx:Form> </mx:Panel> 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="dataService"> <properties> <source>net.zhuoqun.connectDB.DataServiceImpl</source> </properties> </destination> </service> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-01-14
为什么flex发布以后index.html是404错误,找不到该页呢?可是我看发布在tomcat里的有这个文件呀。
|
|
返回顶楼 | |