- I'm VC 首页
- Visual C++
- C++
- C语言
- Visual C#
- DotNet
- VB.Net
- Java
- Android
- Visual Basic
- Delphi
- 汇编
- F#
- 软件架构
- 数据结构/算法
- 软件工程
- Web开发
- |ASP.Net
- |PHP
- |JSP
- |ASP
- |JavaScript
- |AJAX
- |Silverlight
- |Flex
- |JQuery
- |prototype
- |MooTools
- |ExtJS
- |dojo
- 数据库
- |Oracle
- |MSSQL
- |MySQL
- |DB2
- |Sybase
- |PowerBuilder
- |Access
- |其他数据库
- |存储技术
- |数据库基础/原理
Flex+blazeds实现与mySQL数据库的连接
刚刚学习flex不久,这个小例子通过remoteobject的通讯方法。实现flex与mySQL数据库 的交互。
1 使用mySQL中的test数据库,建表名为admin.表中字段为: id username userpwd
(最终目的:将数据库表中的记录最终显示在flex端的DateGrid 组件中显示出来)
2 建立myEclipse的工程。新建->web project 输入工程名后 。需要将事先下载的Blazeds包粘贴至工程目录下。并且将mysql的驱动放到工程目录lib的文 件夹下
(我使用版本blazeds_turnkey_3-0-0-544.zip。将解压后的文件夹中的 blazeds.war改成rar的拓展名,并解压,将得到的META-INF和WEB-INF两个文件夹拷入刚建成 的工程下webroot下)
3 编写方法ConnectionHelper.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionHelper
{
private String url;
private static ConnectionHelper instance;
private ConnectionHelper()
{
try {
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:MySQL://localhost/test";
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
if (instance == null) {
instance = new ConnectionHelper();
}
try {
return DriverManager.getConnection (instance.url,"root","root");
} catch (SQLException e) {
throw e;
}
}
public static void close(Connection connection)
{
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
DAOException.java
public class DAOException extends RuntimeException
{
static final long serialVersionUID = -1881205326938716446L;
public DAOException(String message)
{
super(message);
}
public DAOException(Throwable cause)
{
super(cause);
}
public DAOException(String message, Throwable cause)
{
super(message, cause);
}
}
以上是数据库连接所需方法。然后根据数据库中表的 结构写下所需的方法类。
Admin.java
import java.io.Serializable;
public class Admin implements Serializable {
static final long serialVersionUID = 103844514947365244L;
private int id;
private String username;
private String userpwd;
public Admin() {
}
public Admin(int id,String username,String userpwd) {
this.id = id;
this.username=username;
this.userpwd=userpwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
AdminService.java
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
import flex.jie.ConnectionHelper;
import flex.jie.DAOException;
public class AdminService {
public List getAdmins() throws DAOException {
List list = new ArrayList();
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(new Admin(rs.getInt("id"),
rs.getString ("username"),
rs.getString ("userpwd")));
}
} catch (SQLException e) {
e.printStackTrace();
throw new DAOException(e);
} finally {
ConnectionHelper.close(c);
}
return list;
}
}
重要一步:

在 remoting-config.xml文件中 添加(source处根据实际的建包名填写)
<destination id="admin">
<properties>
<source>flex.jie.user.AdminService</source>
</properties>
</destination>
至此。myEclipse工程中所需代码以及配置已 经完成。将工程部署并发布在Tomacat下面。
接下来,flex端的步骤。
首先 新建Flex Project ,输入工程名。在server technology处选择J2EE,点击next

填写刚刚 发布在tomcat下的目录名。点击完成。
在默认的mxml中的代码如下:
index.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#FFFFFF">
<mx:RemoteObject id="srv" destination="admin"/>
<mx:DataGrid dataProvider="{srv.getAdmins.lastResult}" width="100%" height="100%"/>
<mx:Button label="Get Data" click="srv.getAdmins() "/>
</mx:Application>
在tomcat启动的前提下,点击运行即可。

或者想显 示指定的列。只需要稍作修改即可
index.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#FFFFFF" creationComplete="srv.getAdmins() ">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
[Bindable]public var ad:ArrayCollection;
private function adminHandler(event:ResultEvent):void
{
ad= event.result as ArrayCollection
}
]]>
</mx:Script>
<mx:RemoteObject id="srv" destination="admin">
<mx:method name="getAdmins" result="adminHandler(event)"/>
</mx:RemoteObject>
<mx:DataGrid dataProvider="{ad}">
<mx:columns>
<mx:DataGridColumn headerText="用户名" dataField="username"/>
<mx:DataGridColumn headerText="密码" dataField="userpwd"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
显示为:

说明:此 时的destination对应remote-config.xml中的id号 。建立连接。从而flex端所用到的java方 法可以找到。返回数据库中所建表的信息
2011-07-01 18:41
阅读: 次
I'm VC , Just U know Y
本站部分文章来源于互联网,版权归原作者所有。
延伸阅读:
flex in action (五)flex与后台Java进行交互(1)
flex in action (六)flex与后台Java进行交互(2)