Flex与Java基于RemoteObject进行数据库(mysql)操作
首先呢,一些基本配置这里就不作一一介绍了(可以去网上查查 很多的配置视频、文档,这里只是代码)
?
1)建立一个Java+Flex工程
?
2)新建java类
?
? 1.AddDataImpl.java
?
?
package com;import java.sql.*;import com.ConnectionFactory;public class AddDataImpl { ?String sql;? // 定义类型?Connection conn = null;?Statement stmt = null;?int rs; ?public AddDataImpl(){} ?public NoticeInfo[] addData(){? ?? try{??? conn = ConnectionFactory.getConnection();??? stmt = conn.createStatement(); ??? String sql = "insert into userinfo values ('11','11','11','1111-11-11') ";??? rs=stmt.executeUpdate(sql);??? stmt.close();??? ?? }catch(SQLException e){??? e.printStackTrace(); ?? }?return null;?} }?
2.ConnectionFactory.java
?
package com;
import java.sql.*; ?public class ConnectionFactory {?private static ConnectionFactory ref = new ConnectionFactory();? // 连接mysql数据库, database : test? user : root? password : 1272107226? private ConnectionFactory()? {?? try{??? Class.forName("com.mysql.jdbc.Driver");?? }catch(ClassNotFoundException e)?? {??? System.out.println("ERROR: exception loading driver class");?? }? }? public static Connection getConnection() throws SQLException{?? ?? String url = new String ("jdbc:mysql://localhost:3306/test?user=root&password=root");?? return DriverManager.getConnection(url);? }? ? public static void close(ResultSet rs)? {?? try{??? rs.close();?? }catch(Exception ignored){}? }? public static void close(Statement stmt)? {?? try{??? stmt.close();?? }catch(Exception ignored){}? }? public static void close(Connection conn)? {?? try{??? conn.close();?? }catch(Exception ignored){}? }?? }?3.DataServiceImpl.java
?
package com;import java.sql.*;import java.util.ArrayList;import com.ConnectionFactory;public class DataServiceImpl { String sql; // 定义类型 Connection conn = null; Statement stmt = null; ResultSet rs = null; public DataServiceImpl(){} public NoticeInfo[] getNotices(){ ArrayList noticeList = new ArrayList(); try{ conn = ConnectionFactory.getConnection(); stmt = conn.createStatement(); String sql = "select userid, username, contents, dates from userinfo "; rs = stmt.executeQuery(sql); while(rs.next()){ NoticeInfo temp = new NoticeInfo(); temp.setUserid(rs.getString("userid")); temp.setUsername(rs.getString("username")); temp.setContents(rs.getString("contents")); temp.setDates(rs.getString("dates")); noticeList.add(temp); } NoticeInfo[] notices = new NoticeInfo[noticeList.size()]; for(int i=0;i < noticeList.size();i++){ notices[i] = (NoticeInfo) noticeList.get(i); } return notices; }catch(SQLException e){ e.printStackTrace(); return null; } }}?4.NoticeInfo.java
?
package com;public class NoticeInfo { private String userid; private String username; private String contents; private String dates; public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } public String getDates() { return dates; } public void setDates(String dates) { this.dates = dates; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }}?
?
?
3)新建Flex页面
?
1.? AddDataText.mxml
?
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" fontSize="12" creationComplete="initApp()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.utils.ArrayUtil; private function initApp():void { addDataId.addData(); } ]]> </mx:Script> <mx:RemoteObject id="addDataId" destination="addData"/> <mx:Button label="插入数据" click="initApp()"/> </mx:Application>?
2.? JavaMysql.mxml
??
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12" creationComplete="initApp()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.utils.ArrayUtil; private function initApp():void { getData.getNotices(); } private function proccessResult(result:Object):void { myDG.dataProvider = ArrayUtil.toArray(result); } private function addDataFunction():void { addDataId.addData(); } ]]> </mx:Script> <mx:RemoteObject id="getData" destination="dataService" result="proccessResult(event.result)" fault="Alert.show(event.fault.faultString,'Error')"/> <mx:RemoteObject id="addDataId" destination="addData"/> <mx:Button label="插入数据" click="addDataFunction()" x="373" y="299"/> <mx:DataGrid id="myDG" x="236" y="103"> <mx:columns> <mx:DataGridColumn headerText="id" dataField="userid"/> <mx:DataGridColumn headerText="username" dataField="username"/> <mx:DataGridColumn headerText="contents" dataField="contents"/> <mx:DataGridColumn headerText="dates" dataField="dates"/> </mx:columns> </mx:DataGrid> </mx:Application>4)配置remoting-config.xml
?
<?xml version="1.0" encoding="UTF-8"?><service id="remoting-service" default="true"/> </adapters> <default-channels> <channel ref="my-amf"/> </default-channels> <destination id="dataService"> <properties> <source>com.DataServiceImpl</source> </properties> </destination> <destination id="addData"> <properties> <source>com.AddDataImpl</source> </properties> </destination> </service>
??
5)创建数据库
?
DROP DATABASE IF EXISTS `test`;CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */;USE `test`;CREATE TABLE `userinfo` ( `userid` int(11) NOT NULL auto_increment, `username` varchar(20) default NULL, `contents` varchar(20) default NULL, `dates` date default NULL, PRIMARY KEY (`userid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `userinfo` VALUES (1,'as','as','2010-05-03');INSERT INTO `userinfo` VALUES (2,'admin','wo','2010-10-12');INSERT INTO `userinfo` VALUES (3,'admin3','wo','2010-10-12');INSERT INTO `userinfo` VALUES (4,'admin4','wo','2010-10-12');INSERT INTO `userinfo` VALUES (5,'admin5','wo','2010-10-12');INSERT INTO `userinfo` VALUES (6,'admin6','wo','2010-10-12');INSERT INTO `userinfo` VALUES (7,'admin7','wo','2010-10-12');INSERT INTO `userinfo` VALUES (8,'admin8','wo','2010-10-12');INSERT INTO `userinfo` VALUES (9,'admin9','wo','2010-10-12');INSERT INTO `userinfo` VALUES (10,'admin10','wo','2011-11-11');
?
6)引进 连接mysql的包
?
名字为:mysql-connector-java-5.1.7-bin.jar
?
最后调试,一般都能成功,要还是不能成功的请发邮箱:xinyuzas611@163.com???
?