java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)

因学校课设要求,在前段时间完成了一个Java Swing图形界面实现的外卖点餐系统,该系统连接SQL server数据库,下面详细讲述该系统:

一、数据库的字段表贴图,方便大家阅读理解该系统:

1、顾客表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第1张图片
2、派送表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第2张图片
3、派送单与顾客产生的关系表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第3张图片
4、订单细节表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第4张图片
5、评价表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第5张图片
6、原料表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第6张图片
7、菜单表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第7张图片
8、订单表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第8张图片
9、购买表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第9张图片
10、商家表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第10张图片
11、供应商表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第11张图片
12、供应表:
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第12张图片
二、外卖点餐系统结构:
1、用户登录界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第13张图片

2、用户注册界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第14张图片

3、管理员登录界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第15张图片

4、用户首页面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第16张图片

5、用户选择商家界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第17张图片

6、用户进入商家点餐界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第18张图片
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第19张图片

7、用户对订单评价界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第20张图片

8、管理中心首界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第21张图片

9、更新菜单界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第22张图片

10、供应原料管理界面
java图形界面swing 连接sql server数据库 实现外卖点餐系统(适用于课设、大作业)_第23张图片
管理中心里面的功能基本都差不多,所以就列举上面几个,下面展示主要代码。

三、部分主要代码:

1、实现登录功能:

	private static final long serialVersionUID = 1L;
	static String Cusername;
	static String Cpassword;
	String url = "jdbc:sqlserver://localhost:1433;DatabaseName=TOODS"; 
    String username = "sa"; 
    String password = "sa123";
    Connection connection = null;
    PreparedStatement ps=null;
	private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField;
    private javax.swing.JToggleButton jToggleButton1; 
    
    public shouye() {
		initComponents();
    }
    
    private void connectdb() {
    	try { 
            Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
            connection = DriverManager.getConnection(url, username, password ); 
         } 
         catch ( ClassNotFoundException cnfex ) { 
            System.err.println( 
               "装载 JDBC/ODBC 驱动程序失败。" ); 
            cnfex.printStackTrace(); 
            System.exit( 1 ); 
         } 
         catch ( SQLException sqlex ) { 
            System.err.println( "无法连接数据库" ); 
            sqlex.printStackTrace(); 
            System.exit( 1 );
         }
    }
    
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jButton3 = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBackground(new java.awt.Color(120, 150, 190));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "外卖点餐系统", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("微软雅黑", 1, 24), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel2.setAutoscrolls(true);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("账号:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("密码:");

        jTextField.setColumns(20);
        jScrollPane1.setViewportView(jTextField);

        jButton1.setText("登录");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					jButton1ActionPerformed(evt);
				} catch (SQLException e) {
					e.printStackTrace();
				}
            }
        });
        jButton2.setText("注册");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jToggleButton1.setText("退出");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("管理员登录");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jToggleButton1))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(162, 162, 162)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jButton1)
                        .addGap(85, 85, 85)
                        .addComponent(jButton2))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPasswordField1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap(171, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(36, 36, 36)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 118, Short.MAX_VALUE)
                .addComponent(jToggleButton1))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(113, 113, 113)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(290, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(441, Short.MAX_VALUE))
        );

        pack();
    }                        

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        System.exit(0);
    }                                              
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) throws SQLException {
    	Cusername=jTextField.getText();
    	Cpassword=jPasswordField1.getText(); 
        String sql="select Cno,Cpassword from Customer where Cno=? and Cpassword=? ";
        connectdb();
        ps=connection.prepareStatement(sql);
        ps.setString(1, Cusername);
        ps.setString(2, Cpassword);
        ResultSet rs=ps.executeQuery();
        if(rs.next()) {
        	shouye.this.setVisible(false);
        	menu1 Menu1=new menu1();
        	Menu1.setVisible(true);
        }
        else {
        	JOptionPane.showMessageDialog(null,"密码或用户名错误!!!!");
        } 
        connection.close();
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        shouye.this.setVisible(false);
        zhuce Zhuce=new zhuce();
        Zhuce.setVisible(true);
    }
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        shouye.this.setVisible(false);
        guanlidenglu Guanlidenglu=new guanlidenglu();
        Guanlidenglu.setVisible(true);
    } 
    
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(shouye.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(shouye.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(shouye.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(shouye.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new shouye().setVisible(true);
            }
        });
    }

2、实现更新菜单功能:

	Connection conn = null;
	Statement stmt = null;
	PreparedStatement pstm =null;
	private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;

    public gengxincaidan() {
        initComponents();
    }
    
    private void connectdb() {
    	try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=TOODS","sa", "sa123");
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    }
    
    	private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBackground(new java.awt.Color(120, 150, 190));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "外卖点餐系统", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("微软雅黑", 1, 24), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel2.setAutoscrolls(true);

        jToggleButton1.setText("退出");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jToggleButton2.setText("返回");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("修改");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton8.setText("刷新");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("删除");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("查询");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton2.setText("增加");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 699, Short.MAX_VALUE)
                        .addComponent(jToggleButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jToggleButton1))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton8)
                        .addGap(54, 54, 54))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton10)
                    .addComponent(jButton1)
                    .addComponent(jButton9)
                    .addComponent(jButton8)
                    .addComponent(jButton2))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jToggleButton1)
                    .addComponent(jToggleButton2))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(282, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(359, Short.MAX_VALUE))
        );

        pack();
    }//                         

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
		String No=JOptionPane.showInputDialog("请输入你要修改的编号:");
		String Name=JOptionPane.showInputDialog("请输入新的菜名:");
		String Dishprice=JOptionPane.showInputDialog("请输入新的价格:");
		String Dishmemo=JOptionPane.showInputDialog("请输入新的备注:");
		try {
			connectdb();
			String sql = "update Menu_Dish set Dishname='"+Name+"',Price='"+Dishprice+"',Memo='"+Dishmemo+"' where DishNo='"+No+"'";
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			JOptionPane.showMessageDialog(null, "修改成功!!!");
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				if (conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
    }  
    
    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                               
		try {
			connectdb();
			String sql = "select DishNo,Dishname,Price,Memo from Menu_Dish";
			pstm = conn.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
			}
			rs = pstm.executeQuery();
			Object[][] info = new Object[count][4];
			count = 0;
			while (rs.next()) {
				info[count][0] = rs.getString("DishNo");
				info[count][1] = rs.getString("Dishname");
				info[count][2] = rs.getString("price");
				info[count][3] = rs.getString("Memo");
				count++;
			}
			String[] title = { "编号", "名字", "价格","备注" };
			this.jTable1 = new JTable(info, title);
			this.jScrollPane1.getViewport().add(jTable1);
			conn.close();
		}catch (SQLException sqle) {
			JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
		}
    }
    
    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                               
		String No=JOptionPane.showInputDialog("请输入你要删除的编号:");
		try {
			connectdb();
			String sql = "delete  from Menu_Dish where DishNo='"+No+"';";
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			JOptionPane.showMessageDialog(null, "删除成功!!!");
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				if (conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}

		}
    }
    
    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                               
		String text=jTextField1.getText();
		try {
			connectdb();
			String sql = "select * from Menu_Dish where DishNo like '%"+text+"%' or Dishname like '%"+text+"%'";
			pstm = conn.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
			}
			rs = pstm.executeQuery();
			Object[][] info = new Object[count][4];
			count = 0;
			while (rs.next()) {
				info[count][0] = rs.getString("DishNo");
				info[count][1] = rs.getString("Dishname");
				info[count][2] = rs.getString("Price");
				info[count][3] = rs.getString("Memo");
				count++;
			}
			String[] title = { "编号", "名字", "价格","备注" };
			this.jTable1 = new JTable(info, title);
			this.jScrollPane1.getViewport().add(jTable1);
			conn.close();
		}catch (SQLException sqle) {
			JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
		}
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                               
		String No=JOptionPane.showInputDialog("请输入编号:");
		String Name=JOptionPane.showInputDialog("请输入菜名:");
		String DishPrice=JOptionPane.showInputDialog("请输入价格:");
		String DishMemo=JOptionPane.showInputDialog("请输入备注:");
		try {
			connectdb();
			String sql = "insert into Menu_Dish(DishNo,Dishname,Price,Memo)"+ "values('"+No+"','"+Name+"','"+DishPrice+"','"+DishMemo+"')";
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			JOptionPane.showMessageDialog(null, "添加成功!!!");
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				if (conn != null) {
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}

		}
} 

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                               
       gengxincaidan.this.setVisible(false);
        guanli Guanli=new guanli();
        Guanli.setVisible(true);
    }                                              

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        System.exit(0);
    }                                              

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(gengxincaidan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(gengxincaidan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(gengxincaidan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(gengxincaidan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new gengxincaidan().setVisible(true);
            }
        });
    }

3、实现点餐功能:

	int sum=0;
    public String num1;
    public int row;
    String url = "jdbc:sqlserver://localhost:1433;DatabaseName=TOODS";
	String username = "sa";
	String password = "sa123";
	Connection conn=null;
	PreparedStatement pstm=null;
	Statement stmt = null;
	private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2; 

    public Order() {
        initComponents();
    }
    
    private void connectdb() {
    	try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException cnfe) {
			JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
		} catch (SQLException sqle) {
			JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
		}
    }
                         
    	private void initComponents() {
        jPanel2 = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setBackground(new java.awt.Color(120, 150, 190));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "外卖点餐系统", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("微软雅黑", 1, 24), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanel2.setAutoscrolls(true);

        jToggleButton1.setText("退出");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jToggleButton2.setText("返回");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("查询菜品");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("查询所有");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText("下单");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.setText("确认支付");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setText("总金额");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 308, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton4)
                    .addComponent(jButton3)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jToggleButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jToggleButton1))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton4)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jToggleButton1)
                    .addComponent(jToggleButton2))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(245, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(49, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(101, 101, 101))
        );

        pack();
    }                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
		String text=jTextField1.getText();
		try {
			String sql = "select * from Menu_Dish where DishNo in(select DishNo from Menu where ShopNo='"+selectshop.shopno+"') and Dishname like '%"+text+"%'";
			connectdb();
			pstm = conn.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
			}
			rs = pstm.executeQuery();
			Object[][] info = new Object[count][4];
			count = 0;
			while (rs.next()) {
				info[count][0] = rs.getString("DishNo");
				info[count][1] = rs.getString("Dishname");
				info[count][2] = rs.getString("Price");
				info[count][3] = rs.getString("Memo");
				count++;
			}
			String[] title = { "编号", "名字", "价格","备注" };
			this.jTable1 = new JTable(info, title);
			this.jScrollPane1.getViewport().add(jTable1);
			conn.close();
		}catch (SQLException sqle) {
			JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
		}
    }  
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        row=jTable1.getSelectedRow();
        String price1=(String) jTable1.getValueAt(row, 2);
        double price=Double.parseDouble(price1);
        num1=JOptionPane.showInputDialog("您点的餐为:"+jTable1.getValueAt(row, 1)+"请输入点餐份数:");
        int num=Integer.parseInt(num1);
        sum+=num*price;
        jTextField2.setText(String.valueOf(sum));
    }
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
		try {
			connectdb();
			String sql ="select * from Menu_Dish where DishNo in(select DishNo from Menu where ShopNo='"+selectshop.shopno+"')";
			pstm = conn.prepareStatement(sql);
			ResultSet rs = pstm.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
			}
			rs = pstm.executeQuery();
			Object[][] info = new Object[count][4];
			count = 0;
			while (rs.next()) {
				info[count][0] = rs.getString("DishNo");
				info[count][1] = rs.getString("Dishname");
				info[count][2] = rs.getString("price");
				info[count][3] = rs.getString("Memo");
				count++;
			}
			String[] title = { "编号", "名字", "价格","备注" };
			this.jTable1 = new JTable(info, title);
			this.jScrollPane1.getViewport().add(jTable1);
			conn.close();
		}catch (SQLException sqle) {
			JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
		}
    }
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
    	if(sum==0) {
    		JOptionPane.showMessageDialog(null, "请选择商品后再进行支付!!!");
    		return;
    	}
		try {
			String sql = "insert into Detail(OrderNo,Acmoney,Odiscount,RealMoney,Quest,Ocontent)"+ "values('10000004','"+sum+"',0.8,'"+sum*0.8+"','正常','"+jTable1.getValueAt(row, 1)+"*"+num1+"')";
			connectdb();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			conn.close();
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, "数据源错误!!!");
			return;
		}
        JOptionPane.showMessageDialog(null, "支付成功!稍等片刻,美味即将到来!");
        Order.this.setVisible(false);
        selectshop selectshop=new selectshop();
        selectshop.setVisible(true);
    }

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        System.exit(0);
    }                                              

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                               
    	Order.this.setVisible(false);
        selectshop selectshop=new selectshop();
        selectshop.setVisible(true);
    }                                              

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Order().setVisible(true);
            }
        });
    }

因篇幅有限,只展示此三个主要模块的代码。以上三个模块的代码基本覆盖了整个系统代码的实现方法,其他模块的基本按照上述代码写法复制粘贴即可,如果有需要完整源代码的可以v我(备注来源):es_lad或者自行下载:https://download.csdn.net/download/weixin_46671666/15463638。

你可能感兴趣的:(java,swing,图形界面,java,数据库,开发语言,swing,javase)