飞龙在天

contact: QQ: 706627987

 

flex结合j2ee操作mssql数据库源码

声明:本人刚刚接触flex两天,希望不要给初学者一个错误的引导。

本例子的bug :在update时不能及时刷新数据。不清楚用DataGrid的啥方法了。谁知道请告诉我。谢谢!  QQ:706627987  MSN:wangfeilong_cn@hotmail.com


运行及开发环境:
flexbuilder2+eclipse3.1.1+tocmcat5+sqlserver2k

数据库信息(数据库名,用户名,密码)在下面的ConnectionManager.java中
这里面  数据库名字为test ,表名字也为test ,用户名为sa  密码为pwd

  表结构:

3.JPG



Eclipse 的工程结构:

 2.JPG

 

FlexBuilder2 的工程结构

 1.JPG

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内容: employee.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="employee_query.send()"  >
 
 <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);
  }
  
 }
 
}


posted on 2006-09-28 15:40 feilongone 阅读(2879) 评论(3)  编辑  收藏

评论

# re: flex结合j2ee操作mssql数据库源码 2006-09-28 20:24 CoderDream

请将图片上传到网络上,现在图片还在你的C盘里面!  回复  更多评论   

# re: flex结合j2ee操作mssql数据库源码 2007-04-07 10:45 lubaolin

TypeError: Error #1009: 无法访问空对象引用的属性或方法。
at employee/::addRow()
at employee/__button1_click()


我运行添加后怎么会报这个错误!  回复  更多评论   

# re: flex结合j2ee操作mssql数据库源码 2007-06-15 12:31 fallingstone

加上dg.validateNow();可以刷新dataGrid
eg.
function query():void {
employee_query.send();
dg.validateNow();
}   回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(2)

随笔档案

文章档案

相册

搜索

最新评论

阅读排行榜

评论排行榜