歡迎您光臨本站 註冊首頁

Flex與java交互之資料庫數據讀取

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

前幾天有網友要我寫一篇關於flex與資料庫交互的例子,一直由於時間緊沒能寫,今天晚上特地花點時間寫了個例子,希望能給大家一點幫助,其實flex與資料庫交互很簡單的,我主要是通過java與資料庫交互讀取數據,然後flex和java進行交互,把java讀取的數據在flex前端顯示出來,好了,說了一堆廢話,下面代碼說明問題.

對於建立一個flex web應用的步驟我就不多說了哈.

要求大家到資料庫建立一個資料庫,在我這例子中資料庫名為userDb,建立一個表userInfo,裡面設置兩個欄位一個為id,一個為name.第二,創建以下java代碼在src目錄下

User.java
package org.rjb.java;

public class User {
private String name;


public String getName() {
return name;
}

public void setName(String name){

this.name=name;
}

}


UserDao.java
package org.rjb.java;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

public static Connection getConnection(){
Connection c
=null;

try{
String driver
="com.mysql.jdbc.Driver";
String url
="jdbc:mysql://localhost:3306/userDb";
String username
="root";
String password
="1235";
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();
ResultSet rs
=st.executeQuery("select * from UserInfo");
ArrayList userList
=new ArrayList<User>();
while(rs.next()){
User u
=new User();

u.setName(rs.getString(
"name"));
userList.add(u);
}

return userList;
}


}

以上是java端的全部代碼了,下面是flex端的代碼.

第三,創建以下代碼在flex_src下:

User.as
package org.rjb.flex
{

[RemoteClass(alias
="org.rjb.java.User")]
public class User
{
private var _name:String;
public function User()
{

}

public function get name():String{
return this._name;
}

public function set name(name:String):void{

this._name=name;
}

}

}


User.as
package org.rjb.flex
{
[RemoteClass(alias
="org.rjb.java.User")]
public class User

{
private var _name:String;
public function User()
{
}

public function get name():String{

return this._name;
}

public function set name(name:String):void{
this._name=name;
}

}

}

接下來是mxml代碼,也就是我們真正展示我們數據的界面

FlexWithJava.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:RemoteObject id="ud" destination="UserDao" result="onResult(event)" fault="onFault()" />
<mx:DataGrid dataProvider="{users}">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="name" />

</mx:columns>
</mx:DataGrid>
<mx:Label id="info" />
<mx:Button label="click" click="clickFun()" />
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
public var users:ArrayCollection=new ArrayCollection();

public function clickFun():void{
ud.getAllUser();
}
public function onResult(event:ResultEvent):void{
users=ArrayCollection(event.result);
info.text="get data successfully";
}
public function onFault():void{
info.text="Error";
}
]]>
</mx:Script>
</mx:Application>

第四,配置remoting-config.xml,在裡面加上如下配置文件:

<destination id="UserDao">

<properties>
<source>org.rjb.java.UserDao</source>
</properties>
</destination>

ok,就這麼簡單,呵呵,趕快運行,看看效果呢.


[火星人 ] Flex與java交互之資料庫數據讀取已經有348次圍觀

http://coctec.com/docs/java/show-post-62029.html