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

📄 timechange.java

📁 餐厅管理系统毕业设计
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
public class TimeChange {
	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;
    ResultSet rSet=null;
    JLabel jLabel0=new JLabel("预约号码"); 
    JLabel jLabel1=new JLabel("月");  
    JLabel jLabel2=new JLabel("日"); 
    JLabel jLabel3=new JLabel("时"); 
    JLabel jLabel4=new JLabel("分"); 
    JTextField jTextField0=new JTextField(10);
    JTextField jTextField1=new JTextField(10);
    JTextField jTextField2=new JTextField(10);
    JTextField jTextField3=new JTextField(10);
    JTextField jTextField4=new JTextField(10);
    JButton jButton1=new JButton("修改预约时间!");
	JButton jButton2=new JButton("退出");
    JFrame f=new JFrame();
    Container contentPane=f.getContentPane();
	public TimeChange(){
		  contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));  
		  contentPane.add(jTextField0);
		  contentPane.add(jLabel0);
		  contentPane.add(jTextField1);
		  contentPane.add(jLabel1);
		  contentPane.add(jTextField2);
    	  contentPane.add(jLabel2);
    	  contentPane.add(jTextField3);
    	  contentPane.add(jLabel3);
    	  contentPane.add(jTextField4);
    	  contentPane.add(jLabel4);
    	  contentPane.add(jButton1);
    	  contentPane.add(jButton2);
    	  f.setSize(200,200);
      	  f.setVisible(true);
      	  jButton1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				timechange();
			}
		});  
      	  jButton2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				f.dispose();
			}
		}); 
      }//
	 void timechange(){
		 String id=jTextField0.getText();
		 int oid=Integer.parseInt(id);
		 String month=jTextField1.getText();
    	 int m2=Integer.parseInt(month);
    	 int m1=getmonth();
    	 if(m2<1||m2>12||(m2<m1&&m2>0)){
    		 JOptionPane.showMessageDialog(null, "无效月份", "错误!", JOptionPane.ERROR_MESSAGE);
    		 f.dispose();
    	 }
    	 String day=jTextField2.getText();
    	 int d2=Integer.parseInt(day);
    	 if((m2==4||m2==6||m2==9||m2==11)&&d2==31){
    		 JOptionPane.showMessageDialog(null, "无效日期", "错误!", JOptionPane.ERROR_MESSAGE);
    	     f.dispose();
    	 }
    	 if(d2<1||d2>31){
    		 JOptionPane.showMessageDialog(null, "无效日期", "错误!", JOptionPane.ERROR_MESSAGE);
    	     f.dispose(); 
    	 }
    	 String hour=jTextField3.getText();
    	 int h2=Integer.parseInt(hour);
    	 if(h2<1||h2>24){
    		 JOptionPane.showMessageDialog(null, "无效时间", "错误!", JOptionPane.ERROR_MESSAGE);
    	     f.dispose(); 
    	 }
    	 String minute=jTextField4.getText();
    	 int mm=Integer.parseInt(minute);
    	 if(mm<0||mm>59){
    		 JOptionPane.showMessageDialog(null, "无效时间", "错误!", JOptionPane.ERROR_MESSAGE);
    	     f.dispose(); 
    	 }
    	 String date=""+month+"."+day;
    	 String time=""+hour+":"+minute;
    	 try{
	          Class.forName(jdbcDriver);
	          }
	     catch(ClassNotFoundException se){
	          System.err.print(se.getMessage());
	       }
         try{
        	     String reserch="SELECT * FROM reservation where oid="+oid+";";
        	     String change1="update reservation set date="+"\""+date+"\""+"where oid="+oid+";";
        	     String change2="update reservation set time="+"\""+time+"\""+"where oid="+oid+";";
        	     con=DriverManager.getConnection(url,username,password);
                 stmt=con.createStatement();
                 rSet=stmt.executeQuery(reserch);
                 if(rSet.next()==false){
                	 JOptionPane.showMessageDialog(null, "没有预约号码为"+oid+"的预约", "错误!", JOptionPane.ERROR_MESSAGE);
                	 f.dispose();
                 }
                 stmt.executeUpdate(change1);
                 stmt.executeUpdate(change2);
                 jTextField1.setText("");
                 jTextField2.setText("");
                 jTextField3.setText("");
                 jTextField4.setText("");
                 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){}          //忽略
               }
    }
    public int getmonth(){
    	Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        int month=calendar.get(Calendar.MONTH)+1;   
        return month;
    }
    public int getday(){
    	Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        int day=calendar.get(Calendar.DAY_OF_MONTH);  
        return day;
    }
}//

⌨️ 快捷键说明

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