这里需要用到services-config.xml配置文件,我个人理解的该配置文件就是一个Flex与业务层间的通道。
假设业务层有一个java类TestAction.java
①那么services-config.xml配置文件中的写法就是这样:
<destination id="TestAction">
<channels>
<channel ref="spring-amf" />
</channels>
</destination>
完成这个配置之后就可以在Flex层调用TestAction里的方法了。
②在MXML文件中这样写:
<mx:RemoteObject id="TestAction"
destination="TestAction"
showBusyCursor="true">
<mx:method name="findDataList"
result="processFindDataList(event.result)">
</mx:method>
</mx:RemoteObject>
其中destination就是你在services-config.xml配置文件声明的通道,
id是你在对应的AS文件可以用的名字。id你可以用随便的名字,你id起的是什么名字那你在AS文件中就用什么名字。
③AS文件中这样写
private function getDataList():void
{
TestAction.findDataList(String para1,String para2,.....)
}
findDataList是TestAction中声明的方法。TestAction.findDataList返回的结果用processFindDataList方法接收(在mx:RemoteObject 中已经声明)
private function processFindDataList(result:Object):void
{
if(result != null)
{
处理
}
else
{
Alert.show("没有你想要的结果")
}
}
flex与java实现增删改查用的是MySQL数据库。
1,建一个userdb库,再建userinfo表,字 段:id(int),username(varchar),password(varchar)。
view plaincopy to clipboardprint?
create database userdb
use userdb
create table userinfo(
id int(10) not null auto_increment,
username varchar(20),
password varchar(20),
primary key(id))
2,DBConnection.java
view plaincopy to clipboardprint?
package com.datainfo
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException
public class DBConnection {
public static Connection getConnection() throws ClassNotFoundException,
SQLException {
Connection conn = null
String driver = "com.mysql.jdbc.Driver"
String url = "jdbc:mysql://localhost:3306/userdb"
String username = "root"
String password = "mysql"
Class.forName(driver)
conn = DriverManager.getConnection(url, username, password)
return conn
}
}
3,User.java
view plaincopy to clipboardprint?
package com.datainfo
public class User {
private int id
private String username
private String password
public User() {
}
/**
* @return the id
*/
public int getId() {
return id
}
/**
* @param id
*the id to set
*/
public void setId(int id) {
this.id = id
}
/**
* @return the username
*/
public String getUsername() {
return username
}
/**
* @param username
*the username to set
*/
public void setUsername(String username) {
this.username = username
}
/**
* @return the password
*/
public String getPassword() {
return password
}
/**
* @param password
*the password to set
*/
public void setPassword(String password) {
this.password = password
}
}
4,UserDAO.java
view plaincopy to clipboardprint?
package com.datainfo
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
import java.util.ArrayList
import com.datainfo.DBConnection
public class UserDAO {
public ArrayList getUserList() throws ClassNotFoundException, SQLException {
Connection conn = DBConnection.getConnection()
Statement stmt = conn.createStatement()
ResultSet rs = stmt.executeQuery("select * from userinfo")
ArrayList userList = null
try {
userList = new ArrayList()
while (rs.next()) {
User user = new User()
user.setId(rs.getInt("id"))
user.setUsername(rs.getString("username"))
user.setPassword(rs.getString("password"))
userList.add(user)
}
rs.close()
stmt.close()
conn.close()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return userList
}
public void addUser(User user) throws ClassNotFoundException, SQLException {
Connection conn = DBConnection.getConnection()
String sql = "insert into userinfo (username,password) values (?,?)"
try {
PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setString(1, user.getUsername())
pstmt.setString(2, user.getPassword())
pstmt.executeUpdate()
pstmt.close()
conn.close()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public void updataUser(User user) throws ClassNotFoundException,
SQLException {
Connection conn = DBConnection.getConnection()
String sql = "update userinfo set username=?,password=? where id=?"
try {
PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setString(1, user.getUsername())
pstmt.setString(2, user.getPassword())
pstmt.setInt(3, user.getId())
pstmt.executeUpdate()
pstmt.close()
conn.close()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public void deleteUser(User user) throws ClassNotFoundException,
SQLException {
Connection conn = DBConnection.getConnection()
String sql = "delete from userinfo where id =?"
try {
PreparedStatement pstmt = conn.prepareStatement(sql)
pstmt.setInt(1, user.getId())
pstmt.executeUpdate()
pstmt.close()
conn.close()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}