?? bookservice.java
字號:
package com.csbook.restaurant;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author pengtao
* @version 1.0
*/
public class BookService extends JInternalFrame {
JPanel panel1 = new JPanel();
JPanel jPanel1 = new JPanel();
JPanel jPanel3 = new JPanel();
BorderLayout borderLayout4 = new BorderLayout();
JLabel jLabel10 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField customer = new JTextField();
JPanel jPanel6 = new JPanel();
JLabel jLabel6 = new JLabel();
JLabel jLabel1 = new JLabel();
JTextField tel = new JTextField();
JLabel jLabel3 = new JLabel();
GridLayout gridLayout2 = new GridLayout(4,4);
JTextField bookDate = new JTextField();
JTextField operator = new JTextField();
JLabel jLabel5 = new JLabel();
JLabel jLabel15 = new JLabel();
JLabel jLabel16 = new JLabel();
JLabel jLabel17 = new JLabel();
JLabel jLabel18 = new JLabel();
JLabel jLabel19 = new JLabel();
JLabel jLabel20 = new JLabel();
TextArea remark=new TextArea();
JButton cancel = new JButton();
JButton ok = new JButton();
String currentOper="";
JLabel jLabel8 = new JLabel();
JTextField roomRemark = new JTextField();
JLabel jLabel21 = new JLabel();
JLabel jLabel12 = new JLabel();
JTextField roomType = new JTextField();
JLabel jLabel14 = new JLabel();
JPanel jPanel8 = new JPanel();
JComboBox roomNo = new JComboBox();
JLabel jLabel11 = new JLabel();
JLabel jLabel22 = new JLabel();
JLabel jLabel13 = new JLabel();
JLabel jLabel7 = new JLabel();
GridLayout gridLayout4 = new GridLayout(4,4);
JTextField roomCapacity = new JTextField();
JLabel jLabel9 = new JLabel();
JTextField roomDept = new JTextField();
BorderLayout borderLayout1 = new BorderLayout();
public BookService(String title,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable,String operator) {
super(title,resizable,closable,maximizable,iconifiable);
this.currentOper=operator;
try {
jbInit();
//pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
panel1.setLayout(borderLayout1);
jPanel3.setLayout(borderLayout4);
jLabel10.setText("其它信息:");
remark.setText("");
jLabel4.setText("客人姓名:");
jLabel2.setText("業務員:");
customer.setText("");
jPanel6.setLayout(gridLayout2);
jLabel6.setOpaque(false);
jLabel6.setRequestFocusEnabled(true);
jLabel6.setText("");
jLabel1.setText("訂單基本信息:");
jLabel3.setText("預定日期:");
jLabel5.setOpaque(false);
jLabel5.setRequestFocusEnabled(true);
jLabel5.setText("");
jLabel16.setText("客人信息");
jLabel17.setText("聯系電話:");
jLabel20.setText("");
jLabel19.setText("");
jLabel18.setText("");
jLabel15.setText("");
bookDate.setText("");
operator.setText("");
tel.setText("");
cancel.setText("取消");
cancel.addActionListener(new BookService_cancel_actionAdapter(this));
ok.setText("確定");
ok.addActionListener(new BookService_ok_actionAdapter(this));
jLabel8.setText("所屬類別:");
jLabel21.setOpaque(false);
jLabel21.setRequestFocusEnabled(true);
jLabel21.setText("");
jLabel12.setText("房臺備注信息:");
roomType.setText("");
jLabel14.setOpaque(false);
jLabel14.setRequestFocusEnabled(true);
jLabel14.setText("");
jPanel8.setLayout(gridLayout4);
roomNo.addActionListener(new BookService_roomNo_actionAdapter(this));
jLabel11.setText("房臺信息:");
jLabel22.setText("容納人數:");
jLabel13.setText("房臺編號:");
jLabel7.setOpaque(false);
jLabel7.setRequestFocusEnabled(true);
jLabel7.setText("");
roomCapacity.setText("");
jLabel9.setText("所屬部門:");
roomDept.setText("");
getContentPane().add(panel1, BorderLayout.CENTER);
panel1.add(jPanel6, BorderLayout.NORTH);
jPanel6.add(jLabel1, null);
jPanel6.add(jLabel20, null);
jPanel6.add(jLabel19, null);
jPanel6.add(jLabel18, null);
jPanel6.add(jLabel3, null);
jPanel6.add(bookDate, null);
jPanel6.add(jLabel2, null);
jPanel6.add(operator, null);
jPanel6.add(jLabel16, null);
jPanel6.add(jLabel15, null);
jPanel6.add(jLabel5, null);
jPanel6.add(jLabel6, null);
jPanel6.add(jLabel4, null);
jPanel6.add(customer, null);
jPanel6.add(jLabel17, null);
jPanel6.add(tel, null);
panel1.add(jPanel8, BorderLayout.CENTER);
jPanel8.add(jLabel11, null);
jPanel8.add(jLabel21, null);
jPanel8.add(jLabel14, null);
jPanel8.add(jLabel7, null);
jPanel8.add(jLabel13, null);
jPanel8.add(roomNo, null);
jPanel8.add(jLabel9, null);
jPanel8.add(roomDept, null);
jPanel8.add(jLabel8, null);
jPanel8.add(roomType, null);
jPanel8.add(jLabel22, null);
jPanel8.add(roomCapacity, null);
jPanel8.add(jLabel12, null);
jPanel8.add(roomRemark, null);
panel1.add(jPanel3, BorderLayout.SOUTH);
jPanel3.add(jLabel10, BorderLayout.NORTH);
jPanel3.add(remark, BorderLayout.CENTER);
this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
jPanel1.add(ok, null);
jPanel1.add(cancel, null);
operator.setText(currentOper);
operator.setEditable(false);
this.prepareShow();
}
void ok_actionPerformed(ActionEvent e) {
//驗證輸入參數的合法性
if(customer.getText().trim().equals("")||tel.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(this, "顧客姓名和聯系電話不能為空", "錯誤",
JOptionPane.ERROR_MESSAGE);
}
StringTokenizer st=new StringTokenizer(bookDate.getText(),"/");
if(st.countTokens()!=3){
JOptionPane.showMessageDialog(this, "請按照年/月/日的格式輸入時間", "提示",
JOptionPane.ERROR_MESSAGE);
return;
}
//檢查該房臺是否已被預訂
boolean occupied=this.roomOccupied(roomNo.getSelectedItem().toString());
if(occupied)
{
JOptionPane.showMessageDialog(this, "該房臺在該日已被預訂", "提示",JOptionPane.PLAIN_MESSAGE);
return;
}
//預訂房臺
Connection conn=null;
PreparedStatement ps=null;
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("insert into roomBook(operator,customer,tel,roomNo,bookDate,remark) values(?,?,?,?,?,?)");
ps.setString(1,operator.getText());
ps.setString(2,customer.getText());
ps.setString(3,tel.getText());
ps.setString(4,roomNo.getSelectedItem().toString());
ps.setString(5,bookDate.getText());
ps.setString(6,remark.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(this, "已成功預定該房臺", "操作",JOptionPane.PLAIN_MESSAGE);
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
private boolean roomOccupied(String roomNo)
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean occupied=false;
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select BookNo from roomBook where roomNo=? and datediff(Day,roomBook.bookDate,CAST(? AS datetime))=0");
ps.setString(1,roomNo);
ps.setString(2,bookDate.getText());
rs=ps.executeQuery();
if(rs.next())
occupied=true;
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
return occupied;
}
void cancel_actionPerformed(ActionEvent e) {
this.dispose();
}
public void prepareShow(){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select id from room");
rs=ps.executeQuery();
while(rs.next())
roomNo.addItem(rs.getString("id"));
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
void roomNo_actionPerformed(ActionEvent e) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
String currentRoom=roomNo.getSelectedItem().toString();
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Restaurant;user=user;password=user");
ps=conn.prepareStatement("select * from room where id=?");
ps.setString(1,currentRoom);
rs=ps.executeQuery();
if(rs.next()){
roomDept.setText(rs.getString("deptName"));
roomType.setText(rs.getString("type"));
roomCapacity.setText(rs.getString("capacity"));
roomRemark.setText(rs.getString("remark"));
}
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
if(rs!=null)try{rs.close();}catch(SQLException ignore){}
if(ps!=null)try{ps.close();}catch(SQLException ignore){}
if(conn!=null)try{conn.close();}catch(SQLException ignore){}
}
}
}
class BookService_ok_actionAdapter implements java.awt.event.ActionListener {
BookService adaptee;
BookService_ok_actionAdapter(BookService adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.ok_actionPerformed(e);
}
}
class BookService_cancel_actionAdapter implements java.awt.event.ActionListener {
BookService adaptee;
BookService_cancel_actionAdapter(BookService adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.cancel_actionPerformed(e);
}
}
class BookService_roomNo_actionAdapter implements java.awt.event.ActionListener {
BookService adaptee;
BookService_roomNo_actionAdapter(BookService adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.roomNo_actionPerformed(e);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -