首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Flex与Java调整连接数据库的实例

2012-11-23 
Flex与Java整合连接数据库的实例Flex与Java整合连接数据库的实例 效果图为: 开发工具:eclipse+flex 数 据

Flex与Java整合连接数据库的实例

Flex与Java整合连接数据库的实例
效果图为:
Flex与Java调整连接数据库的实例

开发工具:eclipse+flex
数 据 库:SQL SERVER 2005
首先建立数据库:
数据库:companyDB
数据表:company
USE [companyDB]
GO
/****** 对象:? Table [dbo].[company]??? 脚本日期: 04/15/2010 14:07:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[company](
[id] [int] IDENTITY(1,1) NOT NULL,
[employ_name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[employ_sex] [char](2) COLLATE Chinese_PRC_CI_AS NULL,
[employ_address] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[employ_phone] [varchar](12) COLLATE Chinese_PRC_CI_AS NULL,
[employ_position] [varchar](30) COLLATE Chinese_PRC_CI_AS NULL,
CONSTRAINT [PK_company] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

工程结构如图

?

Flex与Java调整连接数据库的实例

Company.java代码如下:

package com.zhn.model;

public class Company {

private int id;
private String employName;
private String employSex;
private String employPhone;
private String employAddress;
private String employPosition;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmployName() {
return employName;
}
public void setEmployName(String employName) {
this.employName = employName;
}
public String getEmploySex() {
return employSex;
}
public void setEmploySex(String employSex) {
this.employSex = employSex;
}
public String getEmployPhone() {
return employPhone;
}
public void setEmployPhone(String employPhone) {
this.employPhone = employPhone;
}
public String getEmployAddress() {
return employAddress;
}
public void setEmployAddress(String employAddress) {
this.employAddress = employAddress;
}
public String getEmployPosition() {
return employPosition;
}
public void setEmployPosition(String employPosition) {
this.employPosition = employPosition;
}
}

ConnectionDB.java为连接数据库及对数据库进行操作的公共方法的类,代码如下:
package com.zhn.db;

import java.sql.*;
import java.util.*;

import com.zhn.model.Company;
public class ConnectionDB {

private static Connection conn=null;
private static Statement stmt = null;
private static ResultSet rs = null;
/*
* 数据库链接声明
* 2000:com.microsoft.jdbc.sqlserver.SQLServerDriver
* 2000:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sample,username,password
* 2005:com.microsoft.sqlserver.jdbc.SQLServerDriver
* 2005:jdbc:sqlserver://localhost:1433;DatabaseName=sample,username,password
*/
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String CON_STR = "jdbc:sqlserver://";
private static final String HOST_NAME = "localhost:1433;";
private static final String DATABASE = "DatabaseName=companyDB";
private static final String USERNAME="sa";
private static final String PASSWORD="zhanghenan";

private static List all = null;
/*
* 链接数据库
*/
public static Connection getCon(){
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(CON_STR+HOST_NAME+DATABASE,USERNAME,PASSWORD);
} catch (Exception e) {
System.out.println("数据库链接失败:"+e.getMessage());
}
return conn;
}
/*
* 关闭连接
*/
public static void closeCon(Connection conn){
try{
if(conn!=null){
conn.close();
}
}catch(Exception e){
System.out.println("数据库关闭失败:"+e.getMessage());
}
}
/*
* 关闭Statement
*/
public static void closeStatement(Statement stmt){
try{
if(stmt!=null){
stmt.close();
}
}catch(Exception e){
System.out.println("Statement关闭失败:"+e.getMessage());
}
}
/*
* 关闭ResultSet
*/
public static void closeRS(ResultSet rss){
try{
if(rss!=null){
rss.close();
}
}catch(Exception e){
System.out.println("ResultSet关闭失败:"+e.getMessage());
}
}
/*
* 获取数据列表
*/
public static List getList(String sql){
conn = ConnectionDB.getCon();
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs!=null){
all = new ArrayList();
}
while(rs.next()){
Company com = new Company();
com.setId(rs.getInt("id"));
com.setEmployAddress(rs.getString("employ_address"));
com.setEmployName(rs.getString("employ_name"));
com.setEmployPhone(rs.getString("employ_phone"));
com.setEmployPosition(rs.getString("employ_position"));
com.setEmploySex(rs.getString("employ_sex"));
all.add(com);
}
ConnectionDB.closeStatement(stmt);
ConnectionDB.closeRS(rs);
ConnectionDB.closeCon(conn);
return all;
} catch (SQLException e) {
System.out.println("查询list失败:"+e.getMessage());
return null;
}
}
}

CompanyAction.java代码如下:
package com.zhn.action;

import java.util.*;

import com.zhn.model.*;
import com.zhn.db.*;

public class CompanyAction {

private List all = new ArrayList();
public Company[] getCompanyList(){
all = ConnectionDB.getList("select * from company");
Company[] comArr = new Company[all.size()];
for(int i=0;i<all.size();i++){
comArr[i] = (Company)all.get(i);
}
return comArr;
}
}

以上这些类熟悉java的人都应该明白。接下来是对Flex进行一些配置,是查询出的数据显示在Flex页面中:

首先是配置remoting-config.xml文件,在此文件中加上如下代码,将操作数据库的类进行配置保证flex与java之间的通信:
<destination id="companyAction">
<properties>
<source>com.zhn.action.CompanyAction</source>
</properties>
</destination>
将此配置好,我们就可以写页面显示部分了,创建FlexConJavaDB.mxml应用:
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="showMsg()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;

var ac:ArrayCollection = new ArrayCollection();
function showMsg():void{
companyAction.getCompanyList();
companyAction.addEventListener(ResultEvent.RESULT,getList);
}
[Bindable]
function getList(e:ResultEvent):void{
dg.dataProvider = e.result as Array;
}
]]>
</mx:Script>

<mx:RemoteObject destination="companyAction" id="companyAction"/>
<mx:DataGrid horizontalCenter="0" verticalCenter="13" id="dg">
<mx:columns>
<mx:DataGridColumn headerText="员工编号" fontSize="14" fontWeight="bold" dataField="id"/>
<mx:DataGridColumn headerText="员工姓名" fontSize="14" fontWeight="bold" dataField="employName"/>
<mx:DataGridColumn headerText="员工性别" fontSize="14" fontWeight="bold" dataField="employSex"/>
<mx:DataGridColumn headerText="员工电话" fontSize="14" fontWeight="bold" dataField="employPhone"/>
<mx:DataGridColumn headerText="员工住址" fontSize="14" fontWeight="bold" dataField="employAddress"/>
<mx:DataGridColumn headerText="员工职位" fontSize="14" fontWeight="bold" dataField="employPosition"/>
</mx:columns>
</mx:DataGrid>
<mx:Form horizontalCenter="-92" verticalCenter="244" width="423" height="230">
<mx:FormItem label="员工编号:">
<mx:Label text="{dg.selectedItem.id}"/>
</mx:FormItem>
<mx:FormItem label="员工姓名:">
<mx:Label text="{dg.selectedItem.employName}"/>
</mx:FormItem>
<mx:FormItem label="员工性别:">
<mx:Label text="{dg.selectedItem.employSex}"/>
</mx:FormItem>
<mx:FormItem label="员工电话:">
<mx:Label text="{dg.selectedItem.employPhone}"/>
</mx:FormItem>
<mx:FormItem label="员工地址:">
<mx:Label text="{dg.selectedItem.employAddress}"/>
</mx:FormItem>
<mx:FormItem label="员工职位:">
<mx:Label text="{dg.selectedItem.employPosition}"/>
</mx:FormItem>
</mx:Form>
</mx:Application>


笼统解释:
<mx:RemoteObject destination="companyAction" id="companyAction"/>
在页面获取remoting-config.xml中的配置,也是保证java与flex之间通信的重要步骤之一。

<mx:Script> 定义两个方法,一个是获取从java返回的集合,一个是定义的绑定事件。
在此,是将返回的数组直接绑定到DataGrid中做显示。
注意:在显示的时候,DataGrid中的属性dataField的值一定要与javaBean中的变量保持一致。

<mx:Form>是在点击DataGrid中某一条记录的时候显示用的。


?

热点排行