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

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

6

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 shiJianGLYGK implements MouseListener, ActionListener {
public glyGuKeGuanLi jieShouchuangTi = null;
public shiJianGLYGK(glyGuKeGuanLi chuangTi) {
this.jieShouchuangTi = chuangTi;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String huoQuDaoDeWenZi = arg0.getActionCommand();
if (huoQuDaoDeWenZi.equals("查詢")) {
String pid=jieShouchuangTi.txt_pid.getText();
String pName=jieShouchuangTi.txt_pname.getText();
jieShouchuangTi.tushu_load(pid, pName);
} else if (huoQuDaoDeWenZi.equals("增加顧客")) {
String trueName = jieShouchuangTi.TruenameWenZiKuang.getText();
String pName = jieShouchuangTi.txt_pName.getText();
int pTypeNameId = jieShouchuangTi.arr_type.get
(jieShouchuangTi.com_type.getSelectedIndex());
String sql = "select * from yh_type where yh_typeid="+pTypeNameId;
ResultSet res_type = DBUtils.Select(sql);
//arr_type = new ArrayList<Integer>();
try {
while (res_type.next()) {
String yhTypeName=res_type.getString("yh_typename");
String pSex = "男";
if (jieShouchuangTi.rb2.isSelected()) {
pSex= "女";
}
String sqlinsert = "insert into yonghu(yh_uname,yh_type,"
+ "yh_sex,yh_name) " + "values('" + pName + "','"
+ yhTypeName + "','" + pSex + "','"+trueName+"')";
System.out.println(sql);
if (DBUtils.ZSG(sqlinsert)) {
jieShouchuangTi.tushu_load(null, null);
JOptionPane.showMessageDialog(null, "顧客增加成功!");
} else {
JOptionPane.showMessageDialog(null, "增加失敗,用戶名不可重名!");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
} else if (huoQuDaoDeWenZi.equals("修改顧客")) {
String pid = jieShouchuangTi.txt_Pid.getText();
String pName = jieShouchuangTi.txt_pName.getText();
String trueName = jieShouchuangTi.TruenameWenZiKuang.getText();
int pTypeNameId = jieShouchuangTi.arr_type
.get(jieShouchuangTi.com_type.getSelectedIndex());
String sql = "select *? from yh_type where yh_typeid="+pTypeNameId;
ResultSet res_type = DBUtils.Select(sql);
//arr_type = new ArrayList<Integer>();
try {
while (res_type.next()) {
String yhTypeName=res_type.getString("yh_typename");
String pSex = "男";
if (jieShouchuangTi.rb2.isSelected()) {
pSex= "女";
}
String sqlupdate = "update yonghu set? yh_uname='" + pName
+ "',yh_type='" + yhTypeName
+ "',yh_sex='" + pSex + "' ,yh_name='"+trueName+"'where yh_id=" + pid
;
if (DBUtils.ZSG(sqlupdate)) {
jieShouchuangTi.tushu_load(null, null);
JOptionPane.showMessageDialog(null, "修改成功!");
} else {
JOptionPane.showMessageDialog(null, "修改失敗,請重試");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
} else if (huoQuDaoDeWenZi.equals("清空")) {
jieShouchuangTi.txt_Pid.setText("");
jieShouchuangTi.txt_pName.setText("");
String pSex = "男";
if (jieShouchuangTi.rb2.isSelected()) {
pSex= "女";
}
jieShouchuangTi.com_type.setSelectedIndex(1);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
int row = jieShouchuangTi.jt1.getSelectedRow();
jieShouchuangTi.txt_Pid.setText(jieShouchuangTi.
jt1.getValueAt(row, 0).toString());
jieShouchuangTi.TruenameWenZiKuang.setText(jieShouchuangTi.
jt1.getValueAt(row, 4).toString());
jieShouchuangTi.txt_pName.setText(jieShouchuangTi.
jt1.getValueAt(row, 1).toString());
if (jieShouchuangTi.jt1.getValueAt(row, 2).equals("男")) {
jieShouchuangTi.rb1.setSelected(true);
}
else if(jieShouchuangTi.jt1.getValueAt(row, 2).equals("女")){
jieShouchuangTi.rb2.setSelected(true);
}
String type = jieShouchuangTi.jt1.getValueAt(row, 3).toString();
jieShouchuangTi.com_type.setSelectedItem(type);
} else if (arg0.isMetaDown()) {
int num = JOptionPane.showConfirmDialog(null, "確定要?jiǎng)h除這條信息嗎?");
if (num == 0) {
int row = jieShouchuangTi.jt1.getSelectedRow();
String pId = jieShouchuangTi.jt1.getValueAt(row, 0).toString();
String sql = "delete yonghu where yh_id='" + pId + "'";
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.tushu_load(null, null);
JOptionPane.showMessageDialog(null, "顧客刪除成功!");
} else {
JOptionPane.showMessageDialog(null, "刪除失敗,請重試");
}
}
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}
public class glyGuKeGuanLi extends JFrame {
public static ArrayList<Integer> arr_type = null;
public static JButton btn_select, btn_insert, btn_update,
btn_qingkong = null;
public static JComboBox com_type = null;
public static DefaultTableModel dtm = null;
public static JPanel jp1, jp2, jp3 = null;
public static JTable jt1 = null;
public static JLabel lbPid, lbPname,TruenameWenZi = null;
public static JLabel lbPId, lbPName,? lb_Ptypename,
lb_pSex = null;
public static JTextField txt_pid, txt_pname,TruenameWenZiKuang = null;
public static JTextField txt_Pid, txt_pName
= null;
public static JRadioButton rb1,rb2;?
public glyGuKeGuanLi() {
this.setTitle("顧客管理");
this.setLayout(null);
this.setSize(710, 550);
this.setLocationRelativeTo(null);
jp2 = new JPanel();
jp2.setBorder(BorderFactory.createLineBorder(Color.gray));
jp2.setLayout(null);
jp2.setBounds(470, 10, 200, 150);
lbPid = new JLabel("顧客編號(hào):");
lbPid.setBounds(10, 10, 70, 30);
jp2.add(lbPid);
lbPname = new JLabel("顧客用戶名:");
lbPname.setBounds(10, 50, 70, 30);
jp2.add(lbPname);
txt_pid = new JTextField();
txt_pname = new JTextField();
txt_pid.setBounds(85, 10, 100, 30);
txt_pname.setBounds(85, 50, 100, 30);
btn_select = new JButton("查詢");
btn_select.setBounds(30, 90, 100, 30);
jp2.add(txt_pid);
jp2.add(txt_pname);
jp2.add(btn_select);
jp3 = new JPanel();
jp3.setBorder(BorderFactory.createLineBorder(Color.gray));
jp3.setLayout(null);
jp3.setBounds(470, 170, 200, 300);
lbPId = new JLabel("顧客編號(hào):");
lbPName = new JLabel("顧客用戶名:");
TruenameWenZi = new JLabel("真實(shí)姓名:");
TruenameWenZi.setBounds(10, 90, 70, 30);
jp3.add(TruenameWenZi);
TruenameWenZiKuang = new JTextField();
TruenameWenZiKuang.setBounds(80, 90, 70, 30);
jp3.add(TruenameWenZiKuang);
lb_Ptypename = new JLabel("顧客類型:");
lb_pSex = new JLabel("顧客性別:");
lbPId.setBounds(10, 10, 70, 30);
lbPName.setBounds(10, 50, 70, 30);
lb_Ptypename.setBounds(10, 130, 70, 30);
lb_pSex.setBounds(10, 170, 70, 30);
jp3.add(lbPId);
jp3.add(lbPName);
jp3.add(lb_Ptypename);
jp3.add(lb_pSex);
txt_Pid = new JTextField();
txt_Pid.setEditable(false);
txt_pName = new JTextField();
rb1 = new JRadioButton("男", true);
rb2 = new JRadioButton("女");
jp3.add(rb1);
jp3.add(rb2);
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
rb1.setBounds(80, 170, 50, 30);
rb2.setBounds(140, 170, 50, 30);
txt_Pid.setBounds(80, 10, 100, 30);
txt_pName.setBounds(80, 50, 100, 30);
com_type = new JComboBox();
String sql = "select * from yh_type";
ResultSet res_type = DBUtils.Select(sql);
arr_type = new ArrayList<Integer>();
try {
while (res_type.next()) {
com_type.addItem(res_type.getObject(2));
arr_type.add(res_type.getInt(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
com_type.setBounds(80, 130, 100, 30);
btn_insert = new JButton("增加顧客");
btn_insert.setBounds(10, 210, 80, 30);
btn_update = new JButton("修改顧客");
btn_update.setBounds(95, 210, 80, 30);
btn_qingkong = new JButton("清空");
btn_qingkong.setBounds(10, 250, 160, 30);
btn_select.addActionListener(new shiJianGLYGK(this));
btn_insert.addActionListener(new shiJianGLYGK(this));
btn_update.addActionListener(new shiJianGLYGK(this));
btn_qingkong.addActionListener(new shiJianGLYGK(this));
jp3.add(txt_Pid);
jp3.add(txt_pName);
jp3.add(com_type);
jp3.add(btn_insert);
jp3.add(btn_update);
jp3.add(btn_qingkong);
this.add(jp2);
this.add(jp3);
this.setVisible(true);
tushu_load(null, null);
}
public void tushu_load(String tid, String tname) {
if (jp1 != null) {
this.remove(jp1);
}
String sql = "select * from yonghu? where 1=1";
if (tid != null && tid.length() > 0) {
sql += " and yh_id='" + tid + "'";
}
if (tname != null && tname.length() > 0) {
sql += " and yh_name like'%" + tname + "%'";
}
Vector<Object> v_head = new Vector<Object>();
v_head.add("顧客編號(hào)");
v_head.add("顧客用戶名");
v_head.add("顧客性別");
v_head.add("顧客類型");
v_head.add("顧客真實(shí)姓名");
Vector<Vector<Object>> v_body = new?
Vector<Vector<Object>>();
ResultSet res = DBUtils.Select(sql);
try {
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getInt("yh_id"));
v.add(res.getString("yh_uname"));
v.add(res.getString("yh_sex"));
v.add(res.getString("yh_type"));
v.add(res.getString("yh_name"));
v_body.add(v);
}
} 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 shiJianGLYGK(this));
JScrollPane jsp = new JScrollPane(jt1);
jsp.setBounds(0, 0, 450, 500);
jp1 = new JPanel();
jp1.setLayout(null);
jp1.setBounds(10, 10, 450, 500);
jp1.add(jsp);
this.add(jp1);
}
}



7

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 shiJianGly implements MouseListener, ActionListener {
public glyShangPinGuanLi jieShouchuangTi = null;
public shiJianGly(glyShangPinGuanLi chuangTi) {
this.jieShouchuangTi = chuangTi;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// huoQuDaoDeWenZi:獲取到的文字
String huoQuDaoDeWenZi = arg0.getActionCommand();
if (huoQuDaoDeWenZi.equals("條件查詢")) {
String spId = jieShouchuangTi.txt_spId.getText();
String uname = jieShouchuangTi.txt_spName.getText();
jieShouchuangTi.chuanZhiFangFa(spId, uname);
} else if (huoQuDaoDeWenZi.equals("增加商品")) {
String sp_name = jieShouchuangTi.txt_spName1.getText();
String spCunHuoShuLiang = jieShouchuangTi.txt_spNum.getText();
int typeid = jieShouchuangTi.jieHe.
get(jieShouchuangTi.xiaLaKuang.getSelectedIndex());
String sp_jieshao = jieShouchuangTi.txt_jieShao.getText();
String sql = "insert into sp(sp_name,spCunHuoShuLiang,sp_typeid,"
+ "sp_jieshao) " + "values('" + sp_name + "'," + spCunHuoShuLiang + ","
+ typeid + ",'" + sp_jieshao + "')";
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.chuanZhiFangFa(null, null);
JOptionPane.showMessageDialog(null, "商品增加成功!");
} else {
JOptionPane.showMessageDialog(null, "增加失敗,商品名不可重復(fù)!");
}
} else if (huoQuDaoDeWenZi.equals("修改商品")) {
String spId = jieShouchuangTi.txt_spid1.getText();
String spName = jieShouchuangTi.txt_spName1.getText();
String spCunHuoShuLiang = jieShouchuangTi.txt_spNum.getText();
int sp_typeid = jieShouchuangTi.jieHe
.get(jieShouchuangTi.xiaLaKuang.getSelectedIndex());
String sp_jieshao = jieShouchuangTi.txt_jieShao.getText();
String sql = "update sp set? sp_name='" + spName
+ "',spCunHuoShuLiang='" + spCunHuoShuLiang + "',sp_typeid=" + sp_typeid
+ ",sp_jieshao='" + sp_jieshao + "' where sp_id='" + spId
+ "'";
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.chuanZhiFangFa(null, null);
JOptionPane.showMessageDialog(null, "修改成功!");
} else {
JOptionPane.showMessageDialog(null, "修改失敗,請雙擊要選中的那一行,重試");
}
} else if (huoQuDaoDeWenZi.equals("清空")) {
jieShouchuangTi.txt_spid1.setText("");
jieShouchuangTi.txt_spName1.setText("");
jieShouchuangTi.txt_spNum.setText("");
jieShouchuangTi.txt_jieShao.setText("");
jieShouchuangTi.xiaLaKuang.setSelectedIndex(1);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
int row = jieShouchuangTi.jt1.getSelectedRow();
jieShouchuangTi.txt_spid1.setText(jieShouchuangTi.jt1.getValueAt(row, 0).toString());
jieShouchuangTi.txt_spName1.setText(jieShouchuangTi.jt1.getValueAt(row, 1).toString());
jieShouchuangTi.txt_spNum.setText(jieShouchuangTi.jt1.getValueAt(row, 2).toString());
jieShouchuangTi.txt_jieShao.setText(jieShouchuangTi.jt1.getValueAt(row, 4)
.toString());
String type = jieShouchuangTi.jt1.getValueAt(row, 3).toString();
jieShouchuangTi.xiaLaKuang.setSelectedItem(type);
} else if (arg0.isMetaDown()) {
int num = JOptionPane.showConfirmDialog(null, "確定要?jiǎng)h除這條信息嗎?");
if (num == 0) {
int row = jieShouchuangTi.jt1.getSelectedRow();
String spId = jieShouchuangTi.jt1.getValueAt(row, 0).toString();
String sql = "delete sp where sp_id='" + spId + "'";
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.chuanZhiFangFa(null, null);
JOptionPane.showMessageDialog(null, "商品刪除成功!");
} 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 glyShangPinGuanLi extends JFrame {
public static ArrayList<Integer> jieHe = null;
public static JButton btn_select, btn_insert, btn_update,
btn_qingkong = null;
public static JComboBox xiaLaKuang = null;
public static DefaultTableModel dtm = null;
public static JPanel jp1, jp2, jp3 = null;
public static JTable jt1 = null;
public static JLabel lb_tid, lb_tname = null;
public static JLabel lb_spId1, lb_spName1, lb_spNum, lb_typename,
lb_spJieShao = null;
public static JTextField txt_spId, txt_spName = null;
public static JTextField txt_spid1, txt_spName1, txt_spNum,
txt_jieShao = null;
public glyShangPinGuanLi() {
this.setTitle("管理員商品管理");
this.setLayout(null);
this.setSize(710, 550);
this.setLocationRelativeTo(null);
jp2 = new JPanel();
jp2.setBorder(BorderFactory.createLineBorder(Color.gray));
jp2.setLayout(null);
jp2.setBounds(470, 10, 200, 150);
lb_tid = new JLabel("商品編號(hào):");
lb_tname = new JLabel("商品名稱:");
lb_tid.setBounds(10, 10, 70, 30);
lb_tname.setBounds(10, 50, 70, 30);
txt_spId = new JTextField();
txt_spName = new JTextField();
txt_spId.setBounds(80, 10, 100, 30);
txt_spName.setBounds(80, 50, 100, 30);
btn_select = new JButton("條件查詢");
btn_select.setBounds(30, 90, 100, 30);
jp2.add(lb_tid);
jp2.add(lb_tname);
jp2.add(txt_spId);
jp2.add(txt_spName);
jp2.add(btn_select);
jp3 = new JPanel();
jp3.setBorder(BorderFactory.createLineBorder(Color.gray));
jp3.setLayout(null);
jp3.setBounds(470, 170, 200, 300);
lb_spId1 = new JLabel("商品編號(hào):");
lb_spName1 = new JLabel("商品名稱:");
lb_spNum = new JLabel("存貨數(shù)量:");
lb_typename = new JLabel("商品類型:");
lb_spJieShao = new JLabel("商品介紹:");
lb_spId1.setBounds(10, 10, 70, 30);
lb_spName1.setBounds(10, 50, 70, 30);
lb_spNum.setBounds(10, 90, 70, 30);
lb_typename.setBounds(10, 130, 70, 30);
lb_spJieShao.setBounds(10, 170, 70, 30);
jp3.add(lb_spId1);
jp3.add(lb_spName1);
jp3.add(lb_spNum);
jp3.add(lb_typename);
jp3.add(lb_spJieShao);
txt_spid1 = new JTextField();
txt_spid1.setEditable(false);
txt_spName1 = new JTextField();
txt_spNum = new JTextField();
txt_jieShao = new JTextField();
txt_spid1.setBounds(80, 10, 100, 30);
txt_spName1.setBounds(80, 50, 100, 30);
txt_spNum.setBounds(80, 90, 100, 30);
txt_jieShao.setBounds(80, 170, 100, 30);
xiaLaKuang = new JComboBox();
String sql = "select * from sp_type";
ResultSet res_type = DBUtils.Select(sql);
jieHe = new ArrayList<Integer>();
try {
while (res_type.next()) {
xiaLaKuang.addItem(res_type.getObject(2));
jieHe.add(res_type.getInt(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xiaLaKuang.setBounds(80, 130, 100, 30);
btn_insert = new JButton("增加商品");
btn_insert.setBounds(10, 210, 80, 30);
btn_update = new JButton("修改商品");
btn_update.setBounds(95, 210, 80, 30);
btn_qingkong = new JButton("清空");
btn_qingkong.setBounds(10, 250, 160, 30);
btn_select.addActionListener(new shiJianGly(this));
btn_insert.addActionListener(new shiJianGly(this));
btn_update.addActionListener(new shiJianGly(this));
btn_qingkong.addActionListener(new shiJianGly(this));
jp3.add(txt_spid1);
jp3.add(txt_spName1);
jp3.add(txt_spNum);
jp3.add(xiaLaKuang);
jp3.add(txt_jieShao);
jp3.add(btn_insert);
jp3.add(btn_update);
jp3.add(btn_qingkong);
this.add(jp2);
this.add(jp3);
this.setVisible(true);
chuanZhiFangFa(null, null);
}
public void chuanZhiFangFa(String spId, String spName) {
if (jp1 != null) {
this.remove(jp1);
}
String sql = "select * from sp a inner join sp_type b on"
+ " a.sp_typeid=b.sp_typeid? where 1=1";
if (spId != null && spId.length() > 0) {
sql += " and sp_id='" + spId + "'";
}
if (spName != null && spName.length() > 0) {
sql += " and sp_name like'%" + spName + "%'";
}
Vector<Object> v_head = new Vector<Object>();
v_head.add("商品編號(hào)");
v_head.add("商品名稱");
v_head.add("存貨數(shù)量");
v_head.add("商品類型");
v_head.add("商品介紹");
Vector<Vector<Object>> v_body = new Vector<Vector<Object>>();
ResultSet res = DBUtils.Select(sql);
try {
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getInt("sp_id"));
v.add(res.getString("sp_name"));
v.add(res.getInt("spCunHuoShuLiang"));
v.add(res.getString("sp_typename"));
v.add(res.getString("sp_jieshao"));
v_body.add(v);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
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 shiJianGly(this));
JScrollPane jsp = new JScrollPane(jt1);
jsp.setBounds(0, 0, 450, 500);
jp1 = new JPanel();
jp1.setLayout(null);
jp1.setBounds(10, 10, 450, 500);
jp1.add(jsp);
this.add(jp1);
}
}


