日期:2014-05-16  浏览次数:20451 次

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

工程结构如图

?



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(