聲明:本人剛剛接觸flex兩天,希望不要給初學者一個錯誤的引導。
本例子的bug :在update時不能及時刷新數(shù)據(jù)。不清楚用DataGrid的啥方法了。誰知道請告訴我。謝謝!? QQ:706627987? MSN:wangfeilong_cn@hotmail.com
運行及開發(fā)環(huán)境:
flexbuilder2+eclipse3.1.1+tocmcat5+sqlserver2k
數(shù)據(jù)庫信息(數(shù)據(jù)庫名,用戶名,密碼)在下面的ConnectionManager.java中
這里面? 數(shù)據(jù)庫名字為test?,表名字也為test?,用戶名為sa? 密碼為pwd
? 表結構:

Eclipse
的工程結構:
?
?
FlexBuilder2
的工程結構
?
將
flexbuilder2
編譯
employee.mxml
后生成的文件
(
在
flexbuilder2
建立的工程的
bin
目錄下
),
拷貝到
eclipse
的工程目錄的
WebRoot
下。
將下面的四個jsp文件(employeelist.jsp,employeedelete.jsp,employeeupdate.jsp,employee.jsp)拷貝到eclipse的工程目錄的WebRoot下。
運行URL:
http://localhost:8080/flex/employee.html
?
MXML內(nèi)容:
employee.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="?
?<mx:HTTPService id="employeeSrv"? url="employee.jsp"? resultFormat="xml">
??????? <mx:request>??????????????????
??????????? <email>{email.text}</email>
??????????? <phone>{phone.text}</phone>
??????????? <zipcode>{zipcode.text}</zipcode>
??????? </mx:request>???????
??? </mx:HTTPService>
???
?????? <mx:HTTPService id="employeeinfo_update"? url="employeeupdate.jsp" resultFormat="xml">
???????? <mx:request>??????????????????
????????????? <email>{email.text}</email>
????????????? <phone>{phone.text}</phone>
????????????? <zipcode>{zipcode.text}</zipcode>
????????? </mx:request>???????
??? </mx:HTTPService>
???
???
???
? <mx:HTTPService id="employeeinfo_delete"? url="employeedelete.jsp" resultFormat="xml">
????????????????? <mx:request>??????????????????
????????????? <email>{email.text}</email>
????????????? <phone>{phone.text}</phone>
????????????? <zipcode>{zipcode.text}</zipcode>
????????? </mx:request>???????
??? </mx:HTTPService>
???
???
???
??
?? <mx:HTTPService id="employee_query"? url="employeelist.jsp" useProxy="false" method="POST"/>???
???
?<mx:Form>
?? <mx:FormHeading label="增加員工"></mx:FormHeading>?
?
?
??????? <mx:FormItem label="Email">
??????????? <mx:TextInput id="email" width="200"? text="{dg.selectedItem.email}"/>
??????? </mx:FormItem>
??????? <mx:FormItem label="Phone">
??????????? <mx:TextInput id="phone" width="200" text="{dg.selectedItem.phone}" />
??????? </mx:FormItem>
??????? <mx:FormItem label="ZipCode">
??????????? <mx:TextInput id="zipcode" width="60" text="{dg.selectedItem.zipcode}" />
??????? </mx:FormItem>
??????? <mx:HBox width="100%" textAlign="center">
??????????? <mx:Button? id="button1" label="Add" click="addRow()" />
??????????? <mx:Button? id="button2" label="Update" click="updateRow()"/>?
??????????? <mx:Button? id="button3" label="Delete" click="deleteRow()"/>
???????????????? </mx:HBox>
??? </mx:Form>
?
????
???
???
?
?<mx:Script>
??<![CDATA[
?
?function addRow() {
??????????? if (email.text != "" && phone.text !="" && zipcode.text != ""){?
??????????????????????employeeSrv.send();?
????????????????? dg.dataProvider.addItem( {email: email.text, phone: phone.text, zipcode: zipcode.text} );
?????????????????????????clearText();??????????????????
??????????? }???????????????????????????
??????????? else
?????????????? mx.controls.Alert.show( " can't be null" , 'Error');
??????? }
function updateRow() {
??????????? if (dg.selectedIndex!=undefined){
??????????? ?? employeeinfo_update.send();?
?????????????????????????????????
??????????? }
??????? }
function deleteRow() {
??????????? if (dg.selectedIndex!=undefined) {
??????????????? employeeinfo_delete.send();
??????????????? dg.dataProvider.removeItemAt(dg.selectedIndex);
??????????????? clearText();
?????????????????? }
??????????????? }
?
?function clearText()
? {
?????? email.text = "";
?????? phone.text = "";
?????? zipcode.text = "";
? }
?
??]]>
?</mx:Script>
?
?<mx:DataGrid x="321" y="10" id="dg"? dataProvider="{employee_query.lastResult.employeelist.employee}"? >
? <mx:columns>
?? <mx:DataGridColumn headerText="email" dataField="email"/>
?? <mx:DataGridColumn headerText="phone" dataField="phone"/>
?? <mx:DataGridColumn headerText="zipcode" dataField="zipcode"/>
? </mx:columns>
?</mx:DataGrid>
?
</mx:Application>
對應的jsp文件:
?employee.jsp
???????<%@ page contentType="text/xml; charset=utf-8" %>
??????<%@ page import="java.sql.*"%>?
??????<%@ page import="flexDemo.*" %>
??????<%@ page import="java.util.List" %>
????? <%
??
???????? EmployeeManager service = new EmployeeManager();
????Employee employee = new Employee(request.getParameter("phone"),request.getParameter("email"),request.getParameter("zipcode"));
?service.addEmployee(employee);
?
%>
employeedelete.jsp
<%@ page contentType="text/xml; charset=utf-8" %>
?
?
<%@ page import="java.sql.*"%>
<%@ page import="flexDemo.*" %>
<%@ page import="java.util.List" %>
<%
? //System.out.println("1111"+ request.getParameter("email"));
?EmployeeManager service = new EmployeeManager();
?Employee employee = new Employee(request.getParameter("phone"),request.getParameter("email"),request.getParameter("zipcode"));
?service.deleteEmployee(employee);
?
%>
employeelist.jsp
<%@ page contentType="text/xml; charset=utf-8" %>
?<%@ page import="java.sql.*"%>
<%@ page import="flexDemo.*" %>
<%@ page import="java.util.List" %>
<%
%>
<employeelist>
<%
???
?EmployeeManager service = new EmployeeManager();
?List listemployees = service.listEmployees();
?for (int i = 0; i < listemployees.size(); i++)
?{
??Employee employee = (Employee) listemployees.get(i);
%>
?<employee>
??<email><%= employee.getEmail() %></email>
??<phone><%= employee.getPhone() %></phone>
??<zipcode><%= employee.getZipcode() %></zipcode>
??
??? </employee>
<%
?}
%>
</employeelist>
employeeupdate.jsp
<%@ page contentType="text/xml; charset=utf-8" %>
?
?
<%@ page import="java.sql.*"%>
<%@ page import="flexDemo.*" %>
<%@ page import="java.util.List" %>
<%
? //? String id= request.getParameter("id");
?EmployeeManager service = new EmployeeManager();
?Employee employee = new Employee(request.getParameter("phone"),request.getParameter("email"),request.getParameter("zipcode"));
?service.updateEmployee(employee);
?
%>
java文件:
ConnectionManager.java
/**
?*
?*/
package flexDemo;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
?* @author wfl
?*
?*/
public class ConnectionManager {
?String sql = "";
?String url = "";
?Connection conn = null;
?Statement stmt = null;
?public String getUrl() {
??return url;
?}
?private static ConnectionManager instance;
?private ConnectionManager() {
??try {
???Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
???url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;User=sa;Password=pwd;";
???conn = DriverManager.getConnection(url);
???
??} catch (Exception e) {
???e.printStackTrace();
??}
?}
?public static ConnectionManager getInstance() {
??if (instance == null)
???instance = new ConnectionManager();
??return instance;
?}
?public static Connection getConnection() throws java.sql.SQLException {
??Connection connection = DriverManager.getConnection(getInstance()
????.getUrl());
??return connection;
?}
?public static void closeConnection(Connection c) {
??try {
???if (c != null) {
????c.close();
???}
??} catch (SQLException e) {
???e.printStackTrace();
??}
?}
}
Employee.java
/**
?*
?*/
package flexDemo;
/**
?* @author wfl
?*
?*/
public class Employee {
?private int id;
?private String name;
?private String phone;
?private String email;
?private String zipcode;
?public Employee(int id, String name, String email , String phone,
???String zipcode) {
??this.id = id;
??this.name = name;??
??this.email = email;
??this.phone = phone;
??this.zipcode = zipcode;
?}
?public Employee( String phone, String email,
???String zipcode) {??
??this.phone = phone;
??this.email = email;
??this.zipcode = zipcode;
?}
?public Employee() {
?}
?public String getEmail() {
??return email;
?}
?public int getId() {
??return id;
?}
?public void setId(int id) {
??this.id = id;
?}
?public void setEmail(String email) {
??this.email = email;
?}
?public String getName() {
??return name;
?}
?public void setName(String name) {
??this.name = name;
?}
?public String getPhone() {
??return phone;
?}
?public void setPhone(String phone) {
??this.phone = phone;
?}
?public String getZipcode() {
??return zipcode;
?}
?public void setZipcode(String zipcode) {
??this.zipcode = zipcode;
?}
}
EmployeeManager.java
/**
?*
?*/
package flexDemo;
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 java.util.List;
/**
?* @author wfl
?*
?*/
public class EmployeeManager {
?/**
? *
? * @return
? */
?public EmployeeManager(){
??
?}
?public List listEmployees() {
??ArrayList list = new ArrayList();
??Connection conn = null;
??try {
???conn = ConnectionManager.getConnection();
???Statement stmt = conn.createStatement();
???String sql = "select * from test";
???ResultSet rs = stmt.executeQuery(sql);
???while (rs.next()) {
????Employee employee = new Employee(rs.getInt("id"), rs
??????.getString("name"), rs.getString("email"), rs
??????.getString("phone"), rs.getString("zipcode")
????);
????list.add(employee);
???}
??} catch (SQLException e) {
???e.printStackTrace();
??} finally {
???ConnectionManager.closeConnection(conn);
??}
??return list;
?}
?/***************************************************************************
? *
? *
? *
? *
? *
? * @param employee
? */
?public void addEmployee(Employee employee) {
??Connection conn = null;
??try {
???conn = ConnectionManager.getConnection();
???String sql = "insert into test(name,email,phone,zipcode) values(?,?,?,?)";
???PreparedStatement pstmt = conn.prepareStatement(sql);
???pstmt.setString(1, employee.getName());
???pstmt.setString(2, employee.getEmail());
???pstmt.setString(3, employee.getPhone());
???pstmt.setString(4, employee.getZipcode());
???pstmt.execute();
??} catch (SQLException e) {
???e.printStackTrace();
??} finally {
???ConnectionManager.closeConnection(conn);
??}
?}
?
/**
?*
?*
?*/?
?public void updateEmployee(Employee employee) {
??Connection conn = null;
??try {
???conn = ConnectionManager.getConnection();
???String sql = "update test set name=? ,email=?,phone=?,zipcode=? where email='"+employee.getEmail()+"'";
???PreparedStatement pstmt = conn.prepareStatement(sql);
???pstmt.setString(1, employee.getName());
???pstmt.setString(2, employee.getEmail());
???pstmt.setString(3, employee.getPhone());
???pstmt.setString(4, employee.getZipcode());
???
???pstmt.execute();
??} catch (SQLException e) {
???e.printStackTrace();
??} finally {
???ConnectionManager.closeConnection(conn);
??}
?}
?
?
?/**
? *
? */
?public void deleteEmployee(Employee employee) {
??Connection conn = null;
??try {
???conn = ConnectionManager.getConnection();
???String sql = "delete test where email='"+employee.getEmail()+"'";
???Statement stmt = conn.createStatement();
???stmt.executeUpdate(sql);
???
????????????
??} catch (SQLException e) {
???e.printStackTrace();
??} finally {
???ConnectionManager.closeConnection(conn);
??}
??
?}
?
}