📄 makewalkin.java
字号:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import javax.swing.*;
public class MakeWalkIn {
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;
JFrame f=new JFrame();
Container contentPane=f.getContentPane();
String[] data1 = {"1", "2", "3", "4","5","6","7","8"};
String[] data2 = {"1", "2", "3", "4","5"};
JComboBox jComboBox1 = new JComboBox(data1);
JComboBox jComboBox2 = new JComboBox(data2);
JScrollPane listScroll1=new JScrollPane(jComboBox1);
JScrollPane listScroll2=new JScrollPane(jComboBox2);
JLabel jLabel1=new JLabel("OID");
JLabel jLabel2=new JLabel("TIME");
JLabel jLabel3=new JLabel("TNO");
JLabel jLabel4=new JLabel("COVERS");
Calendar calendar=Calendar.getInstance();
int year,month,day,week,hour,minute;
JLabel jLabel5=new JLabel(" ");
JButton jButton1=new JButton("确定");
JButton jButton2=new JButton("退出");
JTextField jTextField=new JTextField(10);
public MakeWalkIn(){
//contentPane.setLayout(new GridLayout(3,4));
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
contentPane.add(jLabel1);
contentPane.add(jTextField);
contentPane.add(jLabel2);
int a=getyear();
int b=getmonth();
int c=getday();
int d=gethour();
int e=getminute();
jLabel5=new JLabel(""+a+"年"+b+"月"+c+"日"+d+"时"+e+"分");
contentPane.add(jLabel5);
contentPane.add(jLabel3);
contentPane.add(listScroll1);
contentPane.add(jLabel4);
contentPane.add(listScroll2);
contentPane.add(jButton1);
contentPane.add(jButton2);
f.setSize(340,150);
f.setVisible(true);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String oid=jTextField.getText();
String covers=(String)jComboBox2.getSelectedItem();
String tno=(String)jComboBox1.getSelectedItem();
JOptionPane.showConfirmDialog(null, "确定OID为"+oid+","+"桌号为"+tno+","+"人数为"+covers+"的预约?", "取消", JOptionPane.YES_NO_OPTION);
makeReservation();
}
});
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.dispose();
}
});
}//
//记录数据
void makeReservation(){
String id=jTextField.getText();
if(id.equals("")){
JOptionPane.showMessageDialog(null, "OID不能为空!", "错误!", JOptionPane.ERROR_MESSAGE);
f.dispose();
}
int oid=Integer.parseInt(id);
String covers=(String)jComboBox2.getSelectedItem();
String tno=(String)jComboBox1.getSelectedItem();
try{
Class.forName(jdbcDriver);
}
catch(ClassNotFoundException se){
System.err.print(se.getMessage());
}
try{
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement();
String AddTable1="INSERT INTO Tableinfo values ("+"'"+oid+"'"+","+"'"+tno+"'"+","+"'"+covers+"'"+");";
String AddTable2="INSERT INTO Walkin values ("+"'"+oid+"'"+","+"'"+tno+"'"+","+"'"+covers+"'"+");";
stmt.executeUpdate(AddTable1);
stmt.executeUpdate(AddTable2);
jTextField.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 getyear(){
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
int year=calendar.get(Calendar.YEAR);
return year;
}
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());
day=calendar.get(Calendar.DAY_OF_MONTH);
return day;
}
public int gethour(){
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
hour=calendar.get(Calendar.HOUR_OF_DAY);
return hour;
}
public int getminute(){
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
minute=calendar.get(Calendar.MINUTE);
return minute;
}
}//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -