ConnectionHelper.javaimportjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;publicclassConnectionHelper
{privateString url;privatestaticConnectionHelper instance;privateConnectionHelper()
{try{
Class.forName("com.mysql.jdbc.Driver");
url="jdbc:MySQL://localhost/test";
}catch(Exception e) {
e.printStackTrace();
}
}publicstaticConnection getConnection()throwsSQLException {if(instance==null) {
instance=newConnectionHelper();
}try{returnDriverManager.getConnection(instance.url,"root","root");
}catch(SQLException e) {throwe;
}
}publicstaticvoidclose(Connection connection)
{try{if(connection!=null) {
connection.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
DAOException.java
DAOException.javapublicclassDAOExceptionextendsRuntimeException
{staticfinallongserialVersionUID=-1881205326938716446L;publicDAOException(String message)
{super(message);
}publicDAOException(Throwable cause)
{super(cause);
}publicDAOException(String message, Throwable cause)
{super(message, cause);
}
}
以上是数据库连接所需方法。然后根据数据库中表的结构写下所需的方法类。
Admin.java
Admin.javaimportjava.io.Serializable;publicclassAdminimplementsSerializable {staticfinallongserialVersionUID=103844514947365244L;privateintid;privateString username;privateString userpwd;publicAdmin() {
}publicAdmin(intid,String username,String userpwd) {this.id=id;this.username=username;this.userpwd=userpwd;
}publicintgetId() {returnid;
}publicvoidsetId(intid) {this.id=id;
}publicString getUsername() {returnusername;
}publicvoidsetUsername(String username) {this.username=username;
}publicString getUserpwd() {returnuserpwd;
}publicvoidsetUserpwd(String userpwd) {this.userpwd=userpwd;
}
}
AdminService.java
AdminService.javaimportjava.util.ArrayList;importjava.util.List;importjava.sql.*;importflex.jie.ConnectionHelper;importflex.jie.DAOException;publicclassAdminService {publicList getAdmins()throwsDAOException {
List list=newArrayList();
Connection c=null;try{
c=ConnectionHelper.getConnection();
Statement s=c.createStatement();
ResultSet rs=s.executeQuery("SELECT * FROM admin ORDER BY id");while(rs.next()) {
list.add(newAdmin(rs.getInt("id"),
rs.getString("username"),
rs.getString("userpwd")));
}
}catch(SQLException e) {
e.printStackTrace();thrownewDAOException(e);
}finally{
ConnectionHelper.close(c);
}returnlist;
}
}
重要一步:
在remoting-config.xml文件中 添加(source处根据实际的建包名填写)
flex.jie.user.AdminService
至此。myEclipse工程中所需代码以及配置已经完成。将工程部署并发布在Tomacat下面。
接下来,flex端的步骤。
首先 新建Flex Project ,输入工程名。在server technology处选择J2EE,点击next
填写刚刚发布在tomcat下的目录名。点击完成。
在默认的mxml中的代码如下:
index.mxml<?xml version="1.0" encoding="utf-8"?>
在tomcat启动的前提下,点击运行即可。
或者想显示指定的列。只需要稍作修改即可
index.mxml<?xml version="1.0" encoding="utf-8"?>
[Bindable]public var ad:ArrayCollection;
private function adminHandler(event:ResultEvent):void
{
ad= event.result as ArrayCollection
}]]>
显示为:
说明:此时的destination对应remote-config.xml中的id号 。建立连接。从而flex端所用到的java方法可以找到。返回数据库中所建表的信息