?? resturant.java
字號(hào):
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
public class Resturant {
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;
JFrame f=new JFrame();
Container contentPane=f.getContentPane();
JLabel jLabel1=new JLabel("預(yù)定號(hào)碼 ");
JLabel jLabel2=new JLabel("桌號(hào)");
JLabel jLabel3=new JLabel("人數(shù) ");
JLabel jLabel4=new JLabel("預(yù)約時(shí)間 ");
JLabel jLabel5=new JLabel("月 ");
JLabel jLabel6=new JLabel("日 ");
JLabel jLabel7=new JLabel("時(shí) ");
JLabel jLabel8=new JLabel("分 ");
JLabel jLabel9=new JLabel("姓名 ");
JLabel jLabel10=new JLabel("電話 ");
JTextField jTextField1=new JTextField(2);
JTextField jTextField2=new JTextField(2);
JTextField jTextField3=new JTextField(2);
JTextField jTextField4=new JTextField(2);
JTextField jTextField5=new JTextField(2);
JTextField jTextField6=new JTextField(8);
JTextField jTextField7=new JTextField(8);
//位置數(shù)
String [] data1 = {"1","2","3","4","5","6","7","8"};
//桌號(hào)
String [] data2 = {"1","2","3","4","5"};
JComboBox jComboBox1 = new JComboBox(data1);
JComboBox jComboBox2 = new JComboBox(data2);
JButton jButton1=new JButton("預(yù)定");
JButton jButton2=new JButton("退出");
JScrollPane listScroll1=new JScrollPane(jComboBox1);
JScrollPane listScroll2=new JScrollPane(jComboBox2);
public Resturant(){
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
contentPane.add(jLabel1);
contentPane.add(jTextField1);
contentPane.add(jLabel2);
contentPane.add( listScroll2);
contentPane.add(jLabel3);
contentPane.add( listScroll1);
contentPane.add(jLabel4);
contentPane.add(jTextField2);
contentPane.add(jLabel5);
contentPane.add(jTextField3);
contentPane.add(jLabel6);
contentPane.add(jTextField4);
contentPane.add(jLabel7);
contentPane.add(jTextField5);
contentPane.add(jLabel8);
contentPane.add(jLabel9);
contentPane.add(jTextField6);
contentPane.add(jLabel10);
contentPane.add(jTextField7);
contentPane.add(jButton1);
contentPane.add(jButton2);
f.setSize(300,280);
f.setVisible(true);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id=jTextField1.getText();
String covers=(String)jComboBox1.getSelectedItem();
String name=jTextField6.getText();
String tno=(String)jComboBox2.getSelectedItem();
String tel=jTextField7.getText();
JOptionPane.showConfirmDialog(null, "確定OID為"+id+","+"桌號(hào)為"+tno+","+"人數(shù)為"+covers+"'"+"姓名為"+name+","+"電話為"+tel+"的預(yù)約?", "取消", JOptionPane.YES_NO_OPTION);
makeReservation();
}
});
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null, "確定要退出?", "退出", JOptionPane.YES_NO_OPTION);
f.dispose();
}
});
}//
void makeReservation(){
String id=jTextField1.getText();
if(id.equals("")){
JOptionPane.showMessageDialog(null, "OID不能為空!", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
int oid=Integer.parseInt(id);
String covers=(String)jComboBox1.getSelectedItem();
String month=jTextField2.getText();
int m2=Integer.parseInt(month);
int m1=getmonth();
if(m2<1||m2>12||(m2<m1&&m2>0)){
JOptionPane.showMessageDialog(null, "無效月份", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
String day=jTextField3.getText();
int d2=Integer.parseInt(day);
int d1=getday();
if((m2==4||m2==6||m2==9||m2==11)&&d2==31){
JOptionPane.showMessageDialog(null, "無效日期", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
if(d2<1||d2>31){
JOptionPane.showMessageDialog(null, "無效日期", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
String hour=jTextField4.getText();
int h2=Integer.parseInt(hour);
if(h2<1||h2>24){
JOptionPane.showMessageDialog(null, "無效時(shí)間", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
String minute=jTextField5.getText();
int mm=Integer.parseInt(minute);
if(mm<0||mm>59){
JOptionPane.showMessageDialog(null, "無效時(shí)間", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
String date=""+month+"."+day;
String time=""+hour+":"+minute;
String name=jTextField6.getText();
if(name.equals("")){
JOptionPane.showMessageDialog(null, "姓名不能為空!", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
String tno=(String)jComboBox2.getSelectedItem();
String tel=jTextField7.getText();
if(tel.equals("")){
JOptionPane.showMessageDialog(null, "電話不能為空!", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
String arrivetime=null;
try{
Class.forName(jdbcDriver);
}
catch(ClassNotFoundException se){
System.err.print(se.getMessage());
}
try{
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
String reserch="SELECT * FROM reservation where oid="+oid+";";
String AddReservation="INSERT INTO Reservation values (" +"'"+ oid +"'"+"," +"'"+ covers +"'"+"," + "'"+ date +"'" +","+"'"+time+"'"+","+"'"+tno+"'"+","+"'"+name+"'"+","+"'"+arrivetime+"'"+");";
String AddCustomer="INSERT INTO Customer values ("+"'"+oid+"'"+","+"'"+name+"'"+","+"'"+tel+"'"+");";
String AddTable="INSERT INTO Tableinfo values ("+"'"+oid+"'"+","+"'"+tno+"'"+","+"'"+covers+"'"+");";
rSet=stmt.executeQuery(reserch);
if(rSet.next()==true){
JOptionPane.showMessageDialog(null, "預(yù)約號(hào)碼重復(fù)", "錯(cuò)誤!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
stmt.executeUpdate(AddReservation);
stmt.executeUpdate(AddCustomer);
stmt.executeUpdate(AddTable);
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
jTextField7.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;
}
}//
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -