?? hotel.java
字號:
import javax.swing.*;//GUI
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class Hotel extends JFrame {
JButton jButton1 = new JButton();//Add guest
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
Connection connect;
String url;
boolean roomordered;
int price_eachday=1;//房價
GridLayout gridLayout1 = new GridLayout();
String Slected[]={"male","female"};
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenu jMenu2 = new JMenu();
JMenu jMenu3 = new JMenu();
JMenu jMenu4 = new JMenu("鏈接");
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem("責任表duty");
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
JComboBox jComboBox1 = new JComboBox(Slected);//性別組合框
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
JTextField jTextField4 = new JTextField();
JTextField jTextField5 = new JTextField();
JTextField jTextField6 = new JTextField();
Clocklabel clocklabel1 = new Clocklabel();//調用Clocklabel類
JScrollPane jScrollPane1 = new JScrollPane();//滾動面板
JTextArea jTextArea1 = new JTextArea();//文本區,用于顯示"連接成功"等操作提示提示
public Hotel() {
try {
Init();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void Init() throws Exception {
//界面設置
this.getContentPane().setLayout(null);
this.setLocale(java.util.Locale.getDefault());
//關于5個按鈕
jButton1.setText("添加客戶");
jButton1.addActionListener(new Hotel_jButton1_actionAdapter(this));
jButton2.setText("刪除客戶");
jButton2.addActionListener(new Hotel_jButton2_actionAdapter(this));
jButton3.setText("房客查尋");
jButton3.addActionListener(new Hotel_jButton3_actionAdapter(this));
jButton4.setText("空房查尋");
jButton4.addActionListener(new Hotel_jButton4_actionAdapter(this));
jButton5.setText("清空");
jButton5.addActionListener(new Hotel_jButton5_actionAdapter(this));
//4張面板布局設置
jPanel1.setLayout(gridLayout1);
jPanel2.setLayout(null);
jPanel3.setLayout(null);
jPanel4.setLayout(null);
//關于6個標簽 2~7
jLabel2.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel2.setText(" 姓名");
jLabel2.setBounds(new Rectangle(0, 0, 161, 32));
jLabel3.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel3.setText(" 性別");
jLabel3.setBounds(new Rectangle(0, 32, 161, 32));
jLabel4.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel4.setText(" 年齡");
jLabel4.setBounds(new Rectangle(0, 64, 161, 32));
jLabel5.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel5.setText(" 身份證號");
jLabel5.setBounds(new Rectangle(0, 96, 161, 32));
jLabel6.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel6.setText(" 房號");
jLabel6.setBounds(new Rectangle(0, 128, 161, 32));
jLabel7.setFont(new java.awt.Font("Dialog", 0, 13));
jLabel7.setText(" 當前金額");
jLabel7.setBounds(new Rectangle(0, 160, 161, 32));
//設置4張面板
jPanel1.setBounds(new Rectangle(0, 1, 493, 41));
jPanel2.setBounds(new Rectangle(-2, 78, 471, 202));
jPanel3.setDebugGraphicsOptions(0);
jPanel3.setBounds(new Rectangle(2, 0, 322, 197));
jPanel4.setDebugGraphicsOptions(0);
jPanel4.setBounds(new Rectangle(335, 0, 118, 202));
//關于3個菜單和2個菜單項
jMenu1.setText(" 關于");
jMenu2.setText("作者及版權");
jMenu3.setText(" 使用方法");
jMenuItem1.setText("Read me");
jMenuItem1.addActionListener(new Hotel_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("Read_me");
jMenuItem2.addActionListener(new Hotel_jMenuItem2_actionAdapter(this));
jMenuItem3.addActionListener(new Hotel_jMenuItem3_actionAdapter(this));
//關于組合框(下拉列表)
jComboBox1.setDebugGraphicsOptions(0);
jComboBox1.setToolTipText("");
jComboBox1.setEditable(false);
jComboBox1.setSelectedIndex(0);
jComboBox1.setSelectedItem(this);
jComboBox1.setBounds(new Rectangle(161, 32, 161, 32));
//關于5個文本域 1,2,4,5,6
jTextField1.setText("jTextField1");
jTextField1.setBounds(new Rectangle(161, 160, 161, 32));
jTextField1.setEditable(false);//不可編輯
jTextField2.setText("jTextField2");
jTextField2.setBounds(new Rectangle(161, 0, 161, 32));
jTextField4.setText("jTextField4");
jTextField4.setBounds(new Rectangle(161, 65, 161, 32));
jTextField5.setText("jTextField5");
jTextField5.setBounds(new Rectangle(161, 96, 161, 32));
jTextField6.setText("jTextField6");
jTextField6.setBounds(new Rectangle(161, 128, 161, 32));
//設置clocklabel
clocklabel1.setBounds(new Rectangle(46, 78, 72, 19));
//設置滾動面板
jScrollPane1.setBounds(new Rectangle(9, 285, 500, 200));
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jTextArea1.setText("jTextArea1");
//jTextArea1.setLineWrap(true);
/* database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:hotel2", "", "", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from hotel2", null, true, Load.ALL));
tableScrollPane1.setBounds(new Rectangle(491, 42, 491, 359));
jdbTable1.setDataSet(queryDataSet1);
jdbNavToolBar1.setBounds(new Rectangle(491, 2, 491, 39));
jdbStatusLabel1.setText("jdbStatusLabel1");
*/
jPanel1.add(jButton1, null);//Panel1放置:五個按鈕,
jPanel1.add(jButton2, null);
jPanel1.add(jButton3, null);
jPanel1.add(jButton4, null);
jPanel1.add(jButton5, null);
/* this.getContentPane().add(jdbNavToolBar1, null);
this.getContentPane().add(tableScrollPane1, null);
tableScrollPane1.getViewport().add(jdbTable1, null);
jdbNavToolBar1.add(jdbStatusLabel1, null);
*/
this.getContentPane().add(jScrollPane1, null);
jScrollPane1.getViewport().add(jTextArea1, null);
//Panel3放置Label 2~7 及Textfied 2~7:name,sex,age,idnumber,room ,in_date(charge)
jPanel3.add(jLabel2, null);
jPanel3.add(jTextField2, null);
jPanel3.add(jLabel3, null);
jPanel3.add(jComboBox1, null);
jPanel3.add(jLabel4, null);
jPanel3.add(jTextField4, null);
jPanel3.add(jLabel5, null);
jPanel3.add(jTextField5, null);
jPanel3.add(jLabel6, null);
jPanel3.add(jTextField6, null);
jPanel3.add(jLabel7, null);
jPanel3.add(jTextField1, null);
jPanel4.add(clocklabel1, null);
jPanel2.add(jPanel3, null);
jPanel2.add(jPanel4, null);
this.getContentPane().add(jPanel1, null);
this.getContentPane().add(jPanel2, null);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu4);
jMenu1.add(jMenu2);
jMenu1.add(jMenu3);
jMenu2.add(jMenuItem1);
jMenu3.add(jMenuItem2);
jMenu4.add(jMenuItem3);
setJMenuBar(jMenuBar1);
//GUI到此結束,下面是與DB的連接 -------------------------------------------------------
try{
url = "jdbc:odbc:hotel";
String user1="sa";
String password1="";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connect = DriverManager.getConnection(url,user1,password1);
jTextArea1.append("Connection Successful!");
}
catch ( ClassNotFoundException cnfex ) {
// process ClassNotFoundExceptions here
cnfex.printStackTrace();
}
catch ( SQLException sqlex ) {
// process SQLExceptions here
sqlex.printStackTrace();
}
catch ( Exception ex ) {
// process remaining Exceptions here
ex.printStackTrace();
}//三個catch可能需要改寫
setSize(new Dimension(550, 650));
setVisible(false);
}
//至此連接結束,以后是監聽器的安裝 --------------------------------------------
//添加客戶 Butten 1
void jButton1_actionPerformed(ActionEvent e) {
try {
roomwithpeople(jTextField6.getText());
//如果已有客人入住
if(roomordered)
{
JOptionPane.showMessageDialog(null, jTextField6.getText()+"房間已經有客人,請您另選一間");
jTextField6.setText("");
}
else
{
// boolean judge=false;
Statement statement = connect.createStatement();
Calendar cal=Calendar.getInstance( );
String c=cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DATE);
//if語句中的4個函數在下面有實現
if (namelegal()&&judge_age()&&judge_sex()) {
String query = "INSERT INTO hotel (" +
" gname,sex,age,idnumber,room ,in_date" +
") VALUES ('" +
jTextField2.getText() + "', '" +
jComboBox1.getSelectedItem() + "', '" +
jTextField4.getText() + "', '" +
jTextField5.getText() + "', '" +
jTextField6.getText() +"', '"+
c+"')";
jTextArea1.append("\n發送的命令:" + connect.nativeSQL(query) + "\n");
int result = statement.executeUpdate(query);
if (result == 1)
jTextArea1.append("\n插入成功\n");
//若插入失敗,則清空一切
else {
jTextArea1.append("\n插入失敗\n");
jTextField2.setText("");
jComboBox1.setSelectedIndex( -1);
jTextField4.setText("");
jTextField5.setText("");
jTextField6.setText("");
}
}
// else
// jTextArea1.append("\n必須輸入輸入姓名和房號");
statement.close();
}
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
jTextArea1.append( sqlex.toString() );
}
}
void roomwithpeople(String roomnum)//針對房號(TextField 6)
{
try{
Statement statement = connect.createStatement( );
String query = "SELECT * FROM hotel WHERE room = '"+roomnum+"'";
jTextArea1.append("\n發送的命令: " +
connect.nativeSQL(query)
+ "\n");
ResultSet rs = statement.executeQuery(query);
judge(rs);
statement.close();
}
catch(SQLException sqlex){sqlex.printStackTrace();
jTextArea1.append( sqlex.toString() );}
}
void judge( ResultSet r )
{
try {
//r.next();
//int recordNumber = r.getInt( 1 );
if (r.next())
{
roomordered=true;
}
else
roomordered=false;
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
jTextArea1.append( sqlex.toString() );
}
}
//刪除客戶 Button 2
// boolean morethanone=false;
void jButton2_actionPerformed(ActionEvent e) {
try {
//按姓名刪除,先查詢再刪除
if ( !jTextField2.getText().equals( "" ) ) {
Statement statement =connect.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String query="SELECT * FROM hotel " +
"WHERE gname = '" +
jTextField2.getText() + "'";
jTextArea1.append( "\n發送的命令: " +
connect.nativeSQL( query )
+ "\n" );
ResultSet rs = statement.executeQuery( query );
displaydetail( rs );
//jTextArea1.append( "\n查詢成功\n" );
statement.close();
}
else
jTextArea1.setText(
"請輸入要刪除的房客姓名" );
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
jTextArea1.append( sqlex.toString() );
}
}
void displaydetail( ResultSet rs ) {
try {
Calendar cal=Calendar.getInstance( );
String c=cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DATE);
boolean morerows=false;
if (rs.next() ) {
jTextField2.setText( rs.getString(1) );
jComboBox1.setSelectedItem( rs.getString( 2 ) );
jTextField4.setText( rs.getString( 3 ) );
jTextField5.setText( rs.getString( 4 ) );
jTextField6.setText( rs.getString( 5 ) );
String temp=rs.getString(6);
Countdays count =new Countdays(temp,c);
jTextField1.setText(" "+price_eachday*count.getdays());
if(rs.next()) morerows=true;
if(!morerows)
{
String command = "DELETE FROM hotel WHERE gname LIKE" +
"'" + jTextField2.getText() + "'";
delete(command);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -