基本的思路就是一个Mysql的连接工具类,然后就是GUI图形界面显示。我这里主要是使用了DefaultTableModel来添加行数据
图片资源:在setbag中图片可自行寻找。随时替换
一个完善的图书管理系统必须具备的:
1、登录界面
2、显示图书界面
3、操作界面
4、显示操作后的图书界面
那么我们直接来看代码:
connect连接类:
package com.library.liuzhi;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.spi.DirStateFactory.Result;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
* author:Mr.Liu
* date:2020/5/22 10:33
* 在conn_tool中加上基本的增删改查功能
*/
public class conn_tool {
Statement statement;
Connection conn;
public static void main(String[] args) throws Exception {
new conn_tool();
}
public conn_tool() throws Exception {
// 驱动程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名Database(注意红色的地方是在插入数据库数据交互时(比如:中文 在数据库显示:???)避免乱码使用了UTF-8)
String url = "jdbc:mysql://localhost:3306/Liuzhi? useUnicode=true&characterEncoding=utf8";
// MySQL配置时的用户名
String username = "root";
// MySQL配置时的密码
String password = "root";
// 加载驱动程序
Class.forName(driver);
// 连续数据库
conn = DriverManager.getConnection(url, username, password);
if (!conn.isClosed()) {
System.out.println("Succeeded connecting to the Database!");
}
}
//添加用户名和密码
public void add_admin(String username, String password) {
try {
// statement用来执行SQL语句
if (!username.equals("") && !password.equals("")) {
String sql = "INSERT INTO admin VALUES ('" + username + "',"
+ "'" + password + "')";
try {
statement = conn.createStatement();
statement.execute(sql);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "用户名已存在", "Tips",
JOptionPane.ERROR_MESSAGE);
}
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//更新修改数据
public void select_query(String book_con, String book_name,
String book_price) {
try {
statement = conn.createStatement();
// UPDATE library_store SET book_name='Paaa',book_price='10' WHERE
// book_con=001
String sql = "UPDATE library_store SET book_name='" + book_name
+ "',book_price='" + book_price + "'" + " WHERE book_con="
+ book_con;
statement = conn.createStatement();
int count=statement.executeUpdate(sql);
if(count>0){
JOptionPane.showMessageDialog(null, "更新成功", "Tips",
JOptionPane.OK_CANCEL_OPTION);
}else{
JOptionPane.showMessageDialog(null, "更新失败(编号不存在)", "Tips",
JOptionPane.ERROR_MESSAGE);
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//添加图书信息
public boolean add_bookstore(String book_name, String book_con,
String book_price) {
try {
// statement用来执行SQL语句
if (!book_name.equals("") && !book_con.equals("")
&& !book_price.equals("")) {
statement=conn.createStatement();
String sql = "INSERT INTO library_store VALUES ('" + book_name
+ "'," + "'" + book_con + "','" + book_price + "')";
int count=statement.executeUpdate(sql);
if(count>0){
JOptionPane.showMessageDialog(null, "添加数据成功", "Tips",
JOptionPane.OK_CANCEL_OPTION);
}else{
JOptionPane.showMessageDialog(null, "添加失败(编号重复)", "Tips",
JOptionPane.ERROR_MESSAGE);
}
return true;
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "图书编号已存在", "Tips",
JOptionPane.ERROR_MESSAGE);
}
return false;
}
//刷新数据库数据
public ResultSet update_bookstore() throws Exception {
// statement用来执行SQL语句
statement = conn.createStatement();
String sql = "SELECT * FROM library_store";
ResultSet result = statement.executeQuery(sql);
return result;
}
//删除数据库行数据
public void delete_data(String data) {
// statement用来执行SQL语句
try {
statement = conn.createStatement();
String sql = "DELETE FROM library_store WHERE book_con=" + data;
statement = conn.createStatement();
int count=statement.executeUpdate(sql);
if(count>0){
JOptionPane.showMessageDialog(null, "成功删除", "Tips",
JOptionPane.OK_CANCEL_OPTION);
}else{
JOptionPane.showMessageDialog(null, "编号不存在", "Tips",
JOptionPane.ERROR_MESSAGE);
}
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// DELETE FROM library_store WHERE book_con=001
}
public boolean Login_Test(String username, String password) {
try {
// statement用来执行SQL语句
statement = conn.createStatement();
String sql = "SELECT username,passwords FROM admin WHERE username='"
+ username + "' AND" + " passwords='" + password + "'";
ResultSet result = statement.executeQuery(sql);
while (result.next()) {
String user = result.getString("username");
String pass = result.getString("passwords");
if (user.equals(username) && pass.equals(password)) {
return true;
} else {
return false;
}
}
result.close();
conn.close();
return false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
2、界面登录
package com.library.liuzhi;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Login_view extends JFrame{
JButton select, reseach;
JLabel jl1,jl2;
JTextField jt1,jt2;
static Login_view log;
public static void main(String[] args) {
log=new Login_view();
}
public Login_view(){
this.setDefaultCloseOperation(0);
this.setSize(new Dimension(450, 450));
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
String str = "是否退出图书管理系统界面";
// 添加消息对话框
if (JOptionPane.showConfirmDialog(null, str, "退出管理系统界面",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0); // 退出
}
}
});
jl1=new JLabel("用 户 名:");
jt1=new JTextField(30);
jl1.setBounds(50, 120, 50, 50);
jt1.setBounds(120, 135, 250, 20);
this.add(jl1);
this.add(jt1);
jl2=new JLabel("密 码:");
jt2=new JTextField(30);
jl2.setBounds(50, 180, 50, 50);
jt2.setBounds(120, 195, 250, 20);
this.add(jl2);
this.add(jt2);
select = new JButton("Login");
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
boolean flag=new conn_tool().Login_Test(jt1.getText(), jt2.getText());
if(flag){
jt1.setText("");
jt2.setText("");
JOptionPane.showMessageDialog(null, "登录成功", "Tips", JOptionPane.OK_CANCEL_OPTION);
log.dispose();
new book_store().setVisible(true);
}
else{
JOptionPane.showMessageDialog(null, "账号或密码不存在", "Tips", JOptionPane.ERROR_MESSAGE);
jt1.setText("");
jt2.setText("");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
reseach = new JButton("Register");
reseach.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
new conn_tool().add_admin(jt1.getText(), jt2.getText());
jt1.setText("");
jt2.setText("");
} catch (Exception e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "用户名已存在", "Tips",JOptionPane.OK_CANCEL_OPTION);
}
}
});
select.setBounds(120, 280, 100, 30);
reseach.setBounds(250, 280, 100, 30);
this.add(select);
this.add(reseach);
this.setVisible(true);
setbg();
}
public void setbg(){
((JPanel)this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("../com.liuzhi.test/1.jpeg");
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
}
3、显示图书信息界面
package com.library.liuzhi;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.Vector;
import java.util.concurrent.BrokenBarrierException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.text.html.HTMLDocument.Iterator;
import org.junit.Test;
public class book_store extends JFrame {
/**
* @param args
*/
JButton select, add, delete, update;
static book_store book;
DefaultTableModel tableModel;
JTable ja;
JTextField jt;
JLabel jl, j2, j3, j4;
boolean flush_flag = true;
public book_store() {
this.setTitle("Library Store");
this.setDefaultCloseOperation(0);
this.setSize(new Dimension(600, 600));
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
String str = "是否退出图书管理系统界面";
// 添加消息对话框
if (JOptionPane.showConfirmDialog(null, str, "退出管理系统界面",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
System.exit(0); // 退出
}
}
});
jl = new JLabel("XXXX图书借阅系统");
jl.setBounds(230, 30, 150, 30);
j2 = new JLabel("图书名称");
j2.setBounds(140, 70, 150, 30);
j3 = new JLabel("图书编号");
j3.setBounds(270, 70, 150, 30);
j4 = new JLabel("图书价格");
j4.setBounds(400, 70, 150, 30);
this.add(jl);
this.add(j2);
this.add(j3);
this.add(j4);
ja = new JTable(0, 3);
tableModel = (DefaultTableModel) ja.getModel();
ja.setBounds(100, 100, 390, 390);
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
ja.setDefaultRenderer(Object.class, r);
ja.setEnabled(false);
this.add(ja);
update = new JButton("Updata");
update.setBounds(50, 500, 120, 30);
add = new JButton("Add");
add.setBounds(175, 500, 120, 30);
delete = new JButton("Delete");
delete.setBounds(300, 500, 120, 30);
select = new JButton("Flush");
select.setBounds(425, 500, 120, 30);
this.add(select);
this.add(add);
this.add(delete);
this.add(update);
onClickListerner(add, 1);
onClickListerner(update, 2);
onClickListerner(delete, 3);
onClickListerner(select, 4);
this.setVisible(true);
setbg();
}
//添加行数据
public void add_row(String bookname, String bookcon, String bookprice) {
tableModel = (DefaultTableModel) ja.getModel();
Object[] all = { bookname, bookcon, bookprice };
tableModel.addRow(all);
}
static boolean flag = true;
static ResultSet result;
public void onClickListerner(JButton click, int i) {
switch (i) {
// 数据表显示增加后的数据
case 1:
click.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
flag = true;
new book_add_store().setVisible(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
break;
// 数据表显示所有的被查找的数据
case 2:
click.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
flag = true;
new modify_panle().setVisible(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
break;
// 数据表显示删除操作后的数据
case 3:
click.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
/* 清空数据集合 */
flag = true;
new delete_panle();
}
});
break;
// 刷新整个数据表
case 4:
click.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (flag)
try {
/* 清空数据集合 */
((DefaultTableModel) ja.getModel()).getDataVector()
.clear();
conn_tool tool = new conn_tool();
result = tool.update_bookstore();
while (result.next()) {
String bookname = result.getString("book_name");
String bookcon = result.getString("book_con");
String bookprice = result
.getString("book_price");
add_row(bookname, bookcon, bookprice);
}
flag = false;
result.close();
tool.conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
break;
default:
break;
}
}
//设置窗体背景
public void setbg() {
((JPanel) this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("../com.liuzhi.test/图片.jpeg");
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
}
//删除操作界面
class delete_panle extends JFrame{
JTextField jt1;
JLabel jl;
JButton ok;
public delete_panle(){
this.setDefaultCloseOperation(0);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setSize(new Dimension(300, 300));
this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
String str = "是否退出图书编号删除界面";
// 添加消息对话框
if (JOptionPane.showConfirmDialog(null, str, "退出图书编号删除界面",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
dispose();// 本窗口销毁,释放内存资源// 退出
}
}
});
jl = new JLabel("编号删除");
jl.setBounds(120, 70, 150, 30);
jt1 = new JTextField();
jt1.setBounds(20, 135, 250, 20);
ok = new JButton("确定");
ok.setBounds(100, 180, 80, 20);
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
conn_tool tool=new conn_tool();
tool.delete_data(jt1.getText());
tool.conn.close();
dispose();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
this.add(ok);
this.add(jl);
this.add(jt1);
this.setVisible(true);
}
}
//更新操作界面
class modify_panle extends JFrame {
JTextField jt1, jt2, jt3;
JLabel jl, j2, j3, j4;
JButton ok;
public modify_panle() {
this.setDefaultCloseOperation(0);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setSize(new Dimension(450, 450));
this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
String str = "是否退出图书修改界面";
// 添加消息对话框
if (JOptionPane.showConfirmDialog(null, str, "退出修改界面",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
dispose();// 本窗口销毁,释放内存资源// 退出
}
}
});
jl = new JLabel("xxx图书修改界面");
jl.setBounds(150, 30, 150, 30);
j2 = new JLabel("图书名称");
j3 = new JLabel("图书编号");
j4 = new JLabel("图书价格");
jt1 = new JTextField();
jt2 = new JTextField();
jt3 = new JTextField();
j2.setBounds(50, 180, 80, 50);
jt1.setBounds(120, 135, 250, 20);
j3.setBounds(50, 120, 80, 50);
jt2.setBounds(120, 195, 250, 20);
j4.setBounds(50, 240, 80, 50);
jt3.setBounds(120, 255, 250, 20);
ok = new JButton("确定");
ok.setBounds(190, 315, 80, 20);
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
conn_tool tool = new conn_tool();
tool.select_query(jt1.getText(), jt2.getText(),
jt3.getText());
dispose();
tool.conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
this.add(ok);
this.add(jl);
this.add(j2);
this.add(j3);
this.add(j4);
this.add(jt1);
this.add(jt2);
this.add(jt3);
this.setVisible(true);
}
}
//添加操作界面
class book_add_store extends JFrame {
JTextField jt1, jt2, jt3;
JLabel jl, j2, j3, j4;
JButton ok;
public book_add_store() {
this.setDefaultCloseOperation(0);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setSize(new Dimension(450, 450));
this.setLayout(null);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
String str = "是否退出图书添加界面";
// 添加消息对话框
if (JOptionPane.showConfirmDialog(null, str, "退出添加界面",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
dispose();// 本窗口销毁,释放内存资源// 退出
}
}
});
jl = new JLabel("xxx图书添加界面");
jl.setBounds(150, 30, 150, 30);
j2 = new JLabel("图书名称");
j3 = new JLabel("图书编号");
j4 = new JLabel("图书价格");
jt1 = new JTextField();
jt2 = new JTextField();
jt3 = new JTextField();
j2.setBounds(50, 120, 80, 50);
jt1.setBounds(120, 135, 250, 20);
j3.setBounds(50, 180, 80, 50);
jt2.setBounds(120, 195, 250, 20);
j4.setBounds(50, 240, 80, 50);
jt3.setBounds(120, 255, 250, 20);
ok = new JButton("确定");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
conn_tool tool = new conn_tool();
boolean ll=tool.add_bookstore(jt1.getText(), jt2.getText(), jt3.getText());
dispose();
tool.conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
ok.setBounds(190, 315, 80, 20);
this.add(ok);
this.add(jl);
this.add(j2);
this.add(j3);
this.add(j4);
this.add(jt1);
this.add(jt2);
this.add(jt3);
this.setVisible(false);
}
}
结尾:一个简单的图形界面图书管理系统就做好了,有些地方可能还没做好,希望大家可以指正出来,毕竟学习是无止境的!!!