用java實現一個簡單的房屋管理程式。 (轉)

worldblog發表於2007-12-11
用java實現一個簡單的房屋管理程式。 (轉)[@more@]

 


最近學習《工程導論》(清華大學出版)
再需求分析中有一個關於住房管理的實現。
於是用寫了一個原型系統。(很簡陋)
主要是為了學習jc的一些特性。
這把部分列出,和大家分享。
問題可參看上面所說的書。
由於時間有限,說明不太準確。今後我會不斷修改的。


/*
住戶房屋申請表
*/



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..*;


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  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..*;
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 f= 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 ranIndex = (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(
  " * 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  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) {
  obj = ae.get();
  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 dinnection(){
  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..odbc.JdbcOdbc");
  }
  catch(Exception e){e.printStackTrace();}
  String url = "jdbc:odbc:住房管理系統";
  String username = "PUBLIC";
  String pass = "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);
  }
 }
}


//////////////////////


以上程式只是主要部分的程式碼。


to:如果需要所有程式碼可bootcool@163和我聯絡">如果需要所有程式碼可bootcool@163.net和我聯絡。


請各位前輩多多指教。


謝謝。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-991428/,如需轉載,請註明出處,否則將追究法律責任。

相關文章