Java swing圖書管理系統(tǒng)3(個人設(shè)計思考得出的全功能簡易原版)


package denglu;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import Model.DBUlits;
public class jieyue extends JFrame {
public static DefaultTableModel dtm_tushu, dtm_yonghu, dtm_jieyue = null;
public static JButton JButton_insert, JButton_update, JButton_qingkong,
JButton_huanshu, JButton_jieshu = null;
public static JPanel jp1, jp2, jp3, jp4 = null;
public static JTable JTable_tushu, JTable_user, JTable_jieyue = null;
public static JLabel lb_jid, lb_tname, lb_uname, lb_jnum, lb_jday = null;
public static JTextField txt_jid, txt_tid, txt_uid, txt_jnum,
txt_jday = null;
public jieyue() {
this.setTitle("借閱管理");
this.setLayout(null);
this.setSize(1050, 700);
this.setLocationRelativeTo(null);
jp4 = new JPanel();
jp4.setBorder(BorderFactory.createLineBorder(Color.gray));
jp4.setBounds(420, 10, 190, 450);
jp4.setLayout(null);
lb_jid = new JLabel("借閱編號");
lb_tname = new JLabel("圖書編號:");
lb_uname = new JLabel("用戶編號:");
lb_jnum = new JLabel("借閱數(shù)量:");
lb_jday = new JLabel("借閱日期:");
lb_jid.setBounds(10, 10, 70, 30);
lb_tname.setBounds(10, 50, 70, 30);
lb_uname.setBounds(10, 90, 70, 30);
lb_jnum.setBounds(10, 130, 70, 30);
lb_jday.setBounds(10, 170, 70, 30);
jp4.add(lb_jid);
jp4.add(lb_tname);
jp4.add(lb_uname);
jp4.add(lb_jnum);
jp4.add(lb_jday);
txt_jid = new JTextField();
txt_tid = new JTextField();
txt_uid = new JTextField();
txt_jnum = new JTextField();
txt_jday = new JTextField();
txt_jid.setEnabled(false);
txt_tid.setEnabled(false);
txt_uid.setEnabled(false);
txt_jid.setBounds(80, 10, 100, 30);
txt_tid.setBounds(80, 50, 100, 30);
txt_uid.setBounds(80, 90, 100, 30);
txt_jnum.setBounds(80, 130, 100, 30);
txt_jday.setBounds(80, 170, 100, 30);
JButton_insert = new JButton("借書且增加借閱記錄");
JButton_insert.setBounds(10, 250, 170, 30);
JButton_insert.addActionListener(new shijian_GUL_jieyue(this));
jp4.add(JButton_insert);
JButton_huanshu = new JButton("還書且改變借閱記錄");
JButton_huanshu.setBounds(10, 300, 170, 30);
JButton_huanshu.addActionListener(new shijian_GUL_jieyue(this));
jp4.add(JButton_huanshu);
JButton_update = new JButton("修改但不改變借閱記錄");
JButton_qingkong = new JButton("清空");
JButton_update.setBounds(10, 350, 170, 30);
JButton_qingkong.setBounds(10, 400, 170, 30);
jp4.add(txt_jid);
jp4.add(txt_tid);
jp4.add(txt_uid);
jp4.add(txt_jnum);
jp4.add(txt_jday);
jp4.add(JButton_update);
jp4.add(JButton_qingkong);
JButton_update.addActionListener(new shijian_GUL_jieyue(this));
JButton_qingkong.addActionListener(new shijian_GUL_jieyue(this));
this.add(jp4);
this.setVisible(true);
tushu_load();
user_load();
jieyue_load();
}
// 初始化借閱信息
public void jieyue_load() {
if (jp3 != null) {
this.remove(jp3);
}
Vector<Object> v_head = new Vector<Object>();
v_head.add("借閱編號");
v_head.add("圖書編號");
v_head.add("用戶編號");
v_head.add("圖書名稱");
v_head.add("借閱數(shù)量");
v_head.add("借閱日期");
v_head.add("借閱人");
Vector<Vector<Object>> v_body = new Vector<Vector<Object>>();
String sql = "select * from jieyue a inner join yonghu b on a.yh_id=b.yh_id"
+ " inner join tushu c on a.ts_id=c.ts_id";
ResultSet res = DBUlits.select(sql);
try {
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getInt("jid"));
v.add(res.getString("ts_id"));
v.add(res.getString("yh_id"));
v.add(res.getString("ts_name"));
v.add(res.getInt("jnum"));
v.add(res.getString("jday"));
v.add(res.getString("yh_name"));
v_body.add(v);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dtm_jieyue = new DefaultTableModel(v_body, v_head) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
JTable_jieyue = new JTable(dtm_jieyue);
JTable_jieyue.addMouseListener(new shijian_GUL_jieyue(this));
JScrollPane jsp = new JScrollPane(JTable_jieyue);
jsp.setBounds(0, 0, 400, 300);
jp3 = new JPanel();
jp3.setBounds(620, 10, 400, 350);
jp3.setLayout(null);
jp3.add(jsp);
this.add(jp3);
}
// 初始化圖書
public void tushu_load() {
if (jp1 != null) {
this.remove(jp1);
}
Vector<Object> v_head = new Vector<Object>();
v_head.add("圖書編號");
v_head.add("圖書名稱");
v_head.add("圖書數(shù)量");
v_head.add("圖書類型");
v_head.add("出版社");
Vector<Vector<Object>> v_body = new Vector<Vector<Object>>();
String sql = "select * from tushu a inner join ts_type b on a.ts_leixingid=b.ts_leixingid";
ResultSet res = DBUlits.select(sql);
try {
while (res.next()) {
Vector<Object> v = new Vector<Object>();
v.add(res.getInt("ts_id"));
v.add(res.getString("ts_name"));
v.add(res.getInt("ts_shuliang"));
v.add(res.getString("ts_leixing"));
v.add(res.getString("ts_chubanshe"));
v_body.add(v);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dtm_tushu = new DefaultTableModel(v_body, v_head) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
JTable_tushu = new JTable(dtm_tushu);
JTable_tushu.addMouseListener(new shijian_GUL_jieyue(this));
JScrollPane jsp = new JScrollPane(JTable_tushu);
jsp.setBounds(0, 0, 400, 300);
jp1 = new JPanel();
jp1.setBounds(10, 10, 400, 350);
jp1.setLayout(null);
jp1.add(jsp);
this.add(jp1);
}
// 用戶初始化
public void user_load() {
if (jp2 != null) {
this.remove(jp2);
}
Vector<Object> v_head = new Vector<Object>();
v_head.add("用戶編號");
v_head.add("用戶名");
v_head.add("真實姓名");
Vector<Vector<Object>> v_body = new Vector<Vector<Object>>();
String sql = "select * from yonghu";
ResultSet res = DBUlits.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_name"));
v_body.add(v);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dtm_yonghu = new DefaultTableModel(v_body, v_head) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
JTable_user = new JTable(dtm_yonghu);
JTable_user.addMouseListener(new shijian_GUL_jieyue(this));
JScrollPane jsp = new JScrollPane(JTable_user);
jsp.setBounds(0, 0, 400, 300);
jp2 = new JPanel();
jp2.setBounds(10, 360, 400, 350);
jp2.setLayout(null);
jp2.add(jsp);
this.add(jp2);
}
}
class shijian_GUL_jieyue implements ActionListener, MouseListener {
public jieyue zz = null;
public shijian_GUL_jieyue(jieyue z) {
this.zz = z;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String str = arg0.getActionCommand();
if (str.equals("借書且增加借閱記錄")) {
String jid = zz.txt_jid.getText();
String uid = zz.txt_uid.getText();
String tid = zz.txt_tid.getText();
String jnum = zz.txt_jnum.getText();
String jday = zz.txt_jday.getText();
if (jnum.length() <= 0) {
JOptionPane.showMessageDialog(null, "請輸入借閱數(shù)量");
return;
} else {
if (jnum.length() > 0 && Integer.parseInt(jnum) <= 0) {
JOptionPane.showMessageDialog(null, "借閱數(shù)量必須大于0");
return;
}
}
// String jday = zz.txt_jday.getText();
// JOptionPane.showMessageDialog(null,jday);
if (jday.length() <= 0) {
JOptionPane.showMessageDialog(null, "請輸入借閱日期");
return;
} else {
if (jday.length() > 0 && Integer.parseInt(jday) <= 0) {
JOptionPane.showMessageDialog(null, "借閱日期必須大于0");
return;
}
}
String tnum = "select ts_shuliang from tushu where ts_id='" + tid
+ "'";
ResultSet res_tushu = DBUlits.select(tnum);
try {
if (res_tushu.next()) {
int tushu_num = res_tushu.getInt(1);
if (tushu_num >= Integer.parseInt(jnum)) {
int shengyunum = tushu_num - Integer.parseInt(jnum);
String sql = "insert into jieyue values('" + jnum
+ "','" + jday + "','" + uid + "','" + tid
+ "')";
String sql_update_tushu = "update tushu set ts_shuliang="
+ shengyunum + " where ts_id='" + tid + "'";
if (DBUlits.zsg(sql) && DBUlits.zsg(sql_update_tushu)) {
zz.jieyue_load();
zz.tushu_load();
JOptionPane.showMessageDialog(null, "增加借閱記錄成功");
} else {
JOptionPane.showMessageDialog(null, "出現(xiàn)了未知的錯誤,請重試");
}
} else {
JOptionPane.showMessageDialog(null, "借閱數(shù)量不能大于剩余書籍?dāng)?shù)量");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (str.equals("還書且改變借閱記錄")) {
String jid = zz.txt_jid.getText();
String uid = zz.txt_uid.getText();
String tid = zz.txt_tid.getText();
String jnum = zz.txt_jnum.getText();
String jday = zz.txt_jday.getText();
// int row = zz.JTable_jieyue.getSelectedRow();
// String jid = zz.JTable_jieyue.getValueAt(row, 0).toString();
if (jnum.length() <= 0) {
JOptionPane.showMessageDialog(null, "請輸入借閱數(shù)量");
return;
} else {
if (jnum.length() > 0 && Integer.parseInt(jnum) <= 0) {
JOptionPane.showMessageDialog(null, "借閱數(shù)量必須大于0");
return;
}
}
// String jday = zz.txt_jday.getText();
// JOptionPane.showMessageDialog(null,jday);
if (jday.length() <= 0) {
JOptionPane.showMessageDialog(null, "請輸入借閱日期");
return;
} else {
if (jday.length() > 0 && Integer.parseInt(jday) <= 0) {
JOptionPane.showMessageDialog(null, "借閱日期必須大于0");
return;
}
}
String jy_id = "select * from jieyue a inner join yonghu b on a.yh_id=b.yh_id inner "
+ "join tushu c on a.ts_id=c.ts_id where jid='" + jid + "'";
// String ts_id = "select * from tushu where ts_id='" + tid + "'";
ResultSet res_jy = DBUlits.select(jy_id);
try {
if (res_jy.next()) {
int jum_num = res_jy.getInt("jnum");
int jid_num = res_jy.getInt("jid");
int ts_shuliang_num = res_jy.getInt("ts_shuliang");
int a = jum_num - Integer.parseInt(jnum);
int b = ts_shuliang_num + Integer.parseInt(jnum);
if (a == 0) {
String sql = "delete jieyue where jid=" + jid;
String sql2 = "update tushu set ts_shuliang=" + b
+ "where ts_id=" + tid;
if (DBUlits.zsg(sql) && DBUlits.zsg(sql2)) {
zz.jieyue_load();
zz.tushu_load();
JOptionPane.showMessageDialog(null, "還書成功且還完");
}
} else if (a > 0) {
String sql = "update jieyue set jnum=" + a
+ "where jid=" + jid;
String sql2 = "update tushu set ts_shuliang=" + b
+ "where ts_id=" + tid;
if (DBUlits.zsg(sql) && DBUlits.zsg(sql2)) {
zz.jieyue_load();
zz.tushu_load();
JOptionPane.showMessageDialog(null, "還書成功但沒還完");
}
// zz.jieyue_load();
// zz.tushu_load();
// JOptionPane.showMessageDialog(null, "還書成功");
} else {
JOptionPane.showMessageDialog(null, "出現(xiàn)了未知的錯誤,請重試");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (str.equals("修改但不改變借閱記錄")) {
String jid = zz.txt_jid.getText();
String uid = zz.txt_uid.getText();
String tid = zz.txt_tid.getText();
String jnum = zz.txt_jnum.getText();
String jday = zz.txt_jday.getText();
String sql = "update jieyue set jnum='" + jnum + "',jday='" + jday
+ "',yh_id='" + uid + "',ts_id='" + tid + "' where jid='"
+ jid + "'";
if (DBUlits.zsg(sql)) {
zz.jieyue_load();
zz.tushu_load();
JOptionPane.showMessageDialog(null, "修改借閱記錄成功");
} else {
JOptionPane.showMessageDialog(null,
"選中要改的借閱編號,要改成的借閱人和圖書才可修改!請重新選擇!");
}
}
else if (str.equals("清空")) {
zz.txt_jid.setText("");
zz.txt_jnum.setText("");
zz.txt_jday.setText("");
zz.txt_tid.setText("");
zz.txt_uid.setText("");
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
// 如果點的是圖書表格
if (arg0.getSource() == zz.JTable_tushu) {
// JOptionPane.showMessageDialog(null,"1");
int row = zz.JTable_tushu.getSelectedRow();
zz.txt_tid.setText(zz.JTable_tushu.getValueAt(row, 0).toString());
}
else if (arg0.isMetaDown()) {
int num = JOptionPane.showConfirmDialog(null, "確定要刪除這條信息嗎?");
if (num == 0) {
int row = zz.JTable_jieyue.getSelectedRow();
String jy_id = zz.JTable_jieyue.getValueAt(row, 0).toString();
String ts_id = zz.JTable_jieyue.getValueAt(row, 1).toString();
String yh_id = zz.JTable_jieyue.getValueAt(row, 2).toString();
String ts_name = zz.JTable_jieyue.getValueAt(row, 3).toString();
String jy_shuliang = zz.JTable_tushu.getValueAt(row, 4)
.toString();
String jy_day = zz.JTable_jieyue.getValueAt(row, 5).toString();
String uname = zz.JTable_jieyue.getValueAt(row, 6).toString();
String ts_shuliang = zz.JTable_tushu.getValueAt(row, 2)
.toString();
String sql = "select * from jieyue a inner join yonghu b on a.yh_id=b.yh_id inner join tushu c on a.ts_id=c.ts_id";
// String ts_id = "select * from tushu where ts_id='" + tid +
// "'";
ResultSet res = DBUlits.select(sql);
try {
if (res.next()) {
int ts_num = res.getInt("ts_shuliang");
int jy_num = res.getInt("jnum");
int jy_jday = res.getInt("jday");
int sum = ts_num + jy_num;
String sql3 = "delete jieyue where jid=" + jy_id;
String sql2 = "update tushu set ts_shuliang='" + sum
+ " ' where? ts_id=" + ts_id;
if (DBUlits.zsg(sql3) && DBUlits.zsg(sql2)) {
zz.jieyue_load();
zz.tushu_load();
JOptionPane.showMessageDialog(null, "刪除成功!");
} else {
JOptionPane.showMessageDialog(null, "刪除失敗,請重試");
}
}
} catch (HeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else if (arg0.getSource() == zz.JTable_user) {// 如果點的是用戶表格
// JOptionPane.showMessageDialog(null,"2");
int row = zz.JTable_user.getSelectedRow();
zz.txt_uid.setText(zz.JTable_user.getValueAt(row, 0).toString());
} else if (arg0.getSource() == zz.JTable_jieyue) {// 如果點的是借閱表格
// JOptionPane.showMessageDialog(null,"3");
int row = zz.JTable_jieyue.getSelectedRow();
zz.txt_jid.setText(zz.JTable_jieyue.getValueAt(row, 0).toString());
zz.txt_tid.setText(zz.JTable_jieyue.getValueAt(row, 1).toString());
zz.txt_uid.setText(zz.JTable_jieyue.getValueAt(row, 2).toString());
zz.txt_jnum.setText(zz.JTable_jieyue.getValueAt(row, 4).toString());
zz.txt_jday.setText(zz.JTable_jieyue.getValueAt(row, 5).toString());
}
}
@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
}
}



package denglu;
public class main {
public static void main(String[] args) {
new denglu();
// new zhuce();
// new zhmm();
// new zhaohuimima();
}
}
