⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookingsystem.java

📁 餐厅管理系统毕业设计
💻 JAVA
字号:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
public class BookingSystem implements MouseListener {
	String username = "root";
    String password = "right";
    String jdbcDriver="com.mysql.jdbc.Driver";
    String url="jdbc:mysql://localhost:3306/resturant";  
    Connection con = null;
    Statement stmt = null;
    JTable jTable=new JTable();
    JScrollPane jScroll;
	Vector vector;
	AbstractTableModel tm;
	String title[]={"预定号码","姓名","电话"};
	ResultSet rSet=null;  
	JFrame f=new JFrame();
	Container contentPane=f.getContentPane();  
	JButton jButton1=new JButton("查询");
	JButton jButton2=new JButton("取消预约");
	JButton jButton3=new JButton("刷新");
	JButton jButton4=new JButton("退出");
	public BookingSystem(){
		contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
		contentPane.add(jButton1);
		contentPane.add(jButton2);
		contentPane.add(jButton3);
		contentPane.add(jButton4);
		display();
		jTable.addMouseListener(this);
		//jTable.addMouseListener((MouseListener) this);
		f.setSize(480,500);
    	f.setVisible(true);
		//查询
    	jButton1.addActionListener(new ActionListener() {
      	     public void actionPerformed(ActionEvent e) {
      	    	reserch();
           }
      	 });
		//取消
    	jButton2.addActionListener(new ActionListener() {
     	     public void actionPerformed(ActionEvent e) {
     	    	String inputValue = JOptionPane.showInputDialog("请输入想要删除的OID");
     	    	if(inputValue.equals("")){
     	    		JOptionPane.showMessageDialog(null, "OID不能为空!", "错误!", JOptionPane.ERROR_MESSAGE);
     	    	}
     	    	else{
     	    	Connection con = null;
     	   	    Statement stmt = null;
     	   	    String cc="SELECT * FROM Customer WHERE oid"+"="+inputValue+";";
     	   	    try{
  	                 Class.forName(jdbcDriver);
  	            }
     	   	    catch(ClassNotFoundException se){
   	                 System.err.print(se.getMessage());
   	                 f.dispose();
   	            }
     	   	    try{
     	   	        con=DriverManager.getConnection(url,username,password);
                    stmt=con.createStatement();
                    rSet=stmt.executeQuery(cc);
                    if(rSet.next()==false){
                    	 JOptionPane.showMessageDialog(null, "没有OID为"+inputValue+"的预约", "错误!", JOptionPane.ERROR_MESSAGE);}
                     
     	   	        else {
     	    	         Object[] options = { "确定", "取消" };
                         JOptionPane.showOptionDialog(null, "确定取消OID为"+inputValue+"的预约?", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null, options, options[0]);
                         cancel(inputValue);
                     }
     	   	        }
     	   	    catch(SQLException se){
                         System.err.println(se.getMessage());
                         f.dispose();
                                       }
                finally{
                try{
                    if(con!=null){
                                   con.close();
                                   }
                    if(stmt!=null){
                                   stmt.close();
                                   }
                }catch(Exception ex){f.dispose();}          //忽略
                   }
     	    	}
     	      }
     	 });
		//刷新
    	jButton3.addActionListener(new ActionListener() {
    	     public void actionPerformed(ActionEvent e) {
    	    	 reserch();
         }
    	 });
    	//退出
		jButton4.addActionListener(new ActionListener() {
     	     public void actionPerformed(ActionEvent e) {
     	    	f.dispose();
          }
     	 });
		
    }//
	
    //  显示预约
    void reserch(){
		Connection con = null;
   	    Statement stmt = null;
   	    String Reserch="SELECT * FROM Customer;";
	    	 try{
	          Class.forName(jdbcDriver);
	          }
	         catch(ClassNotFoundException se){
	         System.err.print(se.getMessage());
	          }
             try{
        	 con=DriverManager.getConnection(url,username,password);
             stmt=con.createStatement();
             rSet=stmt.executeQuery(Reserch);
             vector.removeAllElements();
             while(rSet.next()){
             Vector rec_vector=new Vector();
           	 rec_vector.addElement(String.valueOf(rSet.getString("oid")));
           	 rec_vector.addElement(String.valueOf(rSet.getString("name")));
           	 rec_vector.addElement(String.valueOf(rSet.getString("phoneNumber")));
           	 vector.addElement(rec_vector);
           	}
             tm.fireTableStructureChanged();               //更新表格
             rSet.close();
             con.close();
           }
         catch(SQLException se){
           System.err.println(se.getMessage());
            }
         finally{
         try{
               if(con!=null){
                              con.close();
                              }
               if(stmt!=null){
                              stmt.close();
                              }
         }catch(Exception ex){}          //忽略
              }
	}
    
	
	//取消预约
	void cancel(String bb){
		Connection con = null;
   	    Statement stmt = null;
   	    String Cancel1="DELETE FROM Reservation WHERE oid "+"="+bb+";";
   	    String Cancel2="DELETE FROM Customer WHERE oid "+"="+bb+";";
   	    String Cancel3="DELETE FROM Tableinfo WHERE oid "+"="+bb+";";
	    	 try{
	          Class.forName(jdbcDriver);
	          }
	         catch(ClassNotFoundException se){
	         System.err.print(se.getMessage());
	          }
             try{
        	 con=DriverManager.getConnection(url,username,password);
             stmt=con.createStatement();
             stmt.executeUpdate(Cancel1);
             stmt.executeUpdate(Cancel2);
             stmt.executeUpdate(Cancel3);
             con.close();
             }
             catch(SQLException se){
                 System.err.println(se.getMessage());
                  }
             finally{
                 try{
                     if(con!=null){
                                    con.close();
                                   }
                     if(stmt!=null){
                                    stmt.close();
                                   }
             }catch(Exception ex){}  
            }
	}
    
	void display(){
    	vector=new Vector();
    	tm=new AbstractTableModel(){
              public int getColumnCount(){         //行
            	  return title.length;
              }
              public int getRowCount(){            //列
            	  return vector.size();
              }
              public Object getValueAt(int row,int column){           //取得单元格的属性值
            	  if(!vector.isEmpty()){
            		  return ((Vector)vector.elementAt(row)).elementAt(column);
            	  }
            	  else{
            		  return null;
            	  }
              }
              public void setValueAt(Object value,int row,int column){  //数据模型不可编辑,方法设置为空
            	        }    
              
    	
    	      public String getColumnName(int column){     //区列名 
    		          return title[column];
    	              }
    	      public Class getColimnClass(int c){
    		          return getValueAt(0,c).getClass();    //取得列所属对象类
    	              }
    	      public boolean isCellEditable(int row,int column){       //设置单元格不可编辑
    		          return false;
    	              }
    	};
    	jTable=new JTable(tm);
    	jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
    	jTable.setToolTipText("DISPLAY");             //设置提示
    	jTable.setAutoResizeMode(jTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);    //设置表格调整尺寸模式
    	jTable.setCellSelectionEnabled(false);         //设置单元格的选择方式
    	jTable.setShowHorizontalLines(true);        //是否有分割线
    	jTable.setShowVerticalLines(true);
    	jTable.setRowSelectionAllowed(true);        //设置表的行可以选择
    	jTable.setBounds(6, 20, 540, 250);
        jScroll=new JScrollPane(jTable);
        jScroll.setBounds(6, 20, 540, 250);
        contentPane.add(jScroll);
	}

	public void mouseClicked(MouseEvent e) {
		//System.out.println("table seccessful!");
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO 自动生成方法存根
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO 自动生成方法存根
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO 自动生成方法存根
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO 自动生成方法存根
		
	}
}//
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -