private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { try{ conn=DriverManager.getConnection(url,userName,password); st = conn.createStatement(); //經過sql語句,將文本框內的商品信息加入購物車,而且加入mlist。 String sql="insert into goods(name,price,num,color,size)" + " values('"+Text1.getText()+"',"+Double.parseDouble(Text2.getText())+","+Integer.parseInt(Text3.getText())+",'"+Text4.getText()+"','"+Text5.getText()+"')"; st.executeUpdate(sql); }catch (SQLException e){ e.printStackTrace(); } mlist.add(new Clothes(Text1.getText(),Double.parseDouble(Text2.getText()), Integer.parseInt(Text3.getText()),Text4.getText(),Text5.getText())); //統計加入了多少條商品信息,顯示到TextArea goodsNum++; addGoods(); } 撤銷添加: private void cancleButtonActionPerformed(java.awt.event.ActionEvent evt) { if(goodsNum>0){ goodsNum--; try{ conn=DriverManager.getConnection(url,userName,password); st = conn.createStatement(); String delName=mlist.get(goodsNum).getName(); String sql = "delete from goods where name = '"+delName+"' "; System.out.println(sql); st.executeUpdate(sql); }catch (SQLException e){ e.printStackTrace(); } addGoods(); } } 用戶界面: 組合框獲取數據庫中商品信息 private static void addComboxItem(){ try{ conn=DriverManager.getConnection(url,userName,password); st = conn.createStatement(); String sql="select * from goods"; rs=st.executeQuery(sql); while(rs.next()){ list.add(new Clothes(rs.getString("name"),rs.getDouble("price"),rs.getInt("num"),rs.getString("color"),rs.getString("size"))); } }catch (SQLException e){ e.printStackTrace(); } System.out.println(); for(int i=0;i<list.size();i++){ String a=list.get(i).getName(); //addItem方法加入組合框列表中 clothesComboBox.addItem(a); } 顯示商品: private void clothesComboBoxActionPerformed(java.awt.event.ActionEvent evt) { //得到你選中的下拉框的位置 int i=clothesComboBox.getSelectedIndex(); //如何i大於零,則將信息顯示購物車界面 if(i>=0) { jLabel6.setText(list.get(i).getName()); jLabel7.setText(Double.toString(list.get(i).getPrice())); jLabel8.setText(Integer.toString(list.get(i).getNum())); jLabel9.setText(list.get(i).getColor()); jLabel10.setText(list.get(i).getSize()); } } 加入購物車: private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { //創建一個GoodDao table; GoodsDao goodDao = new Jtable(jTable1); try{ count=Integer.parseInt(jTextField1.getText()); }catch(NumberFormatException e){ System.out.println(e); } int i=clothesComboBox.getSelectedIndex(); if(i>=0) { String name=list.get(i).getName(); double price=list.get(i).getPrice(); Goods good = new Goods(name,price,count); boolean flag = goodDao.save(good); if(flag){ JOptionPane.showMessageDialog(null, "添加成功"); }else{ JOptionPane.showMessageDialog(null, "添加失敗!"); } sum+=list.get(i).getPrice()*count; } }
郭雅芳:管理員界面、用戶界面html