商品訂購及貨物採購信息系統代碼java
登陸主界面及客服主界面git
登陸界面及登陸按鈕監視器代碼github
package hxy; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingConstants; public class LogIn { private JFrame frame; private JTextField textField; private JPasswordField passwordField; ConnectDatabase conn=null; Statement state=null; ResultSet re=null; public LogIn() { initialize(); } private void initialize() { frame = new JFrame(); frame.setVisible(true); frame.getContentPane().setLayout(null); frame.setBounds(100, 100, 529, 347); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel lblHang = new JLabel("Hang&650"); lblHang.setHorizontalAlignment(SwingConstants.CENTER); lblHang.setFont(new Font("宋體", Font.PLAIN, 18)); lblHang.setBounds(223, 30, 106, 38); frame.getContentPane().add(lblHang); JLabel lblUsername = new JLabel("UserName"); lblUsername.setHorizontalAlignment(SwingConstants.CENTER); lblUsername.setFont(new Font("宋體", Font.PLAIN, 16)); lblUsername.setBounds(72, 75, 86, 27); frame.getContentPane().add(lblUsername); textField = new JTextField(); textField.setBounds(193, 76, 183, 27); frame.getContentPane().add(textField); textField.setColumns(10); JLabel lblPassword = new JLabel("PassWord"); lblPassword.setHorizontalAlignment(SwingConstants.CENTER); lblPassword.setFont(new Font("宋體", Font.PLAIN, 16)); lblPassword.setBounds(73, 130, 85, 27); frame.getContentPane().add(lblPassword); JButton btnLogin = new JButton("Login"); //////////////////登陸按鈕監視器 passwordField = new JPasswordField(); passwordField.setFont(new Font("宋體", Font.PLAIN, 16)); passwordField.setEchoChar('*'); passwordField.setBounds(193, 130, 183, 27); frame.getContentPane().add(passwordField); passwordField.addKeyListener(new KeyListener() { @SuppressWarnings("deprecation") @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==(char)10){ if((!"".equals(textField.getText().trim()))&&(!"".equals(passwordField.getText().trim()))){/////判斷輸入的值是否爲空 conn=new ConnectDatabase(); /////////將鏈接數據庫對象實例化準備用戶名密碼驗證 String cmd=null; cmd="select password,role from staff_Infor where username='"+textField.getText().trim()+"';"; try { re=conn.state.executeQuery(cmd); if(re.next()){ if(passwordField.getText().equals(re.getString(1).trim())){ ///////////密碼驗證成功 System.out.println("Log in succeed!!!"); frame.dispose(); /////////登陸界面退出 if(re.getString(2).trim().equals("Administrator".trim())){ ////////判斷權限進入不一樣的界面,管理員 new OwnerForm(); } else if(re.getString(2).trim().equals("Buyer".trim())){//////////採購員 new BuyerForm(); } else if(re.getString(2).trim().equals("Service".trim())){ /////////////客服 new ServiceForm(textField.getText().trim()); } else{ JOptionPane.showMessageDialog(frame, "用戶權限不存在!!!\n請從新登陸"); } } else{ ///////////////////密碼驗證失敗 JOptionPane.showMessageDialog(frame, "密碼錯誤!!!\n請從新輸入"); } } else{ JOptionPane.showMessageDialog(frame, "用戶名不存在!!!\n請從新輸入"); } conn.conn.close(); } catch (SQLException e1) { e1.printStackTrace(); JOptionPane.showMessageDialog(frame, e1); } } } } }); btnLogin.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0) { if((!"".equals(textField.getText().trim()))&&(!"".equals(passwordField.getText().trim()))){/////判斷輸入的值是否爲空 try { conn=new ConnectDatabase(); /////////將鏈接數據庫對象實例化準備用戶名密碼驗證 String cmd=null; cmd="select password,role from staff_Infor where username='"+textField.getText().trim()+"';"; re=conn.state.executeQuery(cmd); if(re.next()){ if(passwordField.getText().equals(re.getString(1).trim())){ ///////////密碼驗證成功 System.out.println("Log in succeed!!!"); frame.dispose(); /////////登陸界面退出 if(re.getString(2).trim().equals("Administrator".trim())){ ////////判斷權限進入不一樣的界面,管理員 new OwnerForm(); } else if(re.getString(2).trim().equals("Buyer".trim())){//////////採購員 new BuyerForm(); } else if(re.getString(2).trim().equals("Service".trim())){ /////////////客服 new ServiceForm(textField.getText().trim()); } else{ JOptionPane.showMessageDialog(frame, "用戶權限不存在!!!\n請從新登陸"); } } else{ ///////////////////密碼驗證失敗 JOptionPane.showMessageDialog(frame, "密碼錯誤!!!\n請從新輸入"); } } else{ JOptionPane.showMessageDialog(frame, "用戶名不存在!!!\n請從新輸入"); } conn.conn.close(); } catch (Exception e) { JOptionPane.showMessageDialog(frame, "Database connection Error!!!"+e); e.printStackTrace(); } }else{ JOptionPane.showMessageDialog(frame, "請輸入用戶名和密碼!!!"); } } }); btnLogin.setBounds(193, 189, 68, 23); frame.getContentPane().add(btnLogin); JButton btnRegister = new JButton("Register"); btnRegister.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(btnRegister, "請以管理員身份進入添加信息!!!"); } }); btnRegister.setBounds(279, 189, 92, 23); frame.getContentPane().add(btnRegister); } }
客服主界面代碼sql
package hxy; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.border.BevelBorder; import javax.swing.border.SoftBevelBorder; import javax.swing.border.TitledBorder; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class ServiceForm { private JFrame frmHuang; private JTable goodsInfoTable; private JLabel lblInformationOfGoods; private JLabel lblNewLabel; private JPanel panel; private JPanel enInfoPanel; private JLabel enName; private JTextField tfEnName; private JLabel enLinkMan; private JTextField tfShortName; private JLabel enShortName; private JLabel enAdress; private JTextField tfAdress; private JLabel enPostCode; private JTextField tfPostCode; private JButton addGoodsButton; private JMenuBar menuBar; private JMenu menu; private JMenuItem maintainMenuItem; private JMenu menu_1; private JMenuItem searchMenuItem; private JTextField tfPhone; private JTextField tfEmail; private JTextField tfLinkMan; private int recoder=0; private int counter=0; ////////////商品記錄計數器 private JLabel label; private JTextField tfOrderDate; private JLabel label_1; private JTextField tfSendDate; private JLabel label_2; private JTextField tfSendAddress; private JLabel label_3; private JTextField tfSpecial; private JLabel label_4; private JLabel label_5; private JLabel label_6; private JMenu menu_2; private JMenuItem menuItem; public ServiceForm(String uername) { initialize(); } private void initialize() { frmHuang = new JFrame(); ////////////定貨主窗口的設置 frmHuang.setVisible(true); frmHuang.setTitle("Huang&650"); frmHuang.setBounds(0, 0, 985, 674); frmHuang.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmHuang.getContentPane().setLayout(null); //////////商品信息的顯示 ////////////表頭信息的設置 Object head[]={"ID","商品名稱", "包裝方式", "計量單位", "商品產地", "保質期", "特徵描述", "售價"}; Object goods[][]=new Object[35][8]; ConnectDatabase conn=new ConnectDatabase(); String cmd="select id,name,packstyle,unite,productarea,keeptime,description,price from Infor_Goods;"; try { ResultSet re=conn.state.executeQuery(cmd); if(re.isBeforeFirst()){ while(re.next()){ for(int i=0;i<7;i++){ goods[counter][i]=re.getString(i+1); } goods[counter++][7]=re.getFloat(8); } } re.close(); conn.conn.close(); } catch (SQLException e) { e.printStackTrace(); } goodsInfoTable = new JTable(goods,head); JScrollPane jsp=new JScrollPane(goodsInfoTable); frmHuang.getContentPane().add(jsp); jsp.setBounds(10, 54, 531, 545); lblInformationOfGoods = new JLabel("Information Of Goods"); /////////////商品信息標籤 lblInformationOfGoods.setFont(new Font("宋體", Font.PLAIN, 16)); lblInformationOfGoods.setHorizontalAlignment(SwingConstants.CENTER); frmHuang.getContentPane().add(lblInformationOfGoods); lblInformationOfGoods.setBounds(125, 25, 233, 30); lblNewLabel = new JLabel("\u5BA2\u6237\u9009\u8D2D"); lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel.setBounds(694, 15, 115, 15); frmHuang.getContentPane().add(lblNewLabel); panel = new JPanel(); panel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null)); frmHuang.getContentPane().add(panel); panel.setBounds(562, 40, 382, 585); panel.setLayout(null); enInfoPanel = new JPanel(); panel.add(enInfoPanel); enInfoPanel.setBounds(10, 10, 362, 208); enInfoPanel.setLayout(null); enName = new JLabel("\u4F01\u4E1A\u540D\u79F0"); enName.setBounds(10, 10, 54, 15); enInfoPanel.add(enName); tfLinkMan = new JTextField(); tfLinkMan.setBounds(74, 72, 96, 21); enInfoPanel.add(tfLinkMan); tfLinkMan.setColumns(10); tfEnName = new JTextField(); tfEnName.addFocusListener(new FocusAdapter() { @SuppressWarnings("deprecation") @Override public void focusLost(FocusEvent arg0) { ////////////////////企業名稱文本框失去焦點監視器,即查詢企業信息和聯繫人信息填入相應的控件中 String enName=tfEnName.getText().trim(); if(!"".equals(enName)){ ///////////////////查詢企業信息 ConnectDatabase conn=new ConnectDatabase(); /////////////////建立數據庫連接對象準備鏈接數據庫 String cmd="select Enterprise.id,Enterprise.company_name,Enterprise.short_name,Enterprise.address,Enterprise.postcode,Linkman.name,Linkman.telephone,Linkman.phone,Linkman.email,Enterprise.address from Linkman,Enterprise where Enterprise.company_name=Linkman.company_name and Enterprise.company_name='"+enName+"'"; try { ResultSet re=conn.state.executeQuery(cmd); if(re.next()){ re.beforeFirst(); int counter=0; Object []linkman=null; String [][]linkManInfo=null; while(re.next()){ ///////////////處理結果集判斷企業聯繫人的個數 counter++; } re.beforeFirst(); linkman=new Object[counter]; linkManInfo=new String[counter][3]; counter=0; while(re.next()){ //////////////將聯繫人存到數組中 linkman[counter]=re.getString(6); linkManInfo[counter][0]=re.getString(6); //////////聯繫人姓名 linkManInfo[counter][1]=re.getString(8); //////////電話 linkManInfo[counter++][2]=re.getString(9); //////////電子信箱 } for(int k=0;k<linkManInfo.length;k++){ System.out.println(linkManInfo[k][0]+" "+linkManInfo[k][1]+" "+linkManInfo[k][2]); } re.first(); //////////////添加企業信息到相應的控件 { tfShortName.setText(re.getString(3)); tfAdress.setText(re.getString(4)); tfPostCode.setText(re.getString(5)); /////////////生成彈出式選擇對話框中的選擇的內容 Object selectValve=JOptionPane.showInputDialog(null, "ChooseLinkMan","LinkMan",JOptionPane.INFORMATION_MESSAGE,null,linkman,linkman[0]); for(int i=0;i<linkManInfo.length;i++){ if(selectValve.toString().equals(linkManInfo[i][0].trim())){ /////////////找到選中的聯繫人並將信息填入到相應的控件中 tfLinkMan.setText(selectValve.toString()); tfPhone.setText(linkManInfo[i][1]); tfEmail.setText(linkManInfo[i][2]); break; } } } Date today=new Date(); tfOrderDate.setText((today.getYear()+1900)+"-"+(today.getMonth()+1)+"-"+today.getDate()); tfSendDate.setText((today.getYear()+1900)+"-"+(today.getMonth()+1)+"-"+(today.getDate()+1)); tfSendAddress.setText(re.getString(10)); re.close(); conn.conn.close(); } else{ JOptionPane.showMessageDialog(frmHuang, "該公司不存在!!!請檢查名稱\n若是新夥伴請先註冊!!!"); } } catch (SQLException e) { e.printStackTrace(); } } } }); tfEnName.setBounds(74, 7, 96, 21); enInfoPanel.add(tfEnName); tfEnName.setColumns(10); enLinkMan = new JLabel("\u4F01\u4E1A\u8054\u7CFB\u4EBA"); enLinkMan.setBounds(10, 75, 66, 15); enInfoPanel.add(enLinkMan); tfShortName = new JTextField(); tfShortName.setBounds(243, 7, 109, 21); enInfoPanel.add(tfShortName); tfShortName.setColumns(10); enShortName = new JLabel("\u4F01\u4E1A\u7B80\u79F0"); enShortName.setBounds(180, 10, 54, 15); enInfoPanel.add(enShortName); enAdress = new JLabel("\u4F01\u4E1A\u5730\u5740"); enAdress.setBounds(10, 41, 54, 15); enInfoPanel.add(enAdress); tfAdress = new JTextField(); tfAdress.setBounds(74, 38, 96, 21); enInfoPanel.add(tfAdress); tfAdress.setColumns(10); enPostCode = new JLabel("\u90AE\u7F16"); enPostCode.setBounds(190, 41, 54, 15); enInfoPanel.add(enPostCode); tfPostCode = new JTextField(); tfPostCode.setBounds(243, 38, 109, 21); enInfoPanel.add(tfPostCode); tfPostCode.setColumns(10); JLabel laPhone = new JLabel("\u8054\u7CFB\u7535\u8BDD"); laPhone.setBounds(180, 72, 54, 15); enInfoPanel.add(laPhone); tfEmail = new JTextField(); tfEmail.setBounds(74, 103, 145, 21); enInfoPanel.add(tfEmail); tfEmail.setColumns(10); JLabel laEmail = new JLabel("\u7535\u5B50\u4FE1\u7BB1"); laEmail.setBounds(10, 106, 54, 15); enInfoPanel.add(laEmail); tfPhone = new JTextField(); tfPhone.setBounds(243, 69, 109, 21); enInfoPanel.add(tfPhone); tfPhone.setColumns(10); label = new JLabel("\u8BA2\u5355\u65E5\u671F"); label.setBounds(10, 134, 54, 15); enInfoPanel.add(label); tfOrderDate = new JTextField(); tfOrderDate.setBounds(74, 128, 96, 21); enInfoPanel.add(tfOrderDate); tfOrderDate.setColumns(10); label_1 = new JLabel("\u9001\u8D27\u65E5\u671F"); label_1.setBounds(180, 134, 54, 15); enInfoPanel.add(label_1); tfSendDate = new JTextField(); tfSendDate.setBounds(243, 131, 109, 21); enInfoPanel.add(tfSendDate); tfSendDate.setColumns(10); label_2 = new JLabel("\u9001\u8D27\u5730\u5740"); label_2.setBounds(10, 164, 54, 15); enInfoPanel.add(label_2); tfSendAddress = new JTextField(); tfSendAddress.setBounds(74, 159, 278, 21); enInfoPanel.add(tfSendAddress); tfSendAddress.setColumns(10); label_3 = new JLabel("\u7279\u6B8A\u8BF4\u660E"); label_3.setBounds(10, 189, 54, 15); enInfoPanel.add(label_3); tfSpecial = new JTextField(); tfSpecial.setBounds(74, 186, 278, 21); enInfoPanel.add(tfSpecial); tfSpecial.setColumns(10); label_4 = new JLabel("\u5E74"); label_4.setHorizontalAlignment(SwingConstants.CENTER); label_4.setBounds(253, 109, 25, 15); enInfoPanel.add(label_4); label_5 = new JLabel("\u6708"); label_5.setHorizontalAlignment(SwingConstants.CENTER); label_5.setBounds(276, 109, 25, 15); enInfoPanel.add(label_5); label_6 = new JLabel("\u65E5"); label_6.setHorizontalAlignment(SwingConstants.CENTER); label_6.setBounds(298, 109, 25, 15); enInfoPanel.add(label_6); JPanel panel_2 = new JPanel(); panel_2.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null)); panel.add(panel_2); panel_2.setLayout(null); panel_2.setBounds(10, 253, 362, 295); Object []headGoods={"ID","商品名稱","計量單位","單價","數量"}; Object [][]goodsData=new Object[20][5]; JTable goodsTable=new JTable(goodsData,headGoods); JScrollPane scrollPane = new JScrollPane(goodsTable); scrollPane.setBounds(0, 0, 362, 285); panel_2.add(scrollPane); JLabel enOrderForm = new JLabel("\u8BA2\u5355\u8BE6\u60C5"); enOrderForm.setHorizontalAlignment(SwingConstants.CENTER); enOrderForm.setBounds(10, 228, 54, 15); panel.add(enOrderForm); addGoodsButton = new JButton("\u6DFB\u52A0\u5546\u54C1"); addGoodsButton.addActionListener(new ActionListener() { ////////////////////訂單商品添加按鈕監視器 public void actionPerformed(ActionEvent arg0) { if(goodsInfoTable.getSelectedRow()>=0&&goodsInfoTable.getSelectedRow()<counter){ /////////////選中的商品存在 if(recoder<=goodsTable.getRowCount()){ goodsTable.setValueAt(goodsInfoTable.getValueAt(goodsInfoTable.getSelectedRow(), 0), recoder, 0); /////////////選中信息欄的商品添加到訂單欄 goodsTable.setValueAt(goodsInfoTable.getValueAt(goodsInfoTable.getSelectedRow(), 1), recoder, 1); goodsTable.setValueAt(goodsInfoTable.getValueAt(goodsInfoTable.getSelectedRow(), 3), recoder, 2); goodsTable.setValueAt(goodsInfoTable.getValueAt(goodsInfoTable.getSelectedRow(), 7), recoder++, 3); } else{ /////////////當記錄數大於表格的行數時 } } else{ JOptionPane.showMessageDialog(frmHuang, "請選擇商品!!!"); } } }); addGoodsButton.setBounds(177, 228, 93, 23); panel.add(addGoodsButton); JButton submitButton = new JButton("\u63D0\u4EA4\u8BA2\u5355"); submitButton.addActionListener(new ActionListener() { ///////////提交訂單按鈕監視器 public void actionPerformed(ActionEvent arg0) { if(recoder>0){ Date orderTime=new Date(); @SuppressWarnings("deprecation") String orderID="650"+tfPhone.getText().trim()+orderTime.getYear()+(orderTime.getMonth()>10?orderTime.getMonth()+1:"0"+(orderTime.getMonth()+1))+(orderTime.getDate()>10?orderTime.getDate()+1:"0"+(orderTime.getDate()+1)); /////////生成訂單號 try { ConnectDatabase conn=new ConnectDatabase(); String validCmd="select bookMan from orderInfo where orderID='"+orderID+"'"; ResultSet re=conn.state.executeQuery(validCmd); ////////////////////判斷是否已經提交了一次訂單 if(re.next()){ JOptionPane.showMessageDialog(frmHuang, "對不起,你今天已經提交了訂單!!!\n訂單號爲:"+orderID); conn.conn.close(); re.close(); } else{ String cmd="insert into orderInfo values('"; //////////添加訂單信息表,訂單號自動生成(設置爲時間加定貨人手機號) cmd=cmd+orderID+"','"+tfOrderDate.getText().trim()+"','"+tfSendDate.getText().trim()+"','"+tfSendAddress.getText().trim()+"','"+tfSpecial.getText().trim()+"','"+tfLinkMan.getText().trim()+"')"; conn.state.executeUpdate(cmd); System.out.println("Order has been created!!!"); //conn.state.close(); ///////////////訂單詳情的生成 try{ //ConnectSQLServer conn=new ConnectSQLServer(); cmd="insert into orderGoodsInfo values('"+orderID+"','"; int count=0; while(goodsTable.getValueAt(count, 0)!=null){ //////////判斷表格中的行中有數據 cmd=cmd+goodsTable.getValueAt(count, 0)+"','"+goodsTable.getValueAt(count, 1)+"',"+Float.parseFloat(goodsTable.getValueAt(count,4).toString().trim())+","+Float.parseFloat(goodsTable.getValueAt(count, 3).toString().trim())+","+Float.parseFloat(goodsTable.getValueAt(count, 3).toString().trim())*Float.parseFloat(goodsTable.getValueAt(count, 4).toString().trim())+")"; conn.state.executeUpdate(cmd); cmd="insert into orderGoodsInfo values('"+orderID+"','"; count++; } conn.state.close(); new JOptionPane(); JOptionPane.showMessageDialog(frmHuang, "訂單提交成功!!!\n訂單號爲:"+orderID); }catch (SQLException e) { e.printStackTrace(); } } } catch (SQLException e) { e.printStackTrace(); } } else{ JOptionPane.showMessageDialog(frmHuang, "請選擇商品後再提交訂單!!!"); } } }); submitButton.setBounds(279, 552, 93, 23); panel.add(submitButton); JButton reduceGoodsButton = new JButton("\u5220\u9664\u5546\u54C1"); reduceGoodsButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //////////////////選中商品刪除按鈕監視器 if(goodsTable.getSelectedRow()>=0&&goodsTable.getSelectedRow()<recoder){ if(!"".equals(goodsTable.getValueAt(goodsTable.getSelectedRow(), 0).toString().trim())){ if(goodsTable.getSelectedRow()==(recoder-1)){ ////////////判斷刪除的信息是否爲最後一條 for(int i=0;i<goodsTable.getColumnCount();i++){ goodsTable.setValueAt("", goodsTable.getSelectedRow(), i); } recoder--; } else{ //////////////不是最後一條 for(int i=goodsTable.getSelectedRow();i<recoder-1;i++){ ///////////////數據向前移動一行 for(int j=0;j<goodsTable.getColumnCount();j++){ goodsTable.setValueAt(goodsTable.getValueAt(i+1, j), i, j); } } for(int k=0;k<goodsTable.getColumnCount();k++){ /////////////最後一行置空 goodsTable.setValueAt("", recoder-1, k); } recoder--; } } } else{ JOptionPane.showMessageDialog(frmHuang, "請選擇須要刪除的商品!!!"); } } }); reduceGoodsButton.setBounds(280, 228, 93, 23); panel.add(reduceGoodsButton); menuBar = new JMenuBar(); menuBar.setBounds(0, 0, 115, 30); frmHuang.getContentPane().add(menuBar); menu = new JMenu("\u7EF4\u62A4"); menuBar.add(menu); maintainMenuItem = new JMenuItem("\u7EF4\u62A4\u5546\u54C1"); ////////維護商品信息菜單被選中 maintainMenuItem.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent arg0) { new MaintainForm(); } }); menu.add(maintainMenuItem); menu_1 = new JMenu("\u67E5\u8BE2"); menuBar.add(menu_1); searchMenuItem = new JMenuItem("\u67E5\u8BE2\u5BA2\u6237\u4FE1\u606F"); searchMenuItem.addMouseListener(new MouseAdapter() { /////////////////查詢客戶信息菜單監視器 @Override public void mousePressed(MouseEvent arg0) { ////////////////調用一個窗體顯示客戶訂單信息(customer) //System.out.println("hhhhhhhhhhhhhhhhhhhhhhhh"); new CustomerOrderForm(); } }); menu_1.add(searchMenuItem); menu_2 = new JMenu("\u5237\u65B0"); menuBar.add(menu_2); menuItem = new JMenuItem("\u4FE1\u606F\u5237\u65B0"); menuItem.addMouseListener(new MouseAdapter() { //////////////////////頁面刷新菜單欄,將表中的數據清空,從新讀取數據庫提取商品信息 @Override public void mousePressed(MouseEvent arg0) { System.out.println(counter); int count=0; System.out.println(goodsInfoTable.getValueAt(count, 0).toString().trim()); while(count<counter){/////////////////判斷表中有數據,將數據清空 for(int i=0;i<goodsInfoTable.getColumnCount();i++){ goodsInfoTable.setValueAt("", count, i); //////////////將表格中的單元格置空 } count++; } String cmd="select id,name,packstyle,unite,productarea,keeptime,description,price from Infor_Goods;"; System.out.println(cmd); ConnectDatabase conn=new ConnectDatabase(); try { ResultSet re=conn.state.executeQuery(cmd); counter=0; while(re.next()){ for(int i=0;i<goodsInfoTable.getColumnCount();i++){ goodsInfoTable.setValueAt(re.getString(i+1), counter, i); } counter++; } re.close(); conn.state.close(); } catch (SQLException e) { e.printStackTrace(); } } }); menu_2.add(menuItem); } }
分析:數據庫
代碼整體分爲兩部分,窗體代碼和監視器代碼。其中在編寫窗體代碼的時候基本的處理思路是大框體是一個JFrame,基本上每一個類就是一個界面,而後將界面上全部的控件都定義爲私有成員,而後在每一個窗體類中都會有一個initialize方法,initialize方法中首先是對窗體的基本設置(包括窗體可見,窗體位置,大小,標題,默認關閉方式),initialize方法中的剩下部分就是對窗體中控件的初始化以及一些屬性的設置和空間的監視器部分。initialize方法是在每一個類的構造方法中調用的。數組
當頁面在加載的時候就須要顯示數據的時候,會在控件的初始化以前經過調用數據庫查詢出所須要的數據用於控件的初始化(例如:在加載客服主頁面的時候就須要在商品信息框中羅列出全部的商品的基本信息,全部在初始化商品信息展現列表前會調用數據控查詢商品基本信息表,而後將商品基本信息表的內容按相應的格式添加到窗體中的table控件中,代碼以下)jsp
//////////商品信息的顯示 ////////////表頭信息的設置 Object head[]={"ID","商品名稱", "包裝方式", "計量單位", "商品產地", "保質期", "特徵描述", "售價"}; Object goods[][]=new Object[35][8]; ConnectDatabase conn=new ConnectDatabase(); String cmd="select id,name,packstyle,unite,productarea,keeptime,description,price from Infor_Goods;"; try { ResultSet re=conn.state.executeQuery(cmd); if(re.isBeforeFirst()){ while(re.next()){ for(int i=0;i<7;i++){ goods[counter][i]=re.getString(i+1); } goods[counter++][7]=re.getFloat(8); } } re.close(); conn.conn.close(); } catch (SQLException e) { e.printStackTrace(); } goodsInfoTable = new JTable(goods,head); JScrollPane jsp=new JScrollPane(goodsInfoTable); frmHuang.getContentPane().add(jsp); jsp.setBounds(10, 54, 531, 545);
在這個MIS中基本上全部的控件的監視器完成的操做是鏈接數據庫,對數據表進行增刪改查操做,因此監視器部分基本分爲鏈接數據庫前的基礎判斷(基本判斷是否符合去查詢數據庫的須要),使用ConnectDatabase類(這個類是本身寫的一個鏈接數據庫的類,後期會有專門分析這個類的)鏈接數據庫,向數據庫發出query或者update命令,使用ResultSet接收數據庫返回的數據進行後期的處理,顯示到控件上,實例代碼以下:ide
JButton submitButton = new JButton("\u63D0\u4EA4\u8BA2\u5355"); submitButton.addActionListener(new ActionListener() { ///////////提交訂單按鈕監視器 public void actionPerformed(ActionEvent arg0) { if(recoder>0){ Date orderTime=new Date(); @SuppressWarnings("deprecation") String orderID="650"+tfPhone.getText().trim()+orderTime.getYear()+(orderTime.getMonth()>10?orderTime.getMonth()+1:"0"+(orderTime.getMonth()+1))+(orderTime.getDate()>10?orderTime.getDate()+1:"0"+(orderTime.getDate()+1)); /////////生成訂單號 try { ConnectDatabase conn=new ConnectDatabase(); String validCmd="select bookMan from orderInfo where orderID='"+orderID+"'"; ResultSet re=conn.state.executeQuery(validCmd); ////////////////////判斷是否已經提交了一次訂單 if(re.next()){ JOptionPane.showMessageDialog(frmHuang, "對不起,你今天已經提交了訂單!!!\n訂單號爲:"+orderID); conn.conn.close(); re.close(); } else{ String cmd="insert into orderInfo values('"; //////////添加訂單信息表,訂單號自動生成(設置爲時間加定貨人手機號) cmd=cmd+orderID+"','"+tfOrderDate.getText().trim()+"','"+tfSendDate.getText().trim()+"','"+tfSendAddress.getText().trim()+"','"+tfSpecial.getText().trim()+"','"+tfLinkMan.getText().trim()+"')"; conn.state.executeUpdate(cmd); System.out.println("Order has been created!!!"); //conn.state.close(); ///////////////訂單詳情的生成 try{ //ConnectSQLServer conn=new ConnectSQLServer(); cmd="insert into orderGoodsInfo values('"+orderID+"','"; int count=0; while(goodsTable.getValueAt(count, 0)!=null){ //////////判斷表格中的行中有數據 cmd=cmd+goodsTable.getValueAt(count, 0)+"','"+goodsTable.getValueAt(count, 1)+"',"+Float.parseFloat(goodsTable.getValueAt(count,4).toString().trim())+","+Float.parseFloat(goodsTable.getValueAt(count, 3).toString().trim())+","+Float.parseFloat(goodsTable.getValueAt(count, 3).toString().trim())*Float.parseFloat(goodsTable.getValueAt(count, 4).toString().trim())+")"; conn.state.executeUpdate(cmd); cmd="insert into orderGoodsInfo values('"+orderID+"','"; count++; } conn.state.close(); new JOptionPane(); JOptionPane.showMessageDialog(frmHuang, "訂單提交成功!!!\n訂單號爲:"+orderID); }catch (SQLException e) { e.printStackTrace(); } } } catch (SQLException e) { e.printStackTrace(); } } else{ JOptionPane.showMessageDialog(frmHuang, "請選擇商品後再提交訂單!!!"); } } });
上述分析是全部代碼的一個總體處理思路的分析,由於這是一個管理信息系統,因此基本上就是根據不一樣的需求對數據庫進行增刪改查操做,固然還有一些小細節會在其餘專門的文章中進行分析介紹。這個系統的總體的代碼處理的就介紹這些,其餘頁面的代碼的處理思路和這個就是大同小異了,固然不一樣的會有一些特殊處理,這個就根據實際狀況而變了。對這個系統的需求分析以及數據庫的分析在上一篇博文中就分析了,整個項目的代碼已經託管到GitHub中(https://github.com/huangxinyuan650/GoodsInformationMIS/tree/hxy),有興趣的能夠看看,歡迎你們批評指正。post