as3.0怎么连接mysql数据库为了使用简单,我做了一个简单的包装类(包含一个数据结构):
package com.easily.ds
{
public class DataBaseData
{
public var host:String;
public var port:int;
public var username:String;
public var password:String;
public var database:String;
}
}
package com.easily.util
{
import com.easily.ds.DataBaseData;
import com.maclema.mysql.Connection;
import com.maclema.mysql.Field;
import com.maclema.mysql.MySqlResponse;
import com.maclema.mysql.MySqlToken;
import com.maclema.mysql.ResultSet;
import com.maclema.mysql.Statement;
import flash.events.Event;
import flash.events.EventDispatcher;
import mx.rpc.AsyncResponder;
/**
* @author Easily
*/
public class DataBase extends EventDispatcher
{
private var mDataBase:DataBaseData;
private var mConnection:Connection;
public function DataBase(database:DataBaseData)
{
mDataBase = database;
}
public function connect():void
{
mConnection = new Connection(mDataBase.host, mDataBase.port,
mDataBase.username, mDataBase.password, mDataBase.database);
mConnection.addEventListener(Event.CONNECT, onConnected);
mConnection.connect();
function onConnected(event:Event):void
{
mConnection.removeEventListener(Event.CONNECT, onConnected);
dispatchEvent(event);
}
}
public function disconnect():void
{
mConnection.disconnect();
}
public function select(sql:String, completeHandler:Function, errorHandler:Function = null):void
{
var st:Statement = mConnection.createStatement();
var token:MySqlToken = st.executeQuery(sql);
var responder:AsyncResponder = new AsyncResponder(resultHandler, faultHandler, token);
token.addResponder(responder);
function resultHandler(result:Object/*ResultSet*/, token:Object/*MySqlToken*/):void
{
var data:Array = [];
if (result is ResultSet)
{
var fieldList:Array = result.getColumns();
while (result.next())
{
var item:Object = {};
for each (var field:Field in fieldList)
{
item[field.getName()] = result.getString(field.getName());
}
data.push(item);
}
}
completeHandler(data);
}
function faultHandler(info:Object, token:Object):void
{
if (errorHandler == null) return;
errorHandler();
}
}
public function insert(sql:String, completeHandler:Function, errorHandler:Function = null):void
{
var st:Statement = mConnection.createStatement();
var token:MySqlToken = st.executeQuery(sql);
var responder:AsyncResponder = new AsyncResponder(resultHandler, faultHandler, token);
token.addResponder(responder);
function resultHandler(result:Object/*MySqlResponse*/, token:Object/*MySqlToken*/):void
{
completeHandler(result.insertID);
}
function faultHandler(info:Object, token:Object):void
{
if (errorHandler == null) return;
errorHandler();
}
}
public function remove(sql:String, completeHandler:Function, errorHandler:Function = null):void
{
var st:Statement = mConnection.createStatement();
var token:MySqlToken = st.executeQuery(sql);
var responder:AsyncResponder = new AsyncResponder(resultHandler, faultHandler, token);
token.addResponder(responder);
function resultHandler(result:Object/*MySqlResponse*/, token:Object/*MySqlToken*/):void
{
completeHandler();
}
function faultHandler(info:Object, token:Object):void
{
if (errorHandler == null) return;
errorHandler();
}
}
}
}
其中有3个方法,select,insert,remove , 分别对应了SQL语句中的select,insert,delete
推荐阅读
- 战略布阵游戏大全,好玩的战略部署游戏
- 包含postgresqlstraster的词条
- 即时战略单机游戏安卓版,即时战略游戏手机版
- asp.net2.0入门经典pdf,aspnet pdf下载
- qt怎么使用mysql的简单介绍
- sqlserver报列名无效,sql列名与表定义不匹配
- flutter兼容ios版本,flutter安卓和ios ui一致吗
- mysql怎么写中文 mysql怎样设置中文字体
- wordpress怎么换大标题,wordpress文章页样式修改