Image类:登陆界面
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class Image {
//定义全局变量,用来判断单选框的状态
static int choice=2;
JFrame frame = new JFrame("图书管理登陆界面");
// 创建用户名和密码标签及两个文本框
JLabel jLabel = new JLabel("<html><font size=5 >用户名:</font></html>");
JLabel jLabel2 = new JLabel("<html><font size=5 >密码:</font></html>");
JTextField text1 = new JTextField(10);
JPasswordField text2 = new JPasswordField(10);
//设置管理者和用户登陆单选按钮
JRadioButton jrb1=new JRadioButton("管理员"),
jrb2=new JRadioButton("用户");
//创建ButtonGroup和面板,添加单选框
ButtonGroup btg=new ButtonGroup();
JPanel panel3=new JPanel();
// 设置登陆,重置,注册按钮
JButton login = new JButton("登陆"), reset = new JButton("重置"), register = new JButton("注册");
// 设置背景颜色
JMenuBar yell = new JMenuBar();
public Image() {
// 使用FlowLayout布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
yell.setOpaque(true);
yell.setBackground(new Color(248, 248, 255));
yell.setPreferredSize(new Dimension(0, 110));
frame.setJMenuBar(yell);
//添加单选框
btg.add(jrb1);
btg.add(jrb2);
// 使用JPanel添加用户名和密码以及文本框
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(2, 2));
panel2.add(jLabel);
panel2.add(text1);
panel2.add(jLabel2);
panel2.add(text2);
frame.add(panel2);
//将单选框添加到容器中,默认值为用户
panel3.add(jrb1);
panel3.add(jrb2);
jrb2.setSelected(true);
frame.add(panel3);
// 注册监视器
listener listen = new listener();
item it=new item();
//为单选框设置监听
jrb1.addItemListener(it);
jrb2.addItemListener(it);
// 使用JPanel添加组件登陆,重置和注册按钮
JPanel panel1 = new JPanel();
panel1.add(login);
panel1.add(reset);
panel1.add(register);
frame.add(panel1);
// 为按钮添加监听器
login.addActionListener(listen);
reset.addActionListener(listen);
register.addActionListener(listen);
// 设置窗口大小不可改变,容器大小,与屏幕相对位置,默认关闭方法,可见
frame.setResizable(false);
frame.setSize(390, 320);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class listener implements ActionListener {
// 检验用户名和密码
String username = null;
String password = null;
@Override
public void actionPerformed(ActionEvent e) {
boolean is=false;
if (e.getSource() == login) {
username = text1.getText();
password = text2.getText();
//用户登陆判断
if(choice==2) {
is = Usermanager.check(username, password);
}
//管理员登陆判断
if(choice==1) {
is=Usermanager.managercheck(username, password);
}
//如果用户名和密码一致则登陆界面
if(is==true) {
if(choice==1) {
new BookImage();
frame.dispose();
}else {
new User(username);
frame.dispose();
}
}else {
JOptionPane.showMessageDialog(null,"密码或用户名输入有误,请重新输入");
}
}
//重置按钮
if (e.getSource() == reset) {
text1.setText("");
text2.setText("");
}
//注册按钮
if (e.getSource() == register) {
frame.dispose();
new Register();
}
}
}
//单选框监听器
public class item implements ItemListener{
@Override
public void itemStateChanged(ItemEvent e) {
//管理员
if(jrb1.isSelected()) {
choice=1;
}
//用户
if(jrb2.isSelected()) {
choice=2;
}
}
}
public static void main(String[] args) {
Image i = new Image();
}
}
BookImage类:管理主界面
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class BookImage {
JFrame frame = new JFrame("图书管理系统");
// 注册监听器
Listener listen = new Listener();
// 创建菜单类
JMenuBar jmb = new JMenuBar();
JMenu fileMenu1 = new JMenu("菜单");
JMenu fileMenu2 = new JMenu("图书类别管理");
JMenuItem jmenu1=new JMenuItem("图书类别添加");
JMenuItem jmenu2=new JMenuItem("图书类别管理");
JMenuItem jmenu3 = new JMenuItem("图书管理");
JMenuItem jmenu4 = new JMenuItem("退出");
JMenuItem jmenu5=new JMenuItem("图书添加");
public BookImage() {
// 将菜单栏与框架关联
frame.setJMenuBar(jmb);
// 将菜单添加到菜单栏
jmb.add(fileMenu1);
//为fileMenu2添加组件
fileMenu2.add(jmenu1);
fileMenu2.add(jmenu2);
// 添加选项
fileMenu1.add(fileMenu2);
fileMenu1.addSeparator();
fileMenu1.add(jmenu3);
fileMenu1.addSeparator();
fileMenu1.add(jmenu5);
fileMenu1.addSeparator();
fileMenu1.add(jmenu4);
//添加监听
jmenu1.addActionListener(listen);
jmenu2.addActionListener(listen);
jmenu3.addActionListener(listen);
jmenu4.addActionListener(listen);
jmenu5.addActionListener(listen);
frame.setResizable(false);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jmenu1) {
new Category();
frame.dispose();
}
if (e.getSource() == jmenu2) {
new CateManager();
frame.dispose();
}
if(e.getSource()==jmenu3) {
new BookManage();
frame.dispose();
}
if (e.getSource() == jmenu4) {
frame.dispose();
}
if(e.getSource()==jmenu5) {
new BookAdd();
frame.dispose();
}
}
}
}
BookAdd:图书添加
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class BookAdd {
JFrame frame = new JFrame("图书添加");
//创建标签
JLabel label1=new JLabel("图书名称:"),
label2=new JLabel("作者:"),
label3=new JLabel("图书类别:"),
label4=new JLabel("图书描述:");
//创建文本框
JTextField field1=new JTextField(10),
field2=new JTextField(8);
//创建文本域
JTextArea field3=new JTextArea(5,23);
//创建滚动条并将文本域加入
JScrollPane scro=new JScrollPane(field3);
//创建面板
JPanel panel1=new JPanel(),
panel2=new JPanel(),
panel3=new JPanel(),
panel4=new JPanel();
//创建按钮
JButton button1=new JButton("添加"),
button2=new JButton("重置"),
button3=new JButton("返回");
//创建类别组合框
JComboBox<String> os=new JComboBox<>();
//创建集合List接受返回图书类别
List<String> list=new ArrayList<>();
public BookAdd() {
//设置流动布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
panel1.setLayout(new FlowLayout(FlowLayout.CENTER,10,15));
panel2.setLayout(new FlowLayout(FlowLayout.CENTER,10,15));
panel3.setLayout(new FlowLayout(FlowLayout.CENTER,10,15));
panel4.setLayout(new FlowLayout(FlowLayout.CENTER,40,15));
//设置自动换行
field3.setLineWrap(true);
//将标签1,2和文本框1,2加入到面板panel1
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
//将面板panel1添加到容器
frame.add(panel1);
//获取数据库中图书类别
list=CategoryData.cate();
for(int i=0;i<list.size();i++) {
os.addItem(list.get(i));
}
//设置下拉列表默认值
os.setSelectedIndex(0);
//固定下拉比列表框的初始长度
os.setPreferredSize(new Dimension(237,25));
//将标签3和下拉列表框加入到panel2
panel2.add(label3);
panel2.add(os);
//将panel2加入到容器中
frame.add(panel2);
//将便签4和文本域加入到面板panel3中
panel3.add(label4);
panel3.add(scro);
//加入到容器
frame.add(panel3);
//将按钮添加到面板,并将面板添加到容器
panel4.add(button1);
panel4.add(button2);
panel4.add(button3);
frame.add(panel4);
//注册监听器
Action action=new Action();
os.addActionListener(action);
button1.addActionListener(action);
button2.addActionListener(action);
button3.addActionListener(action);
// 设置窗口大小不可改变,容器大小,与屏幕相对位置,默认关闭方法,可见
frame.setResizable(false);
frame.setSize(390, 370);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class Action implements ActionListener{
//获取下拉列表默认值
String value=list.get(0);
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==os) {
value=(String)os.getSelectedItem();
}
if(e.getSource()==button1) {
//获取添加图书编号
int id= CategoryData.id(value);
//获取图书名称,作者及描述
String fie1=field1.getText();
String fie2=field2.getText();
String fie3=field3.getText();
CategoryData.Add(id, fie1, fie2, fie3);
JOptionPane.showMessageDialog(null, "添加成功");
}
if(e.getSource()==button2) {
field1.setText("");
field2.setText("");
field3.setText("");
}
if(e.getSource()==button3) {
new BookImage();
frame.dispose();
}
}
}
}
BookManage类:图书管理类
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class BookManage {
JFrame frame = new JFrame("图书管理");
// 创建标签
JLabel label1 = new JLabel("图书名称:"), label2 = new JLabel("图书作者:"), label3 = new JLabel("编号:"),
label4 = new JLabel("图书类别名称:"), label5 = new JLabel("描述:");
// 创建文本框
JTextField field1 = new JTextField(10), field2 = new JTextField(10), field3 = new JTextField(10),
field4 = new JTextField(10);
// 创建文本域
JTextArea field5 = new JTextArea(4, 36);
// 创建按钮
JButton button1 = new JButton("查询"), button2 = new JButton("修改"), button3 = new JButton("删除"),
button4 = new JButton("返回");
// 创建面板
JPanel panel1 = new JPanel(), panel2 = new JPanel(), panel3 = new JPanel(), panel4 = new JPanel();
// 创建一维数组
String[] str1 = { "编号", "图书名称", "图书作者", "图书描述", "图书类别","借书人" };
// 创建集合list接受返回图书信息
List<Book> list = new ArrayList<>();
public BookManage() {
// 设置流动布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20));
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 10));
panel4.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 10));
// 将组件添加到面板再添加到容器
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
panel1.add(button1);
frame.add(panel1);
// 创建二维数据并创建表单
list = CategoryData.book();
Object[][] table1 = new Object[list.size()][6];
for (int i = 0; i < list.size(); i++) {
table1[i][0] = list.get(i).getId();
table1[i][1] = list.get(i).getName();
table1[i][2] = list.get(i).getAuthor();
table1[i][3] = list.get(i).getDes();
String cate = CategoryData.Cate(list.get(i).getCateID());
table1[i][4] = cate;
table1[i][5]=list.get(i).getUser();
}
JTable table = new JTable(table1, str1);
// 设置表格大小并将其添加到滚动组件中,最后添加到容器
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
JScrollPane scro = new JScrollPane(table);
frame.add(scro);
// 添加组件到面板,設置field3為不可編輯
field3.setEditable(false);
panel2.add(label3);
panel2.add(field3);
panel2.add(label4);
panel2.add(field4);
frame.add(panel2);
// 添加文本域
field5.setLineWrap(true);
JScrollPane scr = new JScrollPane(field5);
panel3.add(label5);
panel3.add(scr);
frame.add(panel3);
// 添加按鈕
panel4.add(button2);
panel4.add(button3);
panel4.add(button4);
frame.add(panel4);
// 注冊監聽
Action action = new Action();
button1.addActionListener(action);
button2.addActionListener(action);
button3.addActionListener(action);
button4.addActionListener(action);
// 设置窗口大小不可改变,容器大小,与屏幕相对位置,默认关闭方法,可见
// frame.setResizable(false);
frame.setSize(580, 490);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String con1 = field1.getText();
String con2 = field2.getText();
if (con1.equals("") || con2.equals("")) {
JOptionPane.showMessageDialog(null, "请先输入查询信息");
}
// 查询图书
List list = new ArrayList();
list = CategoryData.Select(con1, con2);
field3.setText(list.get(0).toString());
field4.setText(list.get(1).toString());
field5.setText(list.get(2).toString());
}
//修改图书
if (e.getSource() == button2) {
String con1 = field3.getText();
if (con1.equals("")) {
JOptionPane.showMessageDialog(null, "请先查询");
}else {
String con2=field4.getText();
String con3=field5.getText();
int is=CategoryData.Modify(Integer.parseInt(con1), con2, con3);
if(is==1) {
JOptionPane.showMessageDialog(null, "修改成功");
}else {
JOptionPane.showMessageDialog(null, "不存在该图书类别,请重新修改");
}
new BookManage();
}
}
//删除图书
if (e.getSource() == button3) {
String con1 = field3.getText();
if (con1.equals("")) {
JOptionPane.showMessageDialog(null, "请先查询");
}else {
String bookname=field1.getText();
boolean is=CategoryData.user(bookname);
if(is==true) {
CategoryData.DELETE(Integer.parseInt(con1));
JOptionPane.showMessageDialog(null, "删除成功");
new BookManage();
}
if(is==false) {
JOptionPane.showMessageDialog(null, "该书已被借走,无法删除");
}
}
}
if(e.getSource()==button4) {
new BookImage();
frame.dispose();
}
}
}
}
CategoryData类:数据库操作类
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class CategoryData {
// 返回图书类别数据
public static List<BookCategory> Data() {
List<BookCategory> list = new ArrayList<>();
Connection coll = null;
PreparedStatement prepare = null;
ResultSet result = null;
try {
coll = JDBCTools.getConnection();
String sql = "SELECT * FROM category";
prepare = coll.prepareStatement(sql);
result = prepare.executeQuery();
while (result.next()) {
BookCategory cate = new BookCategory();
cate.setId(result.getInt(1));
cate.setCate(result.getString(2));
cate.setDes(result.getString(3));
list.add(cate);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, prepare, result);
}
return list;
}
// 检查是否存在该类别
public static List<BookCategory> check(String cate) {
List<BookCategory> list = new ArrayList<>();
Connection connection = null;
PreparedStatement prepared = null;
ResultSet result = null;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT * From category WHERE cate=?";
prepared = connection.prepareStatement(sql);
prepared.setString(1, cate);
result = prepared.executeQuery();
if (result.next()) {
BookCategory book = new BookCategory();
book.setId(result.getInt(1));
;
book.setCate(result.getString(2));
book.setDes(result.getString(3));
list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, result);
}
return list;
}
// 执行删除操作
public static void remove(int id) {
Connection connection = null;
PreparedStatement prepared = null;
try {
connection = JDBCTools.getConnection();
String sql = "DELETE From category WHERE id=?";
prepared = connection.prepareStatement(sql);
prepared.setInt(1, id);
prepared.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, null);
}
}
// 修改操作
public static void modify(int id, String cate, String content) {
Connection connection = null;
PreparedStatement prepared = null;
try {
connection = JDBCTools.getConnection();
String sql = "UPDATE category SET cate=? ,des=? WHERE id=?";
prepared = connection.prepareStatement(sql);
prepared.setString(1, cate);
prepared.setString(2, content);
prepared.setInt(3, id);
prepared.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, null);
}
}
// 图书类别添加
public static void add(String cate, String des) {
Connection coll = null;
PreparedStatement prepared = null;
try {
coll = JDBCTools.getConnection();
String sql = "INSERT INTO category(cate,des) VALUES(?,?)";
prepared = coll.prepareStatement(sql);
prepared.setString(1, cate);
prepared.setString(2, des);
prepared.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, prepared, null);
}
}
// 返回多个图书类别
public static List<String> cate() {
List<String> list = new ArrayList<>();
Connection coll = null;
PreparedStatement prepared = null;
ResultSet result = null;
try {
coll = JDBCTools.getConnection();
String sql = "SELECT cate FROM category";
prepared = coll.prepareStatement(sql);
result = prepared.executeQuery();
while (result.next()) {
list.add(result.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, prepared, result);
}
return list;
}
// 添加图书
public static void Add(int cate, String name, String author, String des) {
Connection coll = null;
PreparedStatement pre = null;
try {
coll = JDBCTools.getConnection();
String sql = "INSERT INTO book(bookname,author,des,cateID) VALUES(?,?,?,?)";
pre = coll.prepareStatement(sql);
pre.setString(1, name);
pre.setString(2, author);
pre.setString(3, des);
pre.setInt(4, cate);
pre.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, pre, null);
}
}
// 获取图书编号
public static int id(String cat) {
int id = 0;
Connection coll = null;
PreparedStatement prepared = null;
ResultSet result = null;
try {
coll = JDBCTools.getConnection();
String sql = "SELECT id FROM category WHERE cate='" + cat + "'";
prepared = coll.prepareStatement(sql);
result = prepared.executeQuery();
while (result.next()) {
id = result.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, prepared, result);
}
return id;
}
// 返回图书信息
public static List<Book> book() {
List<Book> list = new ArrayList<>();
Connection coll = null;
PreparedStatement prepared = null;
ResultSet result = null;
try {
coll = JDBCTools.getConnection();
String sql = "SELECT * FROM book";
prepared = coll.prepareStatement(sql);
result = prepared.executeQuery();
while (result.next()) {
Book book = new Book();
book.setId(result.getInt(1));
book.setName(result.getString(2));
book.setAuthor(result.getString(3));
book.setDes(result.getString(4));
book.setCateID(result.getInt(5));
book.setUser(result.getString(6));
list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, prepared, result);
}
return list;
}
// 返回图书类别
public static String Cate(int cateID) {
String cate = null;
Connection coll = null;
PreparedStatement prepared = null;
ResultSet result = null;
try {
coll = JDBCTools.getConnection();
String sql = "SELECT cate FROM category WHERE id=" + cateID;
prepared = coll.prepareStatement(sql);
result = prepared.executeQuery();
if (result.next()) {
cate = result.getString(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, prepared, result);
}
return cate;
}
// 查询图书
public static List Select(String bookname, String author) {
List list = new ArrayList();
Connection coll = null;
PreparedStatement prepared = null;
ResultSet result = null;
try {
coll = JDBCTools.getConnection();
String sql = "SELECT id,cateID,des FROM book WHERE bookname='" + bookname + "' AND author='" + author + "'";
prepared = coll.prepareStatement(sql);
result = prepared.executeQuery();
if (result.next()) {
list.add(result.getInt(1));
list.add(Cate(result.getInt(2)));
list.add(result.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, prepared, result);
}
return list;
}
// 修改图书
public static int Modify(int id, String cate, String des) {
int is = 0;
Connection connection = null;
PreparedStatement prepared = null;
try {
connection = JDBCTools.getConnection();
// 返回图书类型
int cateid = id(cate);
String sql = "UPDATE book SET cateID=? ,des=? WHERE id=?";
prepared = connection.prepareStatement(sql);
prepared.setInt(1, cateid);
prepared.setString(2, des);
prepared.setInt(3, id);
is = prepared.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, null);
}
return is;
}
// 删除图书
public static void DELETE(int id) {
Connection connection = null;
PreparedStatement prepared = null;
try {
connection = JDBCTools.getConnection();
String sql = "DELETE From book WHERE id=?";
prepared = connection.prepareStatement(sql);
prepared.setInt(1, id);
prepared.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, null);
}
}
// 判断图书状态
public static boolean user(String bookname) {
boolean is = true;
String us = "";
Connection coll = null;
PreparedStatement prepared = null;
ResultSet result = null;
try {
coll = JDBCTools.getConnection();
String sql = "SELECT user FROM book WHERE bookname='" + bookname + "'";
prepared = coll.prepareStatement(sql);
result = prepared.executeQuery();
if (result.next()) {
us = result.getString(1);
}
if (us != null) {
is = false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(coll, prepared, result);
}
return is;
}
// 借书功能
public static void borrow(String name,int id) {
Connection connection = null;
PreparedStatement prepared = null;
try {
connection = JDBCTools.getConnection();
String sql = "UPDATE book SET user=? WHERE id=?";
prepared = connection.prepareStatement(sql);
prepared.setString(1,name);
prepared.setInt(2, id);
prepared.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, null);
}
}
//还书功能
public static void rt(String bookname) {
Connection connection = null;
PreparedStatement prepared = null;
try {
connection = JDBCTools.getConnection();
String sql = "UPDATE book SET user=null WHERE bookname=?";
prepared = connection.prepareStatement(sql);
prepared.setString(1,bookname);
prepared.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, null);
}
}
//判断还书人是否一致
public static boolean ruser(String user,int id) {
boolean is=false;
Connection connection = null;
PreparedStatement prepared = null;
ResultSet result=null;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT user FROM book WHERE id="+id;
prepared = connection.prepareStatement(sql);
result=prepared.executeQuery();
if(result.next()) {
if(result.getString(1).equals(user)) {
is=true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, result);
}
return is;
}
}
BookCategory类:图书分类属性
package Design;
public class BookCategory {
private int id;
private String cate;
private String des;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCate() {
return cate;
}
public void setCate(String cate) {
this.cate = cate;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
@Override
public String toString() {
return "id="+id+",cate="+cate+",des="+des;
}
}
Category类:图书类别添加类
package Design;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Category {
JFrame frame = new JFrame("图书类别添加");
JLabel label1 = new JLabel("图书类别名称:"), label2 = new JLabel("图书类别描述:");
// 创建文本框和文本域
JTextField field1 = new JTextField(10);
JTextArea field2 = new JTextArea(8, 40);
// 创建面板
JPanel panel1 = new JPanel(), panel2 = new JPanel(), panel3 = new JPanel();
// 创建添加和重置按钮
JButton button1 = new JButton("添加"), button2 = new JButton("重置"),button3=new JButton("返回");
public Category() {
// 注册监听器
listen lis = new listen();
// 使用流动布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 800, 20));
panel2.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
panel3.setLayout(new FlowLayout(FlowLayout.CENTER, 55, 0));
// 添加组件
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
// 设置自动换行和添加滚动条
field2.setLineWrap(true);
JScrollPane jsp = new JScrollPane(field2);
// 将label2和文本域添加到panel2
panel2.add(label2);
panel2.add(jsp);
// 将按钮添加到panel3
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
//设置监听器
button1.addActionListener(lis);
button2.addActionListener(lis);
button3.addActionListener(lis);
//面板添加到容器
frame.add(panel1);
frame.add(label2);
frame.add(panel2);
frame.add(panel3);
frame.setResizable(false);
frame.setSize(500, 370);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class listen implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//注册
if (e.getSource() == button1) {
String str1=field1.getText();
String str2=field2.getText();
CategoryData.add(str1, str2);
JOptionPane.showMessageDialog(null, "添加成功");
}
//重置
if (e.getSource() == button2) {
field1.setText("");
field2.setText("");
}
if(e.getSource()==button3) {
new BookImage();
frame.dispose();
}
}
}
}
CateManager类:图书管理界面
package Design;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class CateManager {
JFrame frame = new JFrame("图书类别管理");
// 创建标签
JLabel label1 = new JLabel("图书类别名称:"),
label2=new JLabel("编号:"),
label3=new JLabel("图书类别名称:"),
label4=new JLabel("描述:");
// 创建文本框
JTextField field1 = new JTextField(10),
field2=new JTextField(10),
field3=new JTextField(10);
//创建文本域
JTextArea field4=new JTextArea(3,35);
// 创建按钮
JButton button1 = new JButton("查询");
// 创建面板
JPanel panel1 = new JPanel(),
panel2=new JPanel(),
panel3=new JPanel(),
panel4=new JPanel();
// 创建List集合接受返回表单的值
List<BookCategory> list = new ArrayList<>();
// 表头
String[] names = { "编号", "图书类别名称", "图书类别描述" };
//创建添加删除按钮
JButton button2=new JButton("修改"),
button3=new JButton("删除"),
button4=new JButton("返回");
public CateManager() {
//注册监听
Listen listen=new Listen();
// 设置流动布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 30));
panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
panel4.setLayout(new FlowLayout(FlowLayout.CENTER,20,0));
// 将组件添加到面板
panel1.add(label1);
panel1.add(field1);
panel1.add(button1);
// 将面板添加到容器
frame.add(panel1);
// 创建二维数据并创建表单
list = CategoryData.Data();
Object[][] table1 = new Object[list.size()][3];
for (int i = 0; i < list.size(); i++) {
table1[i][0] = list.get(i).getId();
table1[i][1] = list.get(i).getCate();
table1[i][2] = list.get(i).getDes();
}
JTable table = new JTable(table1, names);
//设置表格大小并将其添加到滚动组件中,最后添加到容器
table.setPreferredScrollableViewportSize(new Dimension(450, 100));
JScrollPane scro = new JScrollPane(table);
frame.add(scro);
//显示内容
panel2.add(label2);
panel2.add(field2);
panel2.add(label3);
panel2.add(field3);
frame.add(panel2);
//描述
panel3.add(label4);
field4.setLineWrap(true);
JScrollPane scro2=new JScrollPane(field4);
panel3.add(scro2);
frame.add(panel3);
//添加按钮
panel4.add(button2);
panel4.add(button3);
panel4.add(button4);
frame.add(panel4);
//设置编号为不可修改
field2.setEditable(false);
//添加监听
button1.addActionListener(listen);
button2.addActionListener(listen);
button3.addActionListener(listen);
button4.addActionListener(listen);
// frame.setResizable(false);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class Listen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//查询按钮操作
if(e.getSource()==button1) {
String content=field1.getText();
if(content.equals("")) {
JOptionPane.showMessageDialog(null,"查询不能为空");
}else {
List<BookCategory> list=new ArrayList<>();
list=CategoryData.check(content);
if(list.size()!=0) {
//将查询结果显示出来
Integer i=list.get(0).getId();
field2.setText(i.toString());
field3.setText(list.get(0).getCate());
field4.setText(list.get(0).getDes());
}else {
JOptionPane.showMessageDialog(null, "不存在该类别,请重新查询");
}
}
}
//修改按钮
if(e.getSource()==button2) {
String id=field2.getText();
String cate=field3.getText();
String content=field4.getText();
CategoryData.modify(Integer.parseInt(id), cate, content);
JOptionPane.showMessageDialog(null, "修改成功");
frame.dispose();
new CateManager();
}
//删除按钮
if(e.getSource()==button3) {
String s=field2.getText();
if(s.equals("")) {
JOptionPane.showMessageDialog(null, "请先输入查询内容");
}else {
CategoryData.remove(Integer.parseInt(s));
JOptionPane.showMessageDialog(null, "删除成功");
frame.dispose();
new CateManager();
}
}
if(e.getSource()==button4) {
new BookImage();
frame.dispose();
}
}
}
}
JDBCTools:连接数据库
package Design;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class JDBCTools {
public static Connection getConnection() throws Exception{
String driverClass=null;
String jdbcUrl=null;
String user=null;
String password=null;
//读取类路径下的jdbc.properties 文件
//JDBCTools.class运用反射使得此方法可以静态使用(getClass不可以)
InputStream in=JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
driverClass=properties.getProperty("driver");
jdbcUrl=properties.getProperty("jdbcUrl");
user=properties.getProperty("user");
password=properties.getProperty("password");
//加载驱动
Class.forName(driverClass);
Connection connection=(Connection) DriverManager.getConnection(jdbcUrl,user,password);
return connection;
}
//关闭资源
public static void release(Connection conn,Statement stmt,ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Register:注册页面
package Design;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.channels.ShutdownChannelGroupException;
import javax.swing.*;
public class Register {
JFrame frame = new JFrame("用户注册");
// 设置背景颜色
JMenuBar yell = new JMenuBar();
// 设置用户名,密码,确认密码标签
JLabel jLabel = new JLabel("<html><font size=5 >用户名:</font></html>");
JLabel jLabel2 = new JLabel("<html><font size=5 >密码:</font></html>");
JLabel jLabel3 = new JLabel("<html><font size=5 >确认密码:</font></html>");
// 设置三个文本框
JTextField text1 = new JTextField(10);
JPasswordField text2 = new JPasswordField(10);
JPasswordField text3 = new JPasswordField(10);
// 创建注册按钮
JButton jbutton = new JButton("注册");
// 创建面板,使用GridLayout布局,并将文本框加入
JPanel jpanel = new JPanel();
public Register() {
// 设置流动布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER));
yell.setOpaque(true);
yell.setBackground(new Color(248, 248, 255));
yell.setPreferredSize(new Dimension(0, 200));
frame.setJMenuBar(yell);
jpanel.setLayout(new GridLayout(3, 2));
jpanel.add(jLabel);
jpanel.add(text1);
jpanel.add(jLabel2);
jpanel.add(text2);
jpanel.add(jLabel3);
jpanel.add(text3);
// 将JPanel面板加入到容器中
frame.add(jpanel);
frame.add(jbutton);
// 为注册按钮加上响应
Listener listener = new Listener();
jbutton.addActionListener(listener);
// 设置界面大小,相对位置,关闭方式及是否可见
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// 接受注册用户的密码和用户名
String name = null;
String password = null;
// 密码不一致
if (e.getSource() == jbutton) {
if (!text2.getText().equals(text3.getText())) {
JOptionPane.showMessageDialog(null, "密码不一致,请重新输入");
} else {
name = text1.getText();
password = text2.getText();
boolean is = Usermanager.check(name);
if(is==true) {
JOptionPane.showMessageDialog(null, "用户名已存在,请重新输入");
}else {
Usermanager.register(name, password);
JOptionPane.showMessageDialog(null, "注册成功");
text1.setText("");
text2.setText("");
text3.setText("");
}
}
}
}
}
}
Usermanager:检查用户信息(用户和管理者)
package Design;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Usermanager {
// 检查用户名和密码
public static boolean check(String username, String passwords) {
Connection connection = null;
PreparedStatement prepared = null;
ResultSet result = null;
boolean is = false;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT * From message WHERE name=? AND password=?";
prepared = connection.prepareStatement(sql);
prepared.setString(1, username);
prepared.setString(2, passwords);
result = prepared.executeQuery();
if (result.next()) {
is = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, result);
}
return is;
}
// 注册时检查用户名是否已经存在
public static boolean check(String username) {
Connection connection = null;
PreparedStatement prepared = null;
ResultSet result = null;
boolean is = false;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT name From message where name=?";
prepared = connection.prepareStatement(sql);
prepared.setString(1, username);
result = prepared.executeQuery();
// 用户名存在,is为true
if (result.next()) {
is = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, result);
}
return is;
}
// 注册用户名
public static void register(String username, String passwords) {
Connection connection = null;
PreparedStatement prepared = null;
try {
connection = JDBCTools.getConnection();
String sql = "INSERT INTO message VALUES(?,?)";
prepared = connection.prepareStatement(sql);
prepared.setString(1, username);
prepared.setString(2, passwords);
prepared.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, null);
}
}
// 管理者身份验证
public static boolean managercheck(String username, String passwords) {
// 检查用户名和密码
Connection connection = null;
PreparedStatement prepared = null;
ResultSet result = null;
boolean is = false;
try {
connection = JDBCTools.getConnection();
String sql = "SELECT * From manager WHERE name=? AND password=?";
prepared = connection.prepareStatement(sql);
prepared.setString(1, username);
prepared.setString(2, passwords);
result = prepared.executeQuery();
if (result.next()) {
is = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, prepared, result);
}
return is;
}
}
CategoryManager:图书类别管理
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class CateManager {
JFrame frame = new JFrame("图书类别管理");
// 创建标签
JLabel label1 = new JLabel("图书类别名称:"),
label2=new JLabel("编号:"),
label3=new JLabel("图书类别名称:"),
label4=new JLabel("描述:");
// 创建文本框
JTextField field1 = new JTextField(10),
field2=new JTextField(10),
field3=new JTextField(10);
//创建文本域
JTextArea field4=new JTextArea(3,35);
// 创建按钮
JButton button1 = new JButton("查询");
// 创建面板
JPanel panel1 = new JPanel(),
panel2=new JPanel(),
panel3=new JPanel(),
panel4=new JPanel();
// 创建List集合接受返回表单的值
List<BookCategory> list = new ArrayList<>();
// 表头
String[] names = { "编号", "图书类别名称", "图书类别描述" };
//创建添加删除按钮
JButton button2=new JButton("修改"),
button3=new JButton("删除"),
button4=new JButton("返回");
public CateManager() {
//注册监听
Listen listen=new Listen();
// 设置流动布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 30));
panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
panel4.setLayout(new FlowLayout(FlowLayout.CENTER,20,0));
// 将组件添加到面板
panel1.add(label1);
panel1.add(field1);
panel1.add(button1);
// 将面板添加到容器
frame.add(panel1);
// 创建二维数据并创建表单
list = CategoryData.Data();
Object[][] table1 = new Object[list.size()][3];
for (int i = 0; i < list.size(); i++) {
table1[i][0] = list.get(i).getId();
table1[i][1] = list.get(i).getCate();
table1[i][2] = list.get(i).getDes();
}
JTable table = new JTable(table1, names);
//设置表格大小并将其添加到滚动组件中,最后添加到容器
table.setPreferredScrollableViewportSize(new Dimension(450, 100));
JScrollPane scro = new JScrollPane(table);
frame.add(scro);
//显示内容
panel2.add(label2);
panel2.add(field2);
panel2.add(label3);
panel2.add(field3);
frame.add(panel2);
//描述
panel3.add(label4);
field4.setLineWrap(true);
JScrollPane scro2=new JScrollPane(field4);
panel3.add(scro2);
frame.add(panel3);
//添加按钮
panel4.add(button2);
panel4.add(button3);
panel4.add(button4);
frame.add(panel4);
//设置编号为不可修改
field2.setEditable(false);
//添加监听
button1.addActionListener(listen);
button2.addActionListener(listen);
button3.addActionListener(listen);
button4.addActionListener(listen);
// frame.setResizable(false);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class Listen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//查询按钮操作
if(e.getSource()==button1) {
String content=field1.getText();
if(content.equals("")) {
JOptionPane.showMessageDialog(null,"查询不能为空");
}else {
List<BookCategory> list=new ArrayList<>();
list=CategoryData.check(content);
if(list.size()!=0) {
//将查询结果显示出来
Integer i=list.get(0).getId();
field2.setText(i.toString());
field3.setText(list.get(0).getCate());
field4.setText(list.get(0).getDes());
}else {
JOptionPane.showMessageDialog(null, "不存在该类别,请重新查询");
}
}
}
//修改按钮
if(e.getSource()==button2) {
String id=field2.getText();
String cate=field3.getText();
String content=field4.getText();
CategoryData.modify(Integer.parseInt(id), cate, content);
JOptionPane.showMessageDialog(null, "修改成功");
frame.dispose();
new CateManager();
}
//删除按钮
if(e.getSource()==button3) {
String s=field2.getText();
if(s.equals("")) {
JOptionPane.showMessageDialog(null, "请先输入查询内容");
}else {
CategoryData.remove(Integer.parseInt(s));
JOptionPane.showMessageDialog(null, "删除成功");
frame.dispose();
new CateManager();
}
}
if(e.getSource()==button4) {
new BookImage();
frame.dispose();
}
}
}
}
User:用户界面
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import Design.BookManage.Action;
public class User {
JFrame frame=new JFrame("图书界面");
// 创建标签
JLabel label1 = new JLabel("图书名称:"), label2 = new JLabel("图书作者:"), label3 = new JLabel("编号:"),
label4 = new JLabel("图书类别名称:"), label5 = new JLabel("描述:");
// 创建文本框
JTextField field1 = new JTextField(10), field2 = new JTextField(10), field3 = new JTextField(10),
field4 = new JTextField(10);
// 创建文本域
JTextArea field5 = new JTextArea(4, 36);
// 创建按钮
JButton button1 = new JButton("查询"), button2 = new JButton("借书"), button3 = new JButton("还书"),
button4 = new JButton("退出");
// 创建面板
JPanel panel1 = new JPanel(), panel2 = new JPanel(), panel3 = new JPanel(), panel4 = new JPanel();
// 创建一维数组
String[] str1 = { "编号", "图书名称", "图书作者", "图书描述", "图书类别","借书人" };
// 创建集合list接受返回图书信息
List<Book> list = new ArrayList<>();
//接受用户名
String Username=null;
public User() {}
public User(String name) {
//接受用户名
Username=name;
// 设置流动布局
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20));
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 10));
panel4.setLayout(new FlowLayout(FlowLayout.CENTER, 40, 10));
// 将组件添加到面板再添加到容器
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
panel1.add(button1);
frame.add(panel1);
// 创建二维数据并创建表单
list = CategoryData.book();
Object[][] table1 = new Object[list.size()][6];
for (int i = 0; i < list.size(); i++) {
table1[i][0] = list.get(i).getId();
table1[i][1] = list.get(i).getName();
table1[i][2] = list.get(i).getAuthor();
table1[i][3] = list.get(i).getDes();
String cate = CategoryData.Cate(list.get(i).getCateID());
table1[i][4] = cate;
table1[i][5]=list.get(i).getUser();
}
JTable table = new JTable(table1, str1);
// 设置表格大小并将其添加到滚动组件中,最后添加到容器
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
JScrollPane scro = new JScrollPane(table);
frame.add(scro);
// 添加组件到面板,設置field3,4,5為不可編輯
field3.setEditable(false);
field4.setEditable(false);
field5.setEditable(false);
panel2.add(label3);
panel2.add(field3);
panel2.add(label4);
panel2.add(field4);
frame.add(panel2);
// 添加文本域
field5.setLineWrap(true);
JScrollPane scr = new JScrollPane(field5);
panel3.add(label5);
panel3.add(scr);
frame.add(panel3);
// 添加按鈕
panel4.add(button2);
panel4.add(button3);
panel4.add(button4);
frame.add(panel4);
// 注冊監聽
Action action = new Action();
button1.addActionListener(action);
button2.addActionListener(action);
button3.addActionListener(action);
button4.addActionListener(action);
// 设置窗口大小不可改变,容器大小,与屏幕相对位置,默认关闭方法,可见
// frame.setResizable(false);
frame.setSize(580, 490);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String con1 = field1.getText();
String con2 = field2.getText();
if (con1.equals("") || con2.equals("")) {
JOptionPane.showMessageDialog(null, "请先输入查询信息");
}
// 查询图书
List list = new ArrayList();
list = CategoryData.Select(con1, con2);
field3.setText(list.get(0).toString());
field4.setText(list.get(1).toString());
field5.setText(list.get(2).toString());
}
//借书
if (e.getSource() == button2) {
String bookname=field1.getText();
boolean is=CategoryData.user(bookname);
if(is==false) {
JOptionPane.showMessageDialog(null, "该书已被借走");
}
if(is==true) {
String id=field3.getText();
CategoryData.borrow(Username,Integer.parseInt(id));
JOptionPane.showMessageDialog(null, "借书成功");
frame.dispose();
new User(Username);
}
}
//还书
if (e.getSource() == button3) {
String bookname=field1.getText();
boolean is=CategoryData.user(bookname);
if(is==false) {
String id=field3.getText();
boolean us=CategoryData.ruser(Username, Integer.parseInt(id));
if(us==false){
JOptionPane.showMessageDialog(null, "你未借过该书");
}
if(us==true) {
CategoryData.rt(bookname);
JOptionPane.showMessageDialog(null, "还书成功");
new User(Username);
}
}
if(is==true) {
JOptionPane.showMessageDialog(null, "该书未被借走");
}
}
if(e.getSource()==button4) {
frame.dispose();
}
}
}
}
Book:图书基本信息
package Design;
public class Book {
private int id;
private String name;
private String author;
private String des;
private int cateID;
private String user;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
public int getCateID() {
return cateID;
}
public void setCateID(int cateID) {
this.cateID = cateID;
}
@Override
public String toString() {
return "id="+id+",BookName="+name+",author="+author+",des="+des+",CateID="+cateID+",user="+user;
}
}
jdbc.properties:加载数据库的配置文件
注意:需先在mysql自行创建三张表,分别存放图书信息,用户和管理员信息