2-Java swing jdbc DBUtils:自己設(shè)計(jì)功能的原創(chuàng)收銀管理系統(tǒng)【詩書畫唱】

目錄:本篇含Java部分的第5個(gè)Java文件

5

package shouYinXiTong;
import java.awt.Color;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class shiJianGLYGWC implements MouseListener, ActionListener {
public glyGouWuChe jieShouchuangTi = null;
public shiJianGLYGWC(glyGouWuChe chuangTi) {
this.jieShouchuangTi = chuangTi;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String huoQuDaoDeWenZi = arg0.getActionCommand();
if (huoQuDaoDeWenZi.equals("條件查詢")) {
String gouWuCheId = jieShouchuangTi.gouWuCheIdWenBenKuangTop.getText();
String spName = jieShouchuangTi
.spNameWenBenKuang.getText();
jieShouchuangTi.chuanZhi(gouWuCheId, spName);
}?
else if (huoQuDaoDeWenZi.equals("結(jié)算單件")) {
int row = jieShouchuangTi.jt1.getSelectedRow();
// int Column = jieShouchuangTi.jt1.getSelectedColumn();
String jiage=jieShouchuangTi.jt1.
getValueAt(row, 7).toString();
String num=jieShouchuangTi.jt1.
getValueAt(row, 8).toString();
Double jiesuan=Double.valueOf(jiage)*Double.valueOf(num);
jieShouchuangTi.spjieSuanWenBenKaung .
setText(String.valueOf(jiesuan).toString()+"元");
}?
else if (huoQuDaoDeWenZi.equals("查看現(xiàn)有用戶編號")) {
new glyGuKeGuanLi();
}?
else if (huoQuDaoDeWenZi.equals("查看現(xiàn)有商品類型編號")) {
new glyShangPinTypeGuanLi();
}?
else if (huoQuDaoDeWenZi.equals("查看現(xiàn)有商品存貨")) {
new glyShangPinGuanLi();
}?
else if (huoQuDaoDeWenZi.equals("增加記錄")) {
int row= jieShouchuangTi.jt1.getSelectedRow();
//String sp_num1 = jieShouchuangTi.jt1.getValueAt(row,8).toString();
String sp_id1 = jieShouchuangTi.jt1.getValueAt(row,3).toString();
String gouWuCheId = jieShouchuangTi.gouWuCheIdWenBenKuangTop.getText();
String userid = jieShouchuangTi.useridWenBenKaung.getText();
//String userName = jieShouchuangTi.yhNameWenBenKuang.getText();
String sp_id = jieShouchuangTi.spIdWenBenKuang.getText();
String sp_num = jieShouchuangTi.spNumWenBenKuang.getText();
String sql = "insert into gouwuche(yh_id,sp_id,sp_num) "?
+ "values(" + userid + "," + sp_id + ","+sp_num+")";
System.out.println(sql);
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.chuanZhi(null, null);
JOptionPane.showMessageDialog(null, "增加成功!");
String sql2 = "select *? from sp where sp_id='" + sp_id1 + "'";
System.out.println(sql2);
ResultSet res=DBUtils.Select(sql2);
try {
while(res.next()){
int sp_cunhuo=res.getInt("spCunHuoShuLiang");
int sp_cunhuoNew=sp_cunhuo-Integer.valueOf(sp_num);
String sql1 = "update sp set spCunHuoShuLiang='" + sp_cunhuoNew
+ "' where sp_id=" + sp_id1+"";
System.out.println(sql1);
DBUtils.ZSG(sql1);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "增加失敗,增加的商品編號不可相同!");
}
} else if (huoQuDaoDeWenZi.equals("修改記錄")) {
String gouWuCheId = jieShouchuangTi.gouWuCheIdWenBenKuang.getText();
String userid = jieShouchuangTi.useridWenBenKaung.getText();
String sp_id = jieShouchuangTi.spIdWenBenKuang.getText();
String sp_numNew = jieShouchuangTi.spNumWenBenKuang.getText();
int row= jieShouchuangTi.jt1.getSelectedRow();
String sp_numOld = jieShouchuangTi.jt1.getValueAt(row,8).toString();
int? sp_numCha=Integer.valueOf(sp_numOld)-Integer.valueOf(sp_numNew);
//本來時(shí)要判斷sp_numCha是否為0,商品編號,商品名要唯一,不然商品排行榜會(huì)有問題,
//但這里自己添加時(shí)注意下即可
//并且修改后,商品儲(chǔ)存數(shù)量等也會(huì)變,和借閱管理邏輯一樣,
//這里的商品數(shù)量差(sp_numCha)>0時(shí),就原來的儲(chǔ)存量-sp_numCha,
//相當(dāng)于刪除了一些記錄,儲(chǔ)存量會(huì)變多
//因?yàn)閷?shí)際中直接會(huì)有修改信息,但不要求商品儲(chǔ)存數(shù)量變,因?yàn)榭赡?/p>
//但沒必要,這里重要的是會(huì)做,以后根據(jù)需求再設(shè)計(jì)更高級等? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
String sql = "update gouwuche set sp_id='" + sp_id +?
"',sp_num='" + sp_numNew?
+ "' where gwc_id='" + gouWuCheId+"'";
System.out.println(sql);
String sp_id1 = jieShouchuangTi.jt1.getValueAt(row,3).toString();
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.chuanZhi(null, null);
JOptionPane.showMessageDialog(null, "修改成功!");
String sqlSelect = "select *? from sp where sp_id='" + sp_id1 + "'";
System.out.println(sqlSelect);
ResultSet res=DBUtils.Select(sqlSelect);
try {
while(res.next()){
int sp_cunhuo=res.getInt("spCunHuoShuLiang");
// int sp_cunhuoNew=sp_cunhuo-Integer.valueOf(sp_numNew);
String sqlUpdate = "update sp set spCunHuoShuLiang='" + (sp_cunhuo+sp_numCha)
+ "' where sp_id=" + sp_id1+"";
System.out.println(sqlUpdate);
DBUtils.ZSG(sqlUpdate);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "修改失敗!修改的商品名不可相同!");
}
} else if (huoQuDaoDeWenZi.equals("清空")) {
jieShouchuangTi.gouWuCheIdWenBenKuangTop.setText("");
jieShouchuangTi.spNameWenBenKuang.setText("");
jieShouchuangTi.gouWuCheIdWenBenKuang.setText("");
jieShouchuangTi.useridWenBenKaung.setText("");
jieShouchuangTi.yhNameWenBenKuang.setText("");
jieShouchuangTi.spNameWenBenKuang2.setText("");
jieShouchuangTi.spDanJiaWenBenKuang.setText("");
jieShouchuangTi.spIdWenBenKuang.setText("");
jieShouchuangTi.spNumWenBenKuang.setText("");
jieShouchuangTi.spjieSuanWenBenKaung.setText("");
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
int row = jieShouchuangTi.jt1.getSelectedRow();
jieShouchuangTi.spNameWenBenKuang2.
setText(jieShouchuangTi.jt1.getValueAt(row, 4)
.toString());
jieShouchuangTi.gouWuCheIdWenBenKuang.setText(jieShouchuangTi.jt1.
getValueAt(row, 0).toString());
jieShouchuangTi.useridWenBenKaung.
setText(jieShouchuangTi.jt1.getValueAt(row, 1).toString());
jieShouchuangTi.spIdWenBenKuang.
setText(jieShouchuangTi.jt1.getValueAt(row, 3).toString());
jieShouchuangTi.spNumWenBenKuang.
setText(jieShouchuangTi.jt1.getValueAt(row, 4)
.toString());
jieShouchuangTi.yhNameWenBenKuang.
setText(jieShouchuangTi.jt1.getValueAt(row, 2).toString());
jieShouchuangTi.spNumWenBenKuang.
setText(jieShouchuangTi.jt1.getValueAt(row, 8).toString());
jieShouchuangTi.spDanJiaWenBenKuang.
setText(jieShouchuangTi.jt1.getValueAt(row,7).toString()+"元");
} else if (arg0.isMetaDown()) {
int num = JOptionPane.showConfirmDialog(null, "確認(rèn)刪除嗎?");
if (num == 0) {
int row = jieShouchuangTi.jt1.getSelectedRow();
String gwc_id = jieShouchuangTi.jt1.getValueAt(row,0).toString();
String sql = "delete gouwuche where gwc_id='" + gwc_id + "'";
String sp_num = jieShouchuangTi.jt1.getValueAt(row,8).toString();
String sp_id = jieShouchuangTi.jt1.getValueAt(row,3).toString();
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.chuanZhi(null, null);
JOptionPane.showMessageDialog(null, "刪除成功!");
String sqlSelect = "select *? from sp where sp_id='" + sp_id + "'";
System.out.println(sqlSelect);
ResultSet res=DBUtils.Select(sqlSelect);
try {
while(res.next()){
int sp_cunhuo=res.getInt("spCunHuoShuLiang");
int sp_cunhuoNew=sp_cunhuo+Integer.valueOf(sp_num);
String sqlUpdate = "update sp set spCunHuoShuLiang='" + sp_cunhuoNew
+ "' where sp_id=" + sp_id+"";
System.out.println(sqlUpdate);
DBUtils.ZSG(sqlUpdate);
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "刪除失敗!");
}
}
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
public class glyGouWuChe extends JFrame {
public static ArrayList<Integer> jieHe = null;
public static JButton SelectAnNiu, insertAnNiu, updateAnNiu,
qingKongAnNiu ,jieSuan,yhIdAnNiu,spIdAnNiu,cunHuoAnNiu= null;
public static DefaultTableModel dtm = null;
public static JPanel jp1, jp2, jp3 = null;
public static JTable jt1 = null;
public static JLabel gouWuCheIdWenZi1,
yhIdWenZi, gouWuCheIdWenZi2,jieSuanAllWenZi,
spNameWenZi,yhNameWenZi,
userIdWenZi,jieSuanDanJianWenZi,spIdWenZi,
guKeBuyNumWenZi,spDanJiaWenZi= null;
public static JTextField gouWuCheIdWenBenKuangTop,?
spNameWenBenKuang ,spjieSuanWenBenKaung,spIdWenBenKuang= null;
public static JTextField gouWuCheIdWenBenKuang,?
useridWenBenKaung, yhNameWenBenKuang,spNumWenBenKuang,
spDanJiaWenBenKuang,jieSuanAllWenZiKuang,
spNameWenBenKuang2 = null;
public glyGouWuChe() {
this.setTitle("管理員購物車和結(jié)算");
this.setLayout(null);
this.setSize(1000, 800);
this.setLocationRelativeTo(null);
jp2 = new JPanel();
jp2.setBorder(BorderFactory.createLineBorder(Color.gray));
jp2.setLayout(null);
jp2.setBounds(670, 10, 300, 270);
gouWuCheIdWenZi1 = new JLabel("購物車編號:");
yhIdWenZi= new JLabel("商品名稱:");
gouWuCheIdWenZi1.setBounds(10, 10, 70, 30);
yhIdWenZi.setBounds(10, 50, 70, 30);
gouWuCheIdWenBenKuangTop = new JTextField();
spNameWenBenKuang = new JTextField();
gouWuCheIdWenBenKuangTop.setBounds(80, 10, 100, 30);
spNameWenBenKuang.setBounds(80, 50, 100, 30);
SelectAnNiu = new JButton("條件查詢");
SelectAnNiu.setBounds(80, 90, 100, 30);
jp2.add(SelectAnNiu);
yhIdAnNiu = new JButton("查看現(xiàn)有用戶編號");
yhIdAnNiu .setBounds(80, 150, 200, 30);
yhIdAnNiu.addActionListener(new shiJianGLYGWC(this));
jp2.add(yhIdAnNiu );
spIdAnNiu = new JButton("查看現(xiàn)有商品類型編號");
spIdAnNiu.setBounds(80, 190, 200, 30);
spIdAnNiu.addActionListener(new shiJianGLYGWC(this));
jp2.add(spIdAnNiu);
cunHuoAnNiu = new JButton("查看現(xiàn)有商品存貨");
cunHuoAnNiu.setBounds(80, 230, 200, 30);
cunHuoAnNiu.addActionListener(new shiJianGLYGWC(this));
jp2.add(cunHuoAnNiu);
jp2.add(gouWuCheIdWenZi1);
jp2.add(yhIdWenZi);
jp2.add(gouWuCheIdWenBenKuangTop);
jp2.add(spNameWenBenKuang);
jp3 = new JPanel();
jp3.setBorder(BorderFactory.createLineBorder(Color.gray));
jp3.setLayout(null);
jp3.setBounds(670, 280, 300, 620);
gouWuCheIdWenZi1 = new JLabel("購物車編號:");
yhIdWenZi = new JLabel("用戶編號:");
yhNameWenZi = new JLabel("用戶名稱:");
spNameWenZi = new JLabel("商品名稱:");
jieSuanDanJianWenZi = new JLabel("結(jié)算單件:");
jieSuanDanJianWenZi.setBounds(10, 370, 70, 30);
jp3.add(jieSuanDanJianWenZi);
spIdWenZi = new JLabel("商品編號:");
spIdWenZi .setBounds(10, 210, 70, 30);
jp3.add(spIdWenZi);
guKeBuyNumWenZi = new JLabel("購買數(shù)量:");
guKeBuyNumWenZi.setBounds(10, 250, 70, 30);
jp3 .add(guKeBuyNumWenZi);
gouWuCheIdWenZi1.setBounds(10, 10, 70, 30);
yhIdWenZi.setBounds(10, 50, 70, 30);
yhNameWenZi.setBounds(10, 90, 70, 30);
spNameWenZi.setBounds(10, 130, 70, 30);
spDanJiaWenZi = new JLabel("商品單價(jià):");
spDanJiaWenZi.setBounds(10, 170, 70, 30);
jp3.add(spDanJiaWenZi);
spDanJiaWenBenKuang = new JTextField();
spDanJiaWenBenKuang.setEditable(false);
spDanJiaWenBenKuang.setForeground(Color.white);
spDanJiaWenBenKuang.setBackground(Color.black);
spDanJiaWenBenKuang.setBounds(80, 170, 100, 30);
jp3.add(spDanJiaWenBenKuang);
jp3.add(gouWuCheIdWenZi1);
jp3.add(yhIdWenZi);
jp3.add(spNameWenZi);
jp3.add(yhNameWenZi);
gouWuCheIdWenBenKuang = new JTextField();
gouWuCheIdWenBenKuang.setForeground(Color.white);
gouWuCheIdWenBenKuang.setBackground(Color.black);
useridWenBenKaung = new JTextField();
//useridWenBenKaung.setEditable(false);
//useridWenBenKaung.setForeground(Color.white);
//useridWenBenKaung.setBackground(Color.black);
yhNameWenBenKuang = new JTextField();
spNameWenBenKuang2 = new JTextField();
spNameWenBenKuang2.setEditable(false);
spNameWenBenKuang2.setForeground(Color.white);
spNameWenBenKuang2.setBackground(Color.black);
spNumWenBenKuang = new JTextField();
spIdWenBenKuang= new JTextField();
yhNameWenBenKuang.setEditable(false);
yhNameWenBenKuang.setForeground(Color.white);
yhNameWenBenKuang.setBackground(Color.black);
gouWuCheIdWenBenKuang.setBounds(80, 10, 100, 30);
useridWenBenKaung.setBounds(80, 50, 100, 30);
yhNameWenBenKuang.setBounds(80, 90, 100, 30);
spNameWenBenKuang2.setBounds(80, 130, 100, 30);
spIdWenBenKuang.setBounds(80, 10, 170, 30);
spNumWenBenKuang.setBounds(80, 50, 210, 30);
spjieSuanWenBenKaung = new JTextField();
spjieSuanWenBenKaung.setEditable(false);
spjieSuanWenBenKaung.setBounds(80, 370, 100, 30);
gouWuCheIdWenBenKuang.setEditable(false);
jp3.add(spjieSuanWenBenKaung);
spIdWenBenKuang = new JTextField();
spIdWenBenKuang.setBounds(80, 210, 100, 30);
jp3.add(spIdWenBenKuang);
spNumWenBenKuang = new JTextField();
spNumWenBenKuang.setBounds(80, 250, 100, 30);
jp3.add(spNumWenBenKuang);
insertAnNiu = new JButton("增加記錄");
insertAnNiu.setBounds(10, 290, 80, 30);
updateAnNiu = new JButton("修改記錄");
updateAnNiu.setBounds(95, 290, 80, 30);
qingKongAnNiu = new JButton("清空");
qingKongAnNiu.setBounds(10, 330, 80, 30);
jieSuan = new JButton("結(jié)算單件");
jieSuan.setBounds(95, 330, 80, 30);
jieSuan.addActionListener(new shiJianGLYGWC(this));
jp3.add(jieSuan);
jieSuanAllWenZi= new JLabel("結(jié)算總金額:");
jieSuanAllWenZi.setBounds(10, 420, 80, 30);
jp3.add(jieSuanAllWenZi);
jieSuanAllWenZiKuang= new JTextField();
jieSuanAllWenZiKuang.setBounds(85, 420, 80, 30);
jp3.add(jieSuanAllWenZiKuang);
SelectAnNiu.addActionListener(new shiJianGLYGWC(this));
insertAnNiu.addActionListener(new shiJianGLYGWC(this));
updateAnNiu.addActionListener(new shiJianGLYGWC(this));
qingKongAnNiu.addActionListener(new shiJianGLYGWC(this));
jp3.add(gouWuCheIdWenBenKuang);
jp3.add(useridWenBenKaung);
jp3.add(yhNameWenBenKuang);
jp3.add(spNameWenBenKuang2);
jp3.add(insertAnNiu);
jp3.add(updateAnNiu);
jp3.add(qingKongAnNiu);
this.add(jp2);
this.add(jp3);
this.setVisible(true);
chuanZhi(null, null);
}
public void chuanZhi(String gwc_id, String sp_name) {
if (jp1 != null) {
this.remove(jp1);
}
String sql = "select * from sp s inner "
+ "join gouwuche g on s.sp_id=g.sp_id "
+ "inner join yonghu y on y.yh_id=g.yh_id "
+ "inner join sp_type spt on "
+ "spt.sp_typeid=s.sp_typeid where 1=1";
System.out.println(sql);
if (gwc_id != null && gwc_id.length() > 0) {
sql += " and gwc_id='" + gwc_id + "'";
}
if (sp_name != null && sp_name.length() > 0) {
sql += " and sp_name like'%" + sp_name + "%'";
}
Vector<Object> v_head = new Vector<Object>();
v_head.add("購物車編號");
v_head.add("用戶編號");
v_head.add("用戶姓名");
v_head.add("商品編號");
v_head.add("商品名稱");
v_head.add("商品介紹");
v_head.add("商品類型");
v_head.add("商品價(jià)格");
v_head.add("購買數(shù)量");
Vector<Vector<Object>> v_body = new Vector<Vector<Object>>();
ResultSet res = DBUtils.Select(sql);
Double jieSuanAll=(double) 0;
try {
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getInt("gwc_id"));
v.add(res.getInt("yh_id"));
v.add(res.getString("yh_uname"));
v.add(res.getInt("sp_id"));
v.add(res.getString("sp_name"));
v.add(res.getString("sp_jieshao"));
v.add(res.getString("sp_typename"));
v.add(res.getDouble("sp_price"));
//+"元"
v.add(res.getInt("sp_num"));
//+"件"
//v_body.add(v);不要多寫了一遍,不然表格會(huì)出現(xiàn)重復(fù)內(nèi)容。
Double sp_price=res.getDouble("sp_price");
int sp_num=res.getInt("sp_num");
v_body.add(v);
jieSuanAll=jieSuanAll+sp_price*sp_num;
jieSuanAllWenZiKuang.
setText(jieSuanAll+"元");
}
} catch (SQLException e) {
e.printStackTrace();
}
dtm = new DefaultTableModel(v_body, v_head) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
jt1 = new JTable(dtm);
jt1.addMouseListener(new shiJianGLYGWC(this));
jt1.getTableHeader().setReorderingAllowed(false);
jt1.getTableHeader().setResizingAllowed(false);
jt1.setBackground(Color.yellow);
?
jt1.setForeground(Color.blue);
JScrollPane jsp = new JScrollPane(jt1);
jsp.setBounds(0, 0, 650, 500);
jp1 = new JPanel();
jp2.setBackground(Color.blue);
//jp2.setForeground(Color.red);
jp3.setBackground(Color.red);
//jp3.setForeground(Color.blue);
//jp1.setBackground(Color.black);
jp1.setLayout(null);
jp1.setBounds(10, 10, 650, 500);
jp1.add(jsp);
this.add(jp1);
}
}





