Java swing簡(jiǎn)化版改進(jìn):?jiǎn)蝹€(gè)圖書管理系統(tǒng):有查詢,清空,增刪改查功能【詩(shī)書畫唱】

前言:
我認(rèn)為無(wú)論是什么,都既要有簡(jiǎn)化版,更多改進(jìn)版和豐富版的話,會(huì)更好等等。--詩(shī)書畫唱
JTable的增刪改查等
SQL部分:
create table ts_Type(
ts_leixingid int primary key identity(1,1),
ts_leixing varchar(100) not null
);
insert into ts_Type values('奇幻');
insert into ts_Type values('愛(ài)情');
insert into ts_Type values('日常');
insert into ts_Type values('科普');
create table tushu(
?ts_ID int primary key identity(1,1),
ts_Name varchar(100) not null,
ts_shuliang int,
?ts_chubanshe varchar(300),
ts_leixingid int
);
insert into tushu values('《奧秘》',12,'人民出版社',1);
insert into tushu values('《愛(ài)你》',2,'人民出版社',2);
insert into tushu values('《詩(shī)書》',4,'人民出版社',3);
insert into tushu values('《科普》',3,'人民出版社',4);

Java部分:


package ZSGC;
import java.sql.*;
public class DBUtils {
static Connection con=null;
static Statement sta=null;
static ResultSet res=null;
//在靜態(tài)代碼塊中執(zhí)行
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//封裝鏈接數(shù)據(jù)庫(kù)的方法
public static Connection getCon(){
if(con==null){
try {
con=DriverManager.getConnection
("jdbc:sqlserver://localhost;databaseName=yonghu","qqq","123");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return con;
}
//查詢的方法
public static ResultSet Select(String sql){
con=getCon();//建立數(shù)據(jù)庫(kù)鏈接
try {
sta=con.createStatement();
res=sta.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
//增刪改查的方法
// 返回int類型的數(shù)據(jù)
public static boolean ZSG(String sql){
con=getCon();//建立數(shù)據(jù)庫(kù)鏈接
boolean b=false;
try {
sta=con.createStatement();
int num=sta.executeUpdate(sql);
//0就是沒(méi)有執(zhí)行成功,大于0 就成功了
if(num>0){
b=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}


package ZSGC;
public class mains {
public static void main(String[] args) {
new ZSGC();
}
}


package ZSGC;
import java.awt.Color;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class shiJian implements MouseListener, ActionListener {
public ZSGC jieShouchuangTi = null;
public shiJian(ZSGC chuangTi) {
this.jieShouchuangTi = chuangTi;
}
@Override
public void actionPerformed(ActionEvent arg0) {
// huoQuDaoDeWenZi:獲取到的文字
String huoQuDaoDeWenZi = arg0.getActionCommand();
if (huoQuDaoDeWenZi.equals("查詢")) {
String tid = jieShouchuangTi.txt_tid.getText();
String uname = jieShouchuangTi.txt_tname.getText();
jieShouchuangTi.tushu_load(tid, uname);
} else if (huoQuDaoDeWenZi.equals("增加圖書")) {
String tname = jieShouchuangTi.txt_tname1.getText();
String tnu = jieShouchuangTi.txt_num.getText();
int typeid = jieShouchuangTi.arr_type.get(jieShouchuangTi.com_type.getSelectedIndex());
String tchubanshe = jieShouchuangTi.txt_tchubanshe.getText();
String sql = "insert into tushu(ts_name,ts_shuliang,ts_leixingid,"
+ "ts_chubanshe) " + "values('" + tname + "'," + tnu + ","
+ typeid + ",'" + tchubanshe + "')";
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.tushu_load(null, null);
JOptionPane.showMessageDialog(null, "圖書增加成功!");
} else {
JOptionPane.showMessageDialog(null, "增加失敗,請(qǐng)重試");
}
} else if (huoQuDaoDeWenZi.equals("修改圖書")) {
String tid = jieShouchuangTi.txt_tid1.getText();
String tname = jieShouchuangTi.txt_tname1.getText();
String tnu = jieShouchuangTi.txt_num.getText();
int typename = jieShouchuangTi.arr_type
.get(jieShouchuangTi.com_type.getSelectedIndex());
String tchubanshe = jieShouchuangTi.txt_tchubanshe.getText();
String sql = "update tushu set? ts_name='" + tname
+ "',ts_shuliang='" + tnu + "',ts_leixingid=" + typename
+ ",ts_chubanshe='" + tchubanshe + "' where ts_id='" + tid
+ "'";
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.tushu_load(null, null);
JOptionPane.showMessageDialog(null, "修改增加成功!");
} else {
JOptionPane.showMessageDialog(null, "修改失敗,請(qǐng)重試");
}
} else if (huoQuDaoDeWenZi.equals("清空")) {
jieShouchuangTi.txt_tid1.setText("");
jieShouchuangTi.txt_tname1.setText("");
jieShouchuangTi.txt_num.setText("");
jieShouchuangTi.txt_tchubanshe.setText("");
jieShouchuangTi.com_type.setSelectedIndex(1);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
int row = jieShouchuangTi.jt1.getSelectedRow();
jieShouchuangTi.txt_tid1.setText(jieShouchuangTi.jt1.getValueAt(row, 0).toString());
jieShouchuangTi.txt_tname1.setText(jieShouchuangTi.jt1.getValueAt(row, 1).toString());
jieShouchuangTi.txt_num.setText(jieShouchuangTi.jt1.getValueAt(row, 2).toString());
jieShouchuangTi.txt_tchubanshe.setText(jieShouchuangTi.jt1.getValueAt(row, 4)
.toString());
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 tid = jieShouchuangTi.jt1.getValueAt(row, 0).toString();
String sql = "delete tushu where ts_id='" + tid + "'";
if (DBUtils.ZSG(sql)) {
jieShouchuangTi.tushu_load(null, null);
JOptionPane.showMessageDialog(null, "圖書刪除成功!");
} else {
JOptionPane.showMessageDialog(null, "刪除失敗,請(qǐng)重試");
}
}
}
}
@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 ZSGC 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 lb_tid, lb_tname = null;
public static JLabel lb_tid1, lb_tname1, lb_tnum, lb_typename,
lb_tchubanshe = null;
public static JTextField txt_tid, txt_tname = null;
public static JTextField txt_tid1, txt_tname1, txt_num,
txt_tchubanshe = null;
public ZSGC() {
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_tid = new JTextField();
txt_tname = new JTextField();
txt_tid.setBounds(80, 10, 100, 30);
txt_tname.setBounds(85, 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_tid);
jp2.add(txt_tname);
jp2.add(btn_select);
jp3 = new JPanel();
jp3.setBorder(BorderFactory.createLineBorder(Color.gray));
jp3.setLayout(null);
jp3.setBounds(470, 170, 200, 300);
lb_tid1 = new JLabel("圖書編號(hào):");
lb_tname1 = new JLabel("圖書名稱:");
lb_tnum = new JLabel("圖書數(shù)量:");
lb_typename = new JLabel("圖書類型:");
lb_tchubanshe = new JLabel("出版社:");
lb_tid1.setBounds(10, 10, 70, 30);
lb_tname1.setBounds(10, 50, 70, 30);
lb_tnum.setBounds(10, 90, 70, 30);
lb_typename.setBounds(10, 130, 70, 30);
lb_tchubanshe.setBounds(10, 170, 70, 30);
jp3.add(lb_tid1);
jp3.add(lb_tname1);
jp3.add(lb_tnum);
jp3.add(lb_typename);
jp3.add(lb_tchubanshe);
txt_tid1 = new JTextField();
txt_tid1.setEditable(false);
txt_tname1 = new JTextField();
txt_num = new JTextField();
txt_tchubanshe = new JTextField();
txt_tid1.setBounds(80, 10, 100, 30);
txt_tname1.setBounds(80, 50, 100, 30);
txt_num.setBounds(80, 90, 100, 30);
txt_tchubanshe.setBounds(80, 170, 100, 30);
com_type = new JComboBox();
String sql = "select * from ts_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) {
// TODO Auto-generated catch block
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 shiJian(this));
btn_insert.addActionListener(new shiJian(this));
btn_update.addActionListener(new shiJian(this));
btn_qingkong.addActionListener(new shiJian(this));
jp3.add(txt_tid1);
jp3.add(txt_tname1);
jp3.add(txt_num);
jp3.add(com_type);
jp3.add(txt_tchubanshe);
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 tushu a inner join ts_type b on"
+ " a.ts_leixingid=b.ts_leixingid where 1=1";
if (tid != null && tid.length() > 0) {
sql += " and ts_id='" + tid + "'";
}
if (tname != null && tname.length() > 0) {
sql += " and ts_name like'%" + tname + "%'";
}
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("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 = new DefaultTableModel(v_body, v_head) {
@Override
public boolean isCellEditable(int a, int b) {
return false;
}
};
jt1 = new JTable(dtm);
jt1.addMouseListener(new shiJian(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);
}
}











