最近学习《软件工程导论》(清华大学出版)
再需求分析中有一个关于住房管理系统的实现。
于是用java写了一个原型系统。(很简陋)
主要是为了学习jdbc的一些特性。
这把部分程序列出,和大家分享。
问题可参看上面所说的书。
由于时间有限,说明不太准确。今后我会不断修改的。
/*
住户房屋申请表
*/
public class ApplicationInfo {
private String name = null;
private String age = null;
private String length_service = null;
private String head_name = null;
private String post = null;
private String population = null ;
private String grade = null;
private String apply_type = null;
public ApplicationInfo(){
}
/*
用数组保存用户的申请信息。
*/
public void setAllAttribute(String result[]){
name = result[0];
age = result[1];
length_service = result[2];
head_name = result[3];
post = result[4];
population = result[5];
grade = result[6];
apply_type = result[7];
}
/*
获得用户的各种申请信息,
包括姓名,年龄,工龄,职位,职称,家庭人口等
信息。
*/
public String getApplyType(){
return apply_type;
}
public String getName(){
return name;
}
public String getAge(){
return age;
}
public String getLengthService(){
return length_service;
}
public String getPost(){
return post;
}
public String getHeadName(){
return head_name;
}
public String getPopulation(){
return population;
}
public String getGrade(){
return grade;
}
}
////////////////////////////////////////////////////////
/*
用来显示空房,
住房,
总共的房屋数的统计信息。
作的很粗糙。
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class BarChartInfo extends JPanel {
private static final int SCALE = 2;
private static final int CHARTCOUNT = 3;
private static final int BARSPACING = 20;
private static final String CHARTTITEL = "HouseNumberGraphics";
private static final Font CURRENTFONT= new Font("Courier",Font.BOLD,12);
private static FontMetrics cfm ;
private static int MAXLABELWIDTH = 0;
private static int BARWIDTH = 0;
private static int MAX = 0;
private int values[];
private Color colors[];
private String labels[];
public BarChartInfo (int valueInfo[]){
cfm = getFontMetrics(CURRENTFONT);
values = new int[CHARTCOUNT];
colors = new Color[CHARTCOUNT];
labels = new String[CHARTCOUNT];
colors[0] = Color.red;
colors[1] = Color.green;
colors[2] = Color.blue;
for (int i=0;i<CHARTCOUNT; i++){
values[i] = valueInfo[i];
if (values[i]> MAX) {
MAX = values[i];
}
labels[0] = "RemainHouseNumber:";
labels[1] = "DistributeHouseNumber:";
labels[2] = "AllHouseNumber:";
MAXLABELWIDTH = Math.max(cfm.stringWidth(labels[i]),
MAXLABELWIDTH);
}
BARWIDTH = CURRENTFONT.getSize();
setSize(Math.max((MAX*SCALE),
cfm.stringWidth(CHARTTITEL))+MAXLABELWIDTH+5,
(CHARTCOUNT*(BARWIDTH+BARSPACING))+CURRENTFONT.getSize()+10);
}
public void update(Graphics g){
g.clearRect(0,0,getWidth(),getHeight());
paintComponent(g);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2D = (Graphics2D)g;
g2D.setFont(CURRENTFONT);
g2D.setColor(Color.black);
int i,cx,cy;
i = cfm.stringWidth(CHARTTITEL);
for (i = 0;i < CHARTCOUNT ; i++) {
cy = ((BARWIDTH + BARSPACING) * i) + BARSPACING;
cx = MAXLABELWIDTH + 1;
cx += Math.max((getWidth()-(MAXLABELWIDTH + 1 +(MAX*SCALE)))/2,0);
g2D.setColor(Color.black);
g2D.drawString(labels[i],cx -MAXLABELWIDTH-1,
cy + cfm.getAscent());
g2D.fillRect(cx+3,cy+5,(values[i]*SCALE),BARWIDTH);
g2D.setColor(colors[i]);
g2D.fillRect(cx,cy,(values[i]*SCALE),BARWIDTH);
g2D.drawString(""+values[i],cx+(values[i]*SCALE)+3,
cy + cfm.getAscent());
}
}
}
///////////////////////////////////////////
/*
把用户的申请信息,写入分房文件中,
一个月后由系统读出信息,然后进行分房。
*/
import java.io.*;
public class CreateApplyQueueFile extends Thread {
private boolean isOver;
private StringBuffer buffer;
public CreateApplyQueueFile(StringBuffer buffer){
isOver = false;
this.buffer = buffer;
}
public void run(){
try {
PrintWriter out= new PrintWriter(
new BufferedWriter(
new FileWriter("house.tmp",true)),true);
out.println(buffer);
out.close();
}
catch(Exception e){e.printStackTrace();}
isOver = true;
}
public boolean isOver(){
return isOver;
}
}
/////////////////////////////////////
/*
进行分房。
*/
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
public class DistributeHouse extends Thread {
private Vector houseInfoVector ;
private Vector applyVector ;
private Connection connection;
private StringBuffer resultBuffer;
private JTextArea textArea;
public DistributeHouse(Connection connection,JTextArea textArea){
resultBuffer = new StringBuffer();
houseInfoVector = new Vector();
applyVector = new Vector();
this.connection = connection;
this.textArea = textArea;
}
public void run(){
LoadApplyQueueFile();
distributeHouse();
}
/*
从分房文件中读出申请者的信息,并加入到向量中,备分房时使用。
*/
private synchronized void LoadApplyQueueFile(){
try { BufferedReader reader = new BufferedReader(
new FileReader("house.tmp"));
String s = null;
while((s = reader.readLine())!=null){
ApplicationInfo applyInfo = new ApplicationInfo();
StringTokenizer stk = new StringTokenizer(s);
String result[] = new String[8];
int i=0;
while(stk.hasMoreElements()){
result[i] = stk.nextToken("*");
i++;
}
applyInfo.setAllAttribute(result);
applyVector.addElement(applyInfo);
}
reader.close();
}
catch(Exception e){e.printStackTrace();}
}
/*
分房完毕后更新分房文件为空文件。
备下批分房使用。
*/
private synchronized void DeleteApplyQueueFile(){
try{
FileOutputStream fos = new FileOutputStream("house.tmp");
fos.close();
}
catch(Exception e){e.printStackTrace();}
}
/*
从向量中随机读出申请者等级,连接数据库,进行相关的查询及更新。
*/
private synchronized void distributeHouse(){
VacantHouseInfo info = null;
while(!applyVector.isEmpty()){
ApplicationInfo applyInfo = (ApplicationInfo)applyVector.elementAt(0);
String grade = getGrade(applyInfo);
int accordHouseNum = getVacantHouseInfo(grade);
int randomIndex = (int)(accordHouseNum*Math.random());
info = (VacantHouseInfo)houseInfoVector.elementAt(randomIndex);
updateRentFile(info,applyInfo);
updateLodgingHouseFile(info,applyInfo);
updateVacantHouseFile(info);
displayResult(info,applyInfo);
applyVector.remove(0);
houseInfoVector.removeAllElements();
}
DeleteApplyQueueFile();
}
/*
获得空房等级。空房等级假设和申请者的年龄,工龄,职务等
有关。这里没有给出函数关系,可以在程序中设置为3,或5等。
*/
private synchronized int getVacantHouseInfo(String houseGrade){
int value = -1;
try{
PreparedStatement Stmt = connection.prepareStatement(
"SELECT * FROM 空房文件 WHERE 房屋等级 = '"+
houseGrade + "'");
ResultSet result = Stmt.executeQuery();
ResultSetMetaData metadata = result.getMetaData();
String houseInfo[] = new String[5];
while (result.next()) {
for(int i=1;i<=metadata.getColumnCount();i++){
houseInfo[i-1] = result.getString(i);
}
VacantHouseInfo vacantHouseInfo = new VacantHouseInfo();
vacantHouseInfo.setAllAttribute(houseInfo);
houseInfoVector.addElement(vacantHouseInfo);
value = houseInfoVector.size();
}
Stmt.close();
}
catch(Exception e){e.printStackTrace();}
return value;
}
/*
把分得房屋的用户写入住房文件中。
*/
private synchronized void updateLodgingHouseFile(VacantHouseInfo info,ApplicationInfo applyInfo){
try { PreparedStatement Stmt = connection.prepareStatement("INSERT INTO 住房文件 VALUES(?,?,?,?,?,?)");
Stmt.setString(1,info.getHouseNumber());
Stmt.setString(2,applyInfo.getName());
Stmt.setString(3,info.getHouseGrade());
Stmt.setString(4,info.getHouseRent());
Stmt.setString(5,info.getHouseArea());
Stmt.setString(6,info.getHouseStructure());
Stmt.executeUpdate();
Stmt.close();
}
catch (Exception e) {e.printStackTrace();}
}
/*
算出房租写入房租文件中。
*/
private synchronized void updateRentFile(VacantHouseInfo info,ApplicationInfo applyInfo){
try { String s = caculateRent(info);
PreparedStatement Stmt = connection.prepareStatement("INSERT INTO 房租文件 VALUES(?,?,?,?)");
Stmt.setString(1,info.getHouseNumber());
Stmt.setString(2,applyInfo.getName());
Stmt.setString(3,info.getHouseGrade());
Stmt.setString(4,s);
Stmt.executeUpdate();
Stmt.close();
}
catch (Exception e) {e.printStackTrace();}
}
/*
删除空房文件中以分配了的房屋信息。
*/
private synchronized void updateVacantHouseFile(VacantHouseInfo info){
try { Statement Stmt = connection.createStatement();
Stmt.execute("DELETE FROM 空房文件 WHERE 房号 = "+"'"+
info.getHouseNumber()+"'");
Stmt.execute("DELETE FROM 空房文件 WHERE 房号 = "+"'"+
info.getHouseNumber()+"'");
Stmt.close();
}
catch (Exception e) {e.printStackTrace();}
}
/*
*/
private synchronized int getInteger(String s){
StringBuffer buffer = new StringBuffer();
for(int i=0;i<s.length();i++){
if((int)s.charAt(i)>=48&&(int)s.charAt(i)<=57){
buffer.append(s.charAt(i));
}
}
return Integer.parseInt(buffer.toString());
}
/*
计算房租。
*/
private synchronized String caculateRent(VacantHouseInfo info){
int area = getInteger(info.getHouseArea());
int rent = getInteger(info.getHouseRent());
int sum = area*rent;
Integer integer = null;
if(sum>0)integer =new Integer(sum);
String s = integer.toString()+"元";
return s;
}
/*
获得房屋等级。
*/
private synchronized String getGrade(ApplicationInfo applyInfo){
return applyInfo.getGrade();
}
/*
显示结果。
*/
private synchronized void displayResult(VacantHouseInfo info ,ApplicationInfo applyInfo){
String rent = caculateRent(info);
String number = info.getHouseNumber();
String name = applyInfo.getName();
String grade = info.getHouseGrade();
String perRent = info.getHouseRent();
String area = info.getHouseArea();
String structure = info.getHouseStructure();
resultBuffer.append("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n");
resultBuffer.append("姓名 :"+name+"\n");
resultBuffer.append("分配房屋号码 :"+number+"\n");
resultBuffer.append("分配房屋等级 :"+grade+"\n");
resultBuffer.append("分配房屋面积(平方米) :"+area+"\n");
resultBuffer.append("分配房屋的结构 :"+structure+"\n");
resultBuffer.append("分配房屋房租(每平方米/元) :"+perRent+"\n");
resultBuffer.append("本月应缴房租(元) :"+rent+"\n");
resultBuffer.append("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n");
textArea.append(resultBuffer.toString());
resultBuffer.setLength(0);
}
}
///////////////////////////////////
/*
管理员对房屋的管理。
*/
import java.sql.*;
public class MasterHouseInfo {
private Connection connection;
private int valueInfo[] = new int[3];
public MasterHouseInfo(Connection connection){
this.connection = connection;
}
public int[] execute(){
valueInfo[0] = getVacantResultCount();
valueInfo[1] = getHouseResultCount();
valueInfo[2] = valueInfo[0]+valueInfo[1];
return valueInfo;
}
private synchronized int getVacantResultCount(){
int Count = 0 ;
try{
PreparedStatement Stmt = connection.prepareStatement("SELECT COUNT(*) FROM 空房文件");
ResultSet result = Stmt.executeQuery();
ResultSetMetaData metadata = result.getMetaData();
while (result.next()) {
Count = result.getInt(1);
}
Stmt.close();
}
catch(Exception e){e.printStackTrace();}
return Count ;
}
private synchronized int getHouseResultCount(){
int Count = 0;
try{
PreparedStatement Stmt = connection.prepareStatement("SELECT COUNT(*) FROM 住房文件");
ResultSet result = Stmt.executeQuery();
ResultSetMetaData metadata = result.getMetaData();
while (result.next()) {
Count = result.getInt(1);
}
Stmt.close();
}
catch(Exception e){e.printStackTrace();}
return Count ;
}
}
///////////////////////////////////////
/*
住户申请调房,先退房,再分房的原则。
这里没有给出最好的实现。
*/
import java.sql.*;
import javax.swing.*;
public class PrepareHouse extends Thread {
private Connection connection;
private String houseNumber = null;
private JTextArea textArea;
public PrepareHouse(Connection connection,String houseNumber,
JTextArea textArea){
this.connection = connection;
this.houseNumber = houseNumber;
this.textArea = textArea;
}
public void run(){
prepareHouse();
}
public void prepareHouse(){
new DistributeHouse(connection,textArea).start();
new QuiteHouse(connection,houseNumber,textArea).start();
textArea.append("**********调房成功!************");
}
}
/////////////////////////////////
/*
查询房屋信息。
*/
import java.sql.*;
import javax.swing.*;
public class QueryHouseInfo extends Thread {
private Connection connection;
private StringBuffer resultBuffer;
private String query ;
private JTextArea textArea;
private String info[];
public QueryHouseInfo(Connection connection,String query,
String info[],JTextArea textArea){
this.query = query;
resultBuffer = new StringBuffer();
this.connection = connection;
this.textArea = textArea;
this.info = info ;
}
public void run(){
if(info == null)
queryHouseInfo(query);
else
queryHouseInfo();
displayResult(resultBuffer);
}
/*
查询房屋所有的信息。
*/
private synchronized void queryHouseInfo(String tableName){
try{
PreparedStatement Stmt = connection.prepareStatement("SELECT * FROM "+tableName);
ResultSet result = Stmt.executeQuery();
ResultSetMetaData metadata = result.getMetaData();
while (result.next()) {
for(int i=1;i<=metadata.getColumnCount();i++){
String label = metadata.getColumnLabel(i);
String info = result.getString(i);
resultBuffer.append(label+":"+info+"\n");
}
resultBuffer.append("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n");
}
Stmt.close();
}
catch(Exception e){e.printStackTrace();}
}
/*
以房屋号查询相关信息。
*/
private synchronized void queryHouseInfo(){
try{
PreparedStatement Stmt = connection.prepareStatement(
"SELECT "+query+" FROM "+info[2]+" WHERE "+
info[0]+"='"+
info[1]+"'");
ResultSet result = Stmt.executeQuery();
ResultSetMetaData metadata = result.getMetaData();
while (result.next()) {
for(int i=1;i<=metadata.getColumnCount();i++){
String label = metadata.getColumnLabel(i);
String info = result.getString(i);
resultBuffer.append(label+":"+info+"\n");
}
resultBuffer.append("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n");
}
Stmt.close();
}
catch(Exception e){e.printStackTrace();}
}
private synchronized String getGrade(ApplicationInfo applyInfo){
return applyInfo.getGrade();
}
private synchronized void displayResult(StringBuffer tmp){
StringBuffer buffer = new StringBuffer();
buffer.append(tmp.toString());
textArea.append(buffer.toString());
resultBuffer.setLength(0);
}
}
///////////////////////////////////////
/*
用户申请退房。
*/
import java.sql.*;
import java.util.*;
import javax.swing.*;
public class QuiteHouse extends Thread {
private Connection connection;
private String houseNumber = null;
private StringBuffer resultBuffer;
private JTextArea textArea;
private String name = null;
public QuiteHouse(Connection connection,String houseNumber,
JTextArea textArea){
this.connection = connection;
this.houseNumber = houseNumber;
resultBuffer = new StringBuffer();
this.textArea = textArea;;
}
public void run(){
VacantHouseInfo info = getQuiteHouseInfo();
updateVacantHouseFile(info);
updateLodgingHouseFile(info);
updateRentFile(info);
displayResult(info);
}
/*
获得所退房屋得相关信息。
*/
public synchronized VacantHouseInfo getQuiteHouseInfo(){
VacantHouseInfo vacantHouseInfo = null;
try{
Statement Stmt = connection.createStatement();
ResultSet result = Stmt.executeQuery("SELECT * FROM 住房文件 WHERE 房号 = '"+
houseNumber+"'");
ResultSetMetaData metadata = result.getMetaData();
String houseInfo[] = new String[5];
while (result.next()) {
for(int i=3;i<= metadata.getColumnCount();i++){
houseInfo[i-2] = result.getString(i);
}
name = result.getString(2);
}
houseInfo[0] = houseNumber;
vacantHouseInfo = new VacantHouseInfo();
vacantHouseInfo.setAllAttribute(houseInfo);
Stmt.close();
}
catch(Exception e){e.printStackTrace();}
return vacantHouseInfo ;
}
/*
从房租文件中删除要退房屋。
*/
private synchronized void updateRentFile(VacantHouseInfo info){
try { Statement Stmt = connection.createStatement();
Stmt.execute("DELETE FROM 房租文件 WHERE 房号 = "+"'"+
info.getHouseNumber()+"'");
Stmt.execute("DELETE FROM 房租文件 WHERE 房号 = "+"'"+
info.getHouseNumber()+"'");
Stmt.close();
}
catch (Exception e) {e.printStackTrace();}
}
/*
把以退房屋重新写入空房文件中。
*/
private synchronized void updateVacantHouseFile(VacantHouseInfo info){
try { PreparedStatement Stmt = connection.prepareStatement("INSERT INTO 空房文件 VALUES(?,?,?,?,?)");
Stmt.setString(1,info.getHouseNumber());
Stmt.setString(2,info.getHouseGrade());
Stmt.setString(3,info.getHouseRent());
Stmt.setString(4,info.getHouseArea());
Stmt.setString(5,info.getHouseStructure());
Stmt.executeUpdate();
Stmt.close();
}
catch (Exception e) {e.printStackTrace();}
}
/*
从住房文件中删除退房信息。
*/
private synchronized void updateLodgingHouseFile(final VacantHouseInfo info){
try{ Statement Stmt = connection.createStatement();
Stmt.execute("DELETE FROM 住房文件 WHERE 房号 = "+"'"+
info.getHouseNumber()+"'");
Stmt.execute("DELETE FROM 住房文件 WHERE 房号 = "+"'"+
info.getHouseNumber()+"'");
Stmt.close();
}
catch (Exception e) {e.printStackTrace();}
}
/*
显示退房结果。
*/
private synchronized void displayResult(VacantHouseInfo info){
String number = info.getHouseNumber();
String grade = info.getHouseGrade();
String perRent = info.getHouseRent();
String area = info.getHouseArea();
String structure = info.getHouseStructure();
resultBuffer.append("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n");
resultBuffer.append("退屋者姓名:"+name+"\n");
resultBuffer.append("所退房屋号码:"+number+"\n");
resultBuffer.append("所退房屋等级:"+grade+"\n");
resultBuffer.append("所退房屋面积(平方米):"+area+"\n");
resultBuffer.append("所退房屋结构:"+structure+"\n");
resultBuffer.append("所退房屋单位面积房租(每平方米/元):"+perRent+"\n");
resultBuffer.append(name+"退屋成功!"+"\n");
resultBuffer.append("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n");
textArea.append(resultBuffer.toString());
resultBuffer.setLength(0);
}
}
/////////////////////////////////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.*;
public class UpdateHouseInfoPanel extends JFrame {
private JButton referButton = new JButton("提及更新信息");
private JButton reEditButton = new JButton("重填更新信息");
private JButton updateButton = new JButton("执行更新");
private JCheckBox grade = new JCheckBox("房屋等级");
private JCheckBox area = new JCheckBox("房屋面积");
private JCheckBox rent = new JCheckBox("单位面积房租");
private JCheckBox structure = new JCheckBox("房屋结构");
private JTextField textfiled = new JTextField();
private JTextField gradeinfo = new JTextField();
private JTextField areainfo = new JTextField();
private JTextField rentinfo = new JTextField();
private JTextField structureinfo = new JTextField();
private JTextArea textArea ;
private JComboBox comBox = new JComboBox();
private String itemText ;
private StringBuffer buffer;
private Connection connection;
public UpdateHouseInfoPanel(Connection connection){
super("管理员更改房屋信息演示");
buffer = new StringBuffer();
this.connection = connection;
getContentPane().setLayout(new BorderLayout());
getContentPane().add(createPanel(),BorderLayout.CENTER);
setSize(500,520);
center(this);
pack();
setResizable(false);
setVisible(true);
}
public void center(Component C) {
Dimension SS = C.getToolkit().getScreenSize();
C.setLocation ((SS.width - 750) / 2,(SS.height - 500) / 2);
}
private JPanel createPanel(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(2,0,5,5));
JPanel p = new JPanel(new DialogLayout2(100,5));
JPanel rl = new JPanel(new GridLayout(0,2,5,5));
JPanel pr = new JPanel();
pr.setLayout(new DialogLayout2(100,5));
pr.add(new DialogSeparator("用户查询信息表"));
JPanel panel = new JPanel(new GridLayout(0,4));
comBox.addItem("房号");
comBox.addItem("房屋等级");
panel.add(new JLabel("选择更新条件:"));
panel.add(comBox);
panel.add(new JLabel("房号/等级:"));
panel.add(textfiled);
pr.add(panel);
pr.add(new JLabel("更改房屋等级为:"));
pr.add(gradeinfo);
pr.add(new JLabel("更改房屋面级为:"));
pr.add(areainfo);
pr.add(new JLabel("更改单位房租为:"));
pr.add(rentinfo);
pr.add(new JLabel("更改房屋结构为:"));
pr.add(structureinfo);
JPanel pl = new JPanel();
pl.setBorder(new TitledBorder(new EtchedBorder(),"选择需要查询的信息"));
JPanel checkPanel = new JPanel();
checkPanel.add(grade);
checkPanel.add(area);
checkPanel.add(rent);
checkPanel.add(structure);
pl.add(checkPanel);
rl.add(pr);
rl.add(pl);
p.add(rl);
p.add(new DialogSeparator("操作"));
p.add(referButton);
p.add(reEditButton);
p.add(updateButton);
textArea = new JTextArea();
JScrollPane sp = new JScrollPane( textArea );
mainPanel.add(p);
mainPanel.add(sp);
gradeinfo.setEditable(false);
areainfo.setEditable(false);
rentinfo.setEditable(false);
structureinfo.setEditable(false);
ButtonListener listener = new ButtonListener() ;
referButton.addActionListener(listener);
reEditButton.addActionListener(listener);
updateButton.addActionListener(listener);
CheckBoxListener checklistener = new CheckBoxListener();
grade.addActionListener(checklistener);
area.addActionListener(checklistener);
rent.addActionListener(checklistener);
structure.addActionListener(checklistener);
comBox.addActionListener(new ComboBoxListener());
return mainPanel;
}
private void getUpdateInfo(){
if(grade.isSelected()){
buffer.append("房屋等级=");
buffer.append("'"+gradeinfo.getText()+"'"+",");
}
if(area.isSelected()){
buffer.append("房屋面积=");
buffer.append("'"+areainfo.getText()+"'"+",");
}
if(rent.isSelected()){
buffer.append("单位面积房租=");
buffer.append("'"+rentinfo.getText()+"'"+",");
}
if(structure.isSelected()){
buffer.append("房屋结构=");
buffer.append("'"+structureinfo.getText()+"'"+",");
}
}
private void reEditApplyType(){
gradeinfo.setText(null);
areainfo.setText(null);
rentinfo.setText(null);
structureinfo.setText(null);
textfiled.setText(null);
itemText = null;
gradeinfo.setEditable(false);
areainfo.setEditable(false);
rentinfo.setEditable(false);
structureinfo.setEditable(false);
grade.setSelected(false);
area.setSelected(false);
rent.setSelected(false);
structure.setSelected(false);
buffer.setLength(0);
}
class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
if (obj == updateButton) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
buffer.deleteCharAt(buffer.length()-1) ;
new UpdateHouseInfo(buffer.toString()).start();
}
});
}
else if (obj == referButton) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
getUpdateInfo();
}
});
}
else if (obj == reEditButton){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
reEditApplyType();
}
});
}
}
}
class CheckBoxListener implements ActionListener {
public void actionPerformed (ActionEvent ae) {
Object obj = ae.getSource();
if (obj == grade) {
if(grade.isSelected())
gradeinfo.setEditable(true);
else
gradeinfo.setEditable(false);
}
else if (obj == area){
if(area.isSelected())
areainfo.setEditable(true);
else
areainfo.setEditable(false);
}
else if (obj == rent){
if(rent.isSelected())
rentinfo.setEditable(true);
else
rentinfo.setEditable(false);
}
else if (obj == structure){
if(structure.isSelected())
structureinfo.setEditable(true);
else
structureinfo.setEditable(false);
}
}
}
class ComboBoxListener implements ActionListener {
public void actionPerformed (ActionEvent ae) {
itemText = comBox.getSelectedItem().toString();
}
}
class UpdateHouseInfo extends Thread {
private String query ;
public UpdateHouseInfo(String query){
this.query = query;
}
public void run(){
updateHouseInfo();
}
private void updateHouseInfo(){
StringBuffer resultBuffer = new StringBuffer();
try{
Statement Stmt = connection.createStatement();
Stmt.executeUpdate("UPDATE 空房文件 SET "+query+" WHERE "+itemText+"='"+
textfiled.getText()+"'");
ResultSet result = Stmt.executeQuery("SELECT * "+"FROM 空房文件 WHERE "+itemText+"='"+
textfiled.getText()+"'");
ResultSetMetaData metadata = result.getMetaData();
while (result.next()) {
for(int i=1;i<=metadata.getColumnCount();i++){
String label = metadata.getColumnLabel(i);
String info = result.getString(i);
resultBuffer.append(label+":"+info+"\n");
}
}
resultBuffer.append("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"+"\n");
textArea.append("更新成功!!"+"\n");
textArea.append(resultBuffer.toString());
Stmt.close();
}
catch(Exception e){e.printStackTrace();}
}
}
}
//////////////////////////////////
/*
包含房屋得相关信息。
*/
public class VacantHouseInfo {
private String house_number = null;
private String house_grade = null;
private String house_area = null;
private String house_rent = null;
private String house_structure = null;
public VacantHouseInfo(){
}
public void setAllAttribute(String houseInfo[]){
house_number = houseInfo[0];
house_grade = houseInfo[1];
house_area = houseInfo[2];
house_rent = houseInfo[3];
house_structure = houseInfo[4];
}
public String getHouseNumber(){
return house_number;
}
public String getHouseGrade(){
return house_grade;
}
public String getHouseArea(){
return house_area;
}
public String getHouseRent(){
return house_rent;
}
public String getHouseStructure(){
return house_structure;
}
}
/////////////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.* ;
public class TrySql extends JFrame {
private static Connection connection ;
public TrySql() {
initializeDemo();
}
public static void initConnection(){
try{
connection = getConnection();
}
catch(Exception e){e.printStackTrace();}
}
public void disConnection(){
try{
connection.close();
}
catch(Exception e){e.printStackTrace();}
clearMemory();
}
public void initializeDemo(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
disConnection();
System.exit(0);
}
});
JMenu fileMenu = new JMenu("文件");
JMenuItem exitItem = new JMenuItem("退出");
fileMenu.add(exitItem);
exitItem.addActionListener(new exitMenuHandler());
JMenu helpMenu = new JMenu("帮助");
JMenuItem aboutItem = new JMenuItem("关于");
helpMenu.add(aboutItem);
aboutItem.addActionListener(new aboutMenuHandler());
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
getContentPane().setLayout(new BorderLayout());
JTabbedPane tabbedpane = new JTabbedPane();
tabbedpane.add(new DistributeHousePanel(connection),"用户分房申请演示");
tabbedpane.add(new QuiteHousePanel(connection),"用户退房申请演示");
tabbedpane.add(new PrepareHousePanel(connection),"用户调房申请演示");
tabbedpane.add(new QueryHouseInfoPanel(connection),"用户查询信息演示");
tabbedpane.add(new MasterHouseInfoPanel(connection),"住房管理科管理演示");
getContentPane().add(tabbedpane, BorderLayout.CENTER);
setSize(750,560);
setTitle("住房管理系统演示");
setResizable(false);
center(this);
pack();
setVisible(true);
}
/*
把主窗口置于屏幕中间。
*/
public void center(Component C) {
Dimension SS = C.getToolkit().getScreenSize();
Dimension CS = C.getSize();
C.setLocation ((SS.width - CS.width) / 2,(SS.height - CS.height) / 2);
}
/*
连接到数据库
*/
public static Connection getConnection()throws SQLException {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){e.printStackTrace();}
String url = "jdbc:odbc:住房管理系统";
String username = "PUBLIC";
String password = "PUBLIC";
return
DriverManager.getConnection(url, username, password);
}
public static void main(String args[]){
initConnection();
Locale.setDefault(Locale.US);
new TrySql();
}
public static void clearMemory(){
connection = null;
}
/*
退出数据库。
*/
class exitMenuHandler implements ActionListener {
public void actionPerformed( ActionEvent ae ) {
disConnection();
System.exit(0);
}
}
/*
显示帮助文件。
*/
class aboutMenuHandler implements ActionListener {
public void actionPerformed( ActionEvent ae ) {
new JDialog().setVisible(true);
}
}
}
//////////////////////
以上程序只是主要部分的代码。
如果需要所有代码可bootcool@163.net和我联系。
请各位前辈多多指教。
谢谢。