📄 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 + -