基于Java Swing 的图书管理系统

本系统是基于Java和MySQL开发的一个简易的图书管理系统,其中数据库表如下:

drop database books;
create database books char set utf8;
use books;
create table user(
    id int auto_increment primary key ,
    username varchar(30) not null ,
    password varchar(30) not null,
    type varchar(30) not null

);
insert into user(username, password,type) value ('admin','admin','系统管理员');
select * from user where username = 'admin' and password = 'admin';
select *from user;

create table booktype(
    id varchar(60) not null primary key ,
    typename varchar(255) not null ,
    typeremark text
);
insert into booktype values ('BK0001','科技类','科技文化类型');
insert into booktype values ('BK0002','文献类','文化类型');
select * from booktype;

create table book(
    id varchar(30) not null primary key ,
    bookname varchar(255) not null ,
    autor varchar(255) not null ,
    sex char(2) not null ,
    price float not null ,
    type varchar(255) not null ,
    remark text
);
alter table book add constraint type foreign key (type) references booktype(typename);
insert into book(id, bookname, autor, sex, price, type, remark)VALUES ('1','软件工程','admin','男',90.9,'科技类','软件工程是一门工程化学科');

User实体类:

package com.book.util;

public class User {
    private Integer id;
    private String username;
    private String password;
    private String type;

    public User(Integer id, String username, String password, String type) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.type = type;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", type='" + type + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

BookType实体类:

package com.book.util;

public class BookType {
    private String id;
    private String typename;
    private String typeremark;
    public BookType(){

    }
    @Override
    public String toString() {
        return "BookType{" +
                "id='" + id + '\'' +
                ", typename='" + typename + '\'' +
                ", typeremark='" + typeremark + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public BookType(String id,String typename, String typeremark) {
        this.id = id;
        this.typename = typename;
        this.typeremark = typeremark;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTypename() {
        return typename;
    }

    public void setTypename(String typename) {
        this.typename = typename;
    }

    public String getTyperemark() {
        return typeremark;
    }

    public void setTyperemark(String typeremark) {
        this.typeremark = typeremark;
    }
}

Book实体类:

package com.book.util;

public class Book {
    private String id;
    private String bookname;
    private String autor;
    private String sex;
    private Float price;
    private String type;
    private String remark;

    public Book() {

    }

    @Override
    public String toString() {
        return "Book{" +
                "id='" + id + '\'' +
                ", bookname='" + bookname + '\'' +
                ", autor='" + autor + '\'' +
                ", sex='" + sex + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", remark='" + remark + '\'' +
                '}';
    }

    public Book(String id, String bookname, String autor, String sex, Float price, String type, String remark) {
        this.id = id;
        this.bookname = bookname;
        this.autor = autor;
        this.sex = sex;
        this.price = price;
        this.type = type;
        this.remark = remark;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public String getAutor() {
        return autor;
    }

    public void setAutor(String autor) {
        this.autor = autor;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

登录
LoginJFrame类:

package com.book.ui;

import com.book.dao.DB;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class LoginJFrame extends JFrame{
    public LoginJFrame(String name,String pwd){
        //1.设置标题
        this.setTitle("登录");
        setIconImage(Toolkit.getDefaultToolkit().getImage(RegisterJFrame.class.getResource("/images/LOGO1.PNG")));
        //事件机制,点击,双击、长按、触摸、 动
        //2.设置jframe位置和宽高
        this.setBounds(200,200,600,600);
        //界面关闭后,查询处理
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置不允许改变界面大小
        this.setResizable(false);
        //3.创建面板
        JPanel jPanel = new JPanel();
        //4.把面板添加到JFrame
        this.add(jPanel);

        //设置布局,采用绝对布局
        jPanel.setLayout(null);
        //设置页面背景

        //开始绘画组件
        JLabel jLabel = new JLabel("登录页面");
        jLabel.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/bookManager.png")));

        jLabel.setFont(new Font("宋体", Font.PLAIN, 26));
        jLabel.setBounds(230,100,180,30);
        //把组件添加到jPanel中
        jPanel.add(jLabel);


        //开始绘画用 户 名:组件
        JLabel username = new JLabel("用 户 名:");
       // username.setForeground(new Color(245, 255, 250));
        username.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/userName.png")));
        username.setBounds(120,150,80,30);
        jPanel.add(username);

        //开始绘画登录密码组件
        JLabel password = new JLabel("密     码:");
        //password.setForeground(new Color(245, 255, 250));
        password.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/password.png")));
        password.setBounds(120,200,80,30);
        jPanel.add(password);

        //输入框
        JTextField usernameInput = new JTextField();
        usernameInput.setBounds(200,150,200,30);

        jPanel.add(usernameInput);
        usernameInput.setText(name);
        //密码框
        JPasswordField passwordInput = new JPasswordField();

        passwordInput.setBounds(200,200,200,30);
        jPanel.add(passwordInput);
        passwordInput.setText(pwd);
        //登录
        JButton loginbutton = new JButton("登录");
        loginbutton.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/login.png")));
        loginbutton.setForeground(new Color(0, 128, 0));
        loginbutton.setBounds(160,250,80,30);
        jPanel.add(loginbutton);

        //重置
        JButton registbutton = new JButton("重置");
        registbutton.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/edit.png")));
        registbutton.setBounds(260,250,80,30);
        registbutton.setForeground(new Color(0, 128, 0));
        jPanel.add(registbutton);

        //注册
        JButton zcbutton = new JButton("注册");
        zcbutton.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/exit.png")));
        zcbutton.setBounds(360,250,80,30);
        zcbutton.setForeground(new Color(0, 128, 0));
        jPanel.add(zcbutton);
        //注册
        zcbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //页面跳转
                new RegisterJFrame().setVisible(true);
                //页面销毁
                LoginJFrame.this.dispose();
            }
        });
        //点击登录处理
        loginbutton.addActionListener(new ActionListener() {
            //用户点击按钮时,会自动调用此方法
            @Override
            public void actionPerformed(ActionEvent e) {
                //消息提示框
                /*
                * 1:获取用户名和密码
                * 2:拿着用户名和密码和服务器里数据进行对比
                *   a:登录成功 弹出对话框登录成功,跳转到主页面,当前页面关闭
                *   b:登录失败  弹出对话框,提示登录失败*/
                String username = usernameInput.getText();
                String password = passwordInput.getText();
                DB db = new DB();
                try {
                    if (db.userLogin(username,password)){
                        //登录成功
                        JOptionPane.showMessageDialog(null,"恭喜你登录成功!");
                        MainJFrame frames = new  MainJFrame();
                        //页面跳转
                        frames.setVisible(true);
                        //注销
                        LoginJFrame.this.dispose();
                    }else {
                        JOptionPane.showMessageDialog(null,"登录失败!请检查用户名或密码是否错误...");
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }

            }
        });

        //重置操作
        registbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                usernameInput.setText("");
                passwordInput.setText("");
            }
        });
        //显示画面内容
        this.setVisible(true);

    }

    public static void main(String[] args) {
       new LoginJFrame("","");

    }

}

登录

注册
RegisterJFrame类:

package com.book.ui;
import com.book.dao.DB;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.sql.SQLException;

public class RegisterJFrame extends JFrame{
    private JTextField textField;
    private JPasswordField passwordField;
    private JPasswordField passwordField_1;
    public RegisterJFrame() {
        this.setBounds(200,200,600,600);
        //界面关闭后,查询处理
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置不允许改变界面大小
        this.setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage(RegisterJFrame.class.getResource("/images/LOGO1.PNG")));
        getContentPane().setEnabled(false);
        setResizable(false);
        setAlwaysOnTop(true);
        getContentPane().setFont(new Font("宋体", Font.PLAIN, 26));
        getContentPane().setForeground(Color.WHITE);
        getContentPane().setBackground(new Color(0, 128, 0));
        setBackground(Color.MAGENTA);
        setTitle("用户注册");
        getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("欢迎注册");
        lblNewLabel.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/bookManager.png")));
        lblNewLabel.setForeground(new Color(248, 248, 255));
        lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 26));
        lblNewLabel.setBounds(230, 38, 197, 39);
        getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("用 户 名:");
        lblNewLabel_1.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/userName.png")));
        lblNewLabel_1.setForeground(new Color(245, 255, 250));
        lblNewLabel_1.setBackground(new Color(248, 248, 255));
        lblNewLabel_1.setBounds(148, 118, 104, 18);
        getContentPane().add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("密    码:");
        lblNewLabel_2.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/password.png")));
        lblNewLabel_2.setForeground(new Color(245, 255, 250));
        lblNewLabel_2.setBounds(148, 149, 114, 33);
        getContentPane().add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel("确认密码:");
        lblNewLabel_3.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/password.png")));
        lblNewLabel_3.setForeground(new Color(245, 255, 250));
        lblNewLabel_3.setBounds(148, 189, 104, 24);
        getContentPane().add(lblNewLabel_3);

        textField = new JTextField();
        textField.setBounds(266, 115, 161, 24);
        getContentPane().add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(266, 153, 161, 24);
        getContentPane().add(passwordField);

        passwordField_1 = new JPasswordField();
        passwordField_1.setBounds(266, 189, 161, 24);
        getContentPane().add(passwordField_1);

        JComboBox comboBox = new JComboBox();
        comboBox.setForeground(new Color(0, 128, 0));
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"请选择", "普通用户", "系统管理员"}));
        comboBox.setBounds(266, 226, 161, 24);
        getContentPane().add(comboBox);

        JButton btnNewButton = new JButton("注册");
        btnNewButton.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/login.png")));
        btnNewButton.setForeground(new Color(0, 128, 0));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //用户什么都没有输入
                if(textField.getText().equals("")||passwordField.getText().equals("")||passwordField_1.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "用户名或密码不能为空!");
                }else {
                    if(passwordField.getText().equals(passwordField_1.getText())) {

                        DB db = new DB();
                        String username = textField.getText();
                        String password = passwordField.getText();
                        String type = (String) comboBox.getSelectedItem();
                        System.out.println(type);
                        try {
                            if(db.userAdd(username,password,type)){
                                JOptionPane.showMessageDialog(null, "注册成功!");
                                //页面跳转
                                new LoginJFrame(username,password).setVisible(true);
                                //页面销毁
                                RegisterJFrame.this.dispose();
                            }

                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        }

                    }else {
                        JOptionPane.showMessageDialog(null, "二次输入密码不一致!");
                    }
                }
            }
        });
        btnNewButton.setBounds(160, 281, 95, 27);
        getContentPane().add(btnNewButton);



        JLabel lblNewLabel_4 = new JLabel("角    色:");
        lblNewLabel_4.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/edit.png")));
        lblNewLabel_4.setForeground(new Color(245, 255, 250));
        lblNewLabel_4.setBounds(148, 229, 104, 18);
        getContentPane().add(lblNewLabel_4);

        JButton btnNewButton_1 = new JButton("重置");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //用户重置
                textField.setText("");
                passwordField.setText("");
                passwordField_1.setText("");
            }
        });
        btnNewButton_1.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/reset.png")));
        btnNewButton_1.setForeground(new Color(0, 128, 128));
        btnNewButton_1.setBounds(265, 281, 95, 27);
        getContentPane().add(btnNewButton_1);

        JButton login = new JButton("登录");
        login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //返回登录界面
               new LoginJFrame("","").setVisible(true);
               //关闭窗口
               RegisterJFrame.this.dispose();
            }
        });
        login.setIcon(new ImageIcon(RegisterJFrame.class.getResource("/images/login.png")));
        login.setForeground(new Color(0, 128, 128));
        login.setBounds(370, 281, 95, 27);
        getContentPane().add(login);
        //显示画面内容
        this.setVisible(true);
    }
    public static void main(String [] args) {
        new RegisterJFrame();
    }
}

MainJFrame类:

package com.book.ui;

import javax.swing.*;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.EventQueue;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import javax.swing.border.EmptyBorder;

public class MainJFrame extends JFrame{

	private JPanel contentPane;

	public MainJFrame() {
		//设置不允许改变界面大小
		this.setResizable(false);

		setIconImage(Toolkit.getDefaultToolkit().getImage(MainJFrame.class.getResource("/images/LOGO1.PNG")));
		setTitle("图书管理系统主界面");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 665, 596);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("图书类别管理");
		//鼠标点击事件,自动执行此方法
		mnNewMenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				//直接跳转图书管理系统主页面
				new BookManagerSystemJFrame().setVisible(true);
			}
		});
		mnNewMenu.setIcon(new ImageIcon(MainJFrame.class.getResource("/images/bookTypeManager.png")));
		menuBar.add(mnNewMenu);

		JMenu mnNewMenu_1 = new JMenu("图书管理");
		mnNewMenu_1.setIcon(new ImageIcon(MainJFrame.class.getResource("/images/bookManager.png")));
		menuBar.add(mnNewMenu_1);
		mnNewMenu_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					new BookManagerJFrame().setVisible(true);
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				} catch (ClassNotFoundException ex) {
					throw new RuntimeException(ex);
				}
			}
		});

		JMenu mnNewMenu_2 = new JMenu("关于");
		mnNewMenu_2.setIcon(new ImageIcon(MainJFrame.class.getResource("/images/about.png")));
		menuBar.add(mnNewMenu_2);
		//关于界面信息展示
		mnNewMenu_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new BookAboutJFrame().setVisible(true);

			}
		});

		JMenu mnNewMenu_3 = new JMenu("安全退出");
		mnNewMenu_3.setIcon(new ImageIcon(MainJFrame.class.getResource("/images/exit.png")));
		menuBar.add(mnNewMenu_3);
		//退出登录
		mnNewMenu_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JOptionPane.showMessageDialog(null,"确定要退出系统吗?","温馨提示:",1);
				new LoginJFrame("","").setVisible(true);
				MainJFrame.this.dispose();
			}
		});

		contentPane = new JPanel();
		contentPane.setBackground(new Color(0, 128, 0));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.setContentPane(contentPane);
		contentPane.setLayout(null);

		//显示画面内容
		this.setVisible(true);
	}
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainJFrame frame = new MainJFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}

BookManagerSystemJFrame类:

package com.book.ui;
import com.book.dao.DB;
import com.book.util.BookType;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class BookManagerSystemJFrame extends JFrame{
	private ArrayList<BookType> list = new ArrayList<>();
	DB db = new DB();
	private JPanel contentPane;
	private JTable table;
	private DefaultTableModel dtm = new DefaultTableModel();
	private JLabel lblNewLabel;
	private JTextField textField;
	private JButton btnNewButton;
	private JLabel lblNewLabel_1;
	private JTextField textField_1;
	private JLabel lblNewLabel_2;
	private JTextField textField_2;
	private JLabel lblNewLabel_3;
	private JTextField textField_3;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JButton btnNewButton_3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookManagerSystemJFrame frame = new BookManagerSystemJFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookManagerSystemJFrame() {
		//设置不允许改变界面大小
		this.setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(BookManagerSystemJFrame.class.getResource("/images/LOGO1.PNG")));
		setTitle("图书管理系统主页面");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 518, 567);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		table = new JTable();// 创建表格
		table.setModel(dtm);// 设置表格的模式,采用的是默认格式
		dtm.addColumn("编号");
		dtm.addColumn("图书类别名称");
		dtm.addColumn("图书类别描述");
		JScrollPane scrollPane = new JScrollPane(table); // 滚动面板
		scrollPane.setBounds(15, 97, 466, 147);
		contentPane.add(scrollPane);

		lblNewLabel = new JLabel("图书类别名称");
		lblNewLabel.setBounds(49, 45, 101, 26);
		contentPane.add(lblNewLabel);

		textField = new JTextField();
		textField.setBounds(164, 46, 185, 24);
		contentPane.add(textField);
		textField.setColumns(10);

		/*系统鼠标点击,进行输出桌面*/
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int row = table.getSelectedRow();
				textField_1.setText((String) table.getValueAt(row,0));;
				textField_2.setText((String) table.getValueAt(row,1));
				textField_3.setText((String) table.getValueAt(row,2));
			}
		});

		/*查询操作*/
		btnNewButton = new JButton("查询");
		btnNewButton.setIcon(new ImageIcon(BookManagerSystemJFrame.class.getResource("/images/search.png")));
		btnNewButton.setBounds(363, 45, 113, 27);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String str = textField.getText();
				if (str.equals("")){
					try {
						showAll();
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					} catch (ClassNotFoundException ex) {
						throw new RuntimeException(ex);
					}
				}else {
					try {
						int count = dtm.getRowCount();
						for (int i=0;i<count;i++){
							dtm.removeRow(0);
						}
						list = (ArrayList<BookType>) db.likeQueryBookType(str);
						for (int i=0;i<list.size();i++){
							Vector vec = new Vector();
							vec.add(list.get(i).getId());
							vec.add(list.get(i).getTypename());
							vec.add(list.get(i).getTyperemark());
							System.out.println(list.get(i).getId());
							dtm.addRow(vec);
						}

					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					} catch (ClassNotFoundException ex) {
						throw new RuntimeException(ex);
					}
				}
			}
		});

		lblNewLabel_1 = new JLabel("编号");
		lblNewLabel_1.setBounds(14, 291, 72, 18);
		contentPane.add(lblNewLabel_1);

		textField_1 = new JTextField();
		textField_1.setBounds(64, 288, 86, 24);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		lblNewLabel_2 = new JLabel("图书类别名称");
		lblNewLabel_2.setBounds(204, 291, 101, 18);
		contentPane.add(lblNewLabel_2);

		textField_2 = new JTextField();
		textField_2.setBounds(310, 288, 166, 24);
		contentPane.add(textField_2);
		textField_2.setColumns(10);

		lblNewLabel_3 = new JLabel("描述");
		lblNewLabel_3.setBounds(15, 352, 39, 18);
		contentPane.add(lblNewLabel_3);

		textField_3 = new JTextField();
		textField_3.setBounds(64, 349, 417, 70);
		contentPane.add(textField_3);
		textField_3.setColumns(10);

		btnNewButton_1 = new JButton("添加");
		btnNewButton_1.setIcon(new ImageIcon(BookManagerSystemJFrame.class.getResource("/images/add.png")));
		btnNewButton_1.setBounds(93, 449, 113, 27);
		contentPane.add(btnNewButton_1);

		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String id = textField_1.getText();
				String booktype = textField_2.getText();
				String bookmark = textField_3.getText();
				if (id.equals("")||booktype.equals("")||bookmark.equals("")){
					JOptionPane.showMessageDialog(null,"输入图书类别信息不能为空!");
				}else {
					BookType bookType = new BookType(id,booktype,bookmark);
					try {
						if(db.addBookType(bookType)){
							JOptionPane.showMessageDialog(null,"添加图书类别信息成功!");
							showAll();
						}else {
							JOptionPane.showMessageDialog(null,"添加图书类别信息失败!");
						}
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					} catch (ClassNotFoundException ex) {
						throw new RuntimeException(ex);
					}
				}
			}
		});

		btnNewButton_2 = new JButton("修改");
		btnNewButton_2.setIcon(new ImageIcon(BookManagerSystemJFrame.class.getResource("/images/modify.png")));
		btnNewButton_2.setBounds(220, 449, 113, 27);
		contentPane.add(btnNewButton_2);
		btnNewButton_2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if (row == -1){
					JOptionPane.showMessageDialog(null,"请选择要修改的行!");
				}else{
					String i = (String) table.getValueAt(row,0);

					String id = textField_1.getText();
					String booktype = textField_2.getText();
					String bookremark = textField_3.getText();
					if (!id.equals("")&&!booktype.equals("")) {
						BookType type = new BookType(id, booktype, bookremark);
						try {
							if (db.UpdateBookTypeOne(type,i)){
								JOptionPane.showMessageDialog(null,"修改数据成功!");
								showAll();
							}
						} catch (SQLException ex) {
							throw new RuntimeException(ex);
						} catch (ClassNotFoundException ex) {
							throw new RuntimeException(ex);
						}

					}
				}
			}
		});

		btnNewButton_3 = new JButton("删除");
		btnNewButton_3.setIcon(new ImageIcon(BookManagerSystemJFrame.class.getResource("/images/delete.png")));
		btnNewButton_3.setBounds(347, 449, 113, 27);
		contentPane.add(btnNewButton_3);
		btnNewButton_3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if (row==-1){
					JOptionPane.showMessageDialog(null,"请选择要删除的行!");
				}else {
					String id = (String) table.getValueAt(row,0);
					try {
						if (db.deleteBookTypeOne(id)){
							JOptionPane.showMessageDialog(null,"删除图书类别信息成功!");
							showAll();
						}
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					} catch (ClassNotFoundException ex) {
						throw new RuntimeException(ex);
					}
				}
			}
		});
	}

	public void showAll() throws SQLException, ClassNotFoundException {
		list = (ArrayList<BookType>) db.allQueryBoolType();
		int count = dtm.getRowCount();
		for (int i=0;i<count;i++){
			dtm.removeRow(0);
		}
		for (BookType books:list){
			Vector vec = new Vector();
			vec.add(books.getId());
			vec.add(books.getTypename());
			vec.add(books.getTyperemark());
			dtm.addRow(vec);
		}
		textField_1.setText("");
		textField_2.setText("");
		textField_3.setText("");
	}
}

BookManagerJFrame类:

package com.book.ui;



import com.book.dao.DB;
import com.book.util.Book;
import com.book.util.BookType;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class BookManagerJFrame extends JFrame {
    private JPanel contentPane;
    private JTable table;
    private DefaultTableModel dtm = new DefaultTableModel();
    private JTextField textField;
    private JTextField textField_1;
    private JComboBox comboBox;
    private JButton btnNewButton;
    private JLabel lblNewLabel_3;
    private JTextField textField_2;
    private JLabel lblNewLabel_4;
    private JTextField textField_3;
    private JLabel lblNewLabel_5;
    private JTextField textField_4;
    private JLabel lblNewLabel_6;
    private JTextField textField_6;
    private JLabel lblNewLabel_7;
    private JTextField textField_5;
    private JLabel lblNewLabel_8;
    private JLabel lblNewLabel_9;
    private JButton btnNewButton_1;
    private JButton btnNewButton_2;
    private JButton btnNewButton_3;
    private JPanel jp1;
    private JPanel jp2;
    DB db = new DB();
    List<Book> list = new ArrayList<>();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BookManagerJFrame frame = new BookManagerJFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public BookManagerJFrame() throws SQLException, ClassNotFoundException {
        //设置不允许改变界面大小
        this.setResizable(false);
        //创建动态字符串数组,提取文件信息
        ArrayList<String> str = new ArrayList<String>();
        List<BookType> bt = new ArrayList<BookType>();
        bt = db.allQueryBoolType();
        str.add("请选择");
        for(int i=0;i<bt.size();i++){
            str.add(bt.get(i).getTypename());
        }
        setIconImage(Toolkit.getDefaultToolkit().getImage(BookManagerJFrame.class.getResource("/images/LOGO1.PNG")));
        setTitle("图书管理");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 800, 720);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        table = new JTable();// 创建表格
        table.setModel(dtm);// 设置表格的模式,采用的是默认格式
        dtm.addColumn("编号");
        dtm.addColumn("图书名称");
        dtm.addColumn("作者");
        dtm.addColumn("作者性别");
        dtm.addColumn("价格");
        dtm.addColumn("图书类别");
        dtm.addColumn("图书描述");
        JScrollPane scrollPane = new JScrollPane(table); // 滚动面板
        scrollPane.setBounds(15, 97, 753, 243);
        contentPane.add(scrollPane);

        JLabel lblNewLabel = new JLabel("图书名称:");
        lblNewLabel.setBounds(32, 45, 94, 18);
        contentPane.add(lblNewLabel);

        textField = new JTextField();
        textField.setBounds(107, 42, 120, 24);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("作者:");
        lblNewLabel_1.setBounds(241, 45, 45, 18);
        contentPane.add(lblNewLabel_1);

        textField_1 = new JTextField();
        textField_1.setBounds(289, 42, 86, 24);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("图书类别:");
        lblNewLabel_2.setBounds(395, 45, 109, 18);
        contentPane.add(lblNewLabel_2);

        comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(str.toArray()));
        comboBox.setBounds(478, 42, 106, 24);
        contentPane.add(comboBox);

        btnNewButton = new JButton("搜索");
        btnNewButton.setIcon(new ImageIcon(BookManagerJFrame.class.getResource("/images/search.png")));
        btnNewButton.setBounds(599, 41, 113, 27);
        contentPane.add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String bookname = textField.getText();
                String autor = textField_1.getText();
                String type = (String) comboBox.getSelectedItem();
                if (bookname.equals("")&&autor.equals("")&&type.equals("请选择")){
                    try {
                        showAll();
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                }else {
                    try {
                        list =  db.linkSelectBook(type,bookname,autor);
                        int count = dtm.getRowCount();
                        for (int i=0;i<count;i++){
                            dtm.removeRow(0);
                        }
                        for (Book books:list){
                            Vector vec = new Vector();
                            vec.add(books.getId());
                            vec.add(books.getBookname());
                            vec.add(books.getAutor());
                            vec.add(books.getSex());
                            vec.add(books.getPrice());
                            vec.add(books.getType());
                            vec.add(books.getRemark());
                            dtm.addRow(vec);
                        }
                        textField_2.setText("");
                        textField_3.setText("");
                        textField_4.setText("");
                        textField_5.setText("");
                        textField_6.setText("");
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        lblNewLabel_3 = new JLabel("编号:");
        lblNewLabel_3.setBounds(37, 436, 72, 18);
        contentPane.add(lblNewLabel_3);

        textField_2 = new JTextField();
        textField_2.setBounds(95, 433, 147, 24);
        contentPane.add(textField_2);
        textField_2.setColumns(10);

        lblNewLabel_4 = new JLabel("价格:");
        lblNewLabel_4.setBounds(37, 481, 45, 18);
        contentPane.add(lblNewLabel_4);

        textField_3 = new JTextField();
        textField_3.setBounds(95, 478, 147, 24);
        contentPane.add(textField_3);
        textField_3.setColumns(10);

        lblNewLabel_5 = new JLabel("图书描述:");
        lblNewLabel_5.setBounds(37, 520, 89, 18);
        contentPane.add(lblNewLabel_5);

        textField_4 = new JTextField();
        textField_4.setBounds(130, 517, 622, 68);
        contentPane.add(textField_4);
        textField_4.setColumns(10);

        lblNewLabel_6 = new JLabel("图书名称:");
        lblNewLabel_6.setBounds(299, 436, 75, 18);
        contentPane.add(lblNewLabel_6);

        textField_6 = new JTextField();
        textField_6.setColumns(10);
        textField_6.setBounds(388, 433, 147, 24);
        contentPane.add(textField_6);

        lblNewLabel_7 = new JLabel("作者:");
        lblNewLabel_7.setBounds(299, 481, 72, 18);
        contentPane.add(lblNewLabel_7);

        textField_5 = new JTextField();
        textField_5.setBounds(388, 478, 147, 24);
        contentPane.add(textField_5);
        textField_5.setColumns(10);

        lblNewLabel_8 = new JLabel("作者性别:");
        lblNewLabel_8.setBounds(566, 436, 86, 18);
        contentPane.add(lblNewLabel_8);

        lblNewLabel_9 = new JLabel("图书类别");
        lblNewLabel_9.setBounds(566, 481, 86, 18);
        contentPane.add(lblNewLabel_9);

        JRadioButton rdbtnNewRadioButton = new JRadioButton("男");
        rdbtnNewRadioButton.setBounds(640, 436, 45, 23);
        contentPane.add(rdbtnNewRadioButton);

        JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("女");
        rdbtnNewRadioButton_1.setBounds(691, 436, 45, 23);
        contentPane.add(rdbtnNewRadioButton_1);

        JComboBox comboBox_1 = new JComboBox();
        //动态字符串数组
        comboBox_1.setModel(new DefaultComboBoxModel(str.toArray()));
        comboBox_1.setBounds(648, 478, 104, 24);
        contentPane.add(comboBox_1);

        btnNewButton_1 = new JButton("添加");
        btnNewButton_1.setIcon(new ImageIcon(BookManagerJFrame.class.getResource("/images/add.png")));
        btnNewButton_1.setBounds(229, 613, 113, 27);
        contentPane.add(btnNewButton_1);
        btnNewButton_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = textField_2.getText();
                String bookname=textField_6.getText();
                String autor = textField_5.getText();
                String price = textField_3.getText();
                String remark = textField_4.getText();
                String sex = "";
                String type = (String) comboBox_1.getSelectedItem();
                if(id.equals("")||bookname.equals("")||autor.equals("") ||price.equals("")||type.equals("请选择")){
                    JOptionPane.showMessageDialog(null,"输入信息不能为空!");
                }else{
                    if(rdbtnNewRadioButton.isSelected()){
                        sex="男";
                    }else {
                        sex="女";
                    }
                    Book b = new Book(id,bookname,autor,sex,Float.valueOf(price),type,remark);
                    try {
                        if (db.addOneBook(b)){
                            JOptionPane.showMessageDialog(null,"添加图书信息成功!");
                            showAll();
                            rdbtnNewRadioButton.setSelected(true);
                            if (comboBox_1.getItemCount()>0){
                                comboBox_1.setSelectedIndex(0);
                            }
                        }
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        btnNewButton_2 = new JButton("修改");
        btnNewButton_2.setIcon(new ImageIcon(BookManagerJFrame.class.getResource("/images/modify.png")));
        btnNewButton_2.setBounds(376, 613, 113, 27);
        contentPane.add(btnNewButton_2);
        /*系统鼠标点击,进行输出*/
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = table.getSelectedRow();
                textField_2.setText((String) table.getValueAt(row,0));;
                textField_6.setText((String) table.getValueAt(row,1));
                textField_5.setText((String) table.getValueAt(row,2));
                textField_3.setText(String.valueOf(table.getValueAt(row,4)));
                textField_4.setText((String) table.getValueAt(row,6));






            }
        });
        btnNewButton_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if (row == -1){
                    JOptionPane.showMessageDialog(null,"请选择要修改的行!");
                }else{
                    String i = (String) table.getValueAt(row,0);

                    String id = textField_2.getText();
                    String bookname=textField_6.getText();
                    String autor = textField_5.getText();
                    String price = textField_3.getText();
                    String remark = textField_4.getText();
                    String sex = "";
                    String type = (String) comboBox_1.getSelectedItem();
                    if (id.equals("")||bookname.equals("")||autor.equals("") ||price.equals("")||type.equals("请选择")) {
                        JOptionPane.showMessageDialog(null,"修改信息不能为空!");
                    }else {
                        if(rdbtnNewRadioButton.isSelected()){
                            sex="男";
                        }else {
                            sex="女";
                        }
                        Book b = new Book(id,bookname,autor,sex,Float.valueOf(price),type,remark);
                        try {
                            if (db.updateOneBook(b,i)){
                                JOptionPane.showMessageDialog(null,"修改图书信息成功!");
                                showAll();
                                rdbtnNewRadioButton.setSelected(true);
                                if (comboBox_1.getItemCount()>0){
                                    comboBox_1.setSelectedIndex(0);
                                }
                            }
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        } catch (ClassNotFoundException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            }
        });

        btnNewButton_3 = new JButton("删除");
        btnNewButton_3.setIcon(new ImageIcon(BookManagerJFrame.class.getResource("/images/delete.png")));
        btnNewButton_3.setBounds(521, 613, 113, 27);
        contentPane.add(btnNewButton_3);
        btnNewButton_3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if (row==-1){
                    JOptionPane.showMessageDialog(null,"请选择要删除的行!");
                }else {
                    String id = (String) table.getValueAt(row,0);
                    try {
                        if (db.deleteOneBook(id)){
                            JOptionPane.showMessageDialog(null,"删除图书信息成功!");
                            showAll();
                        }
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        });

        /*搜索条件*/
        jp1 = new JPanel();
        jp1.setBorder(new TitledBorder(null,"搜索条件",TitledBorder.LEADING,TitledBorder.TOP,null,null));
        jp1.setBounds(15, 13, 753, 73);
        contentPane.add(jp1);

        jp2 = new JPanel();
        jp2.setBorder(new TitledBorder(null,"表单操作",TitledBorder.LEADING,TitledBorder.TOP,null,null));
        jp2.setBounds(15, 386, 753, 274);
        contentPane.add(jp2);
    }
    public void showAll() throws SQLException, ClassNotFoundException {
        list =  db.allSelectBook();
        int count = dtm.getRowCount();
        for (int i=0;i<count;i++){
            dtm.removeRow(0);
        }
        for (Book books:list){
            Vector vec = new Vector();
            vec.add(books.getId());
            vec.add(books.getBookname());
            vec.add(books.getAutor());
            vec.add(books.getSex());
            vec.add(books.getPrice());
            vec.add(books.getType());
            vec.add(books.getRemark());
            dtm.addRow(vec);
        }
        textField_2.setText("");
        textField_3.setText("");
        textField_4.setText("");
        textField_5.setText("");
        textField_6.setText("");
    }
}

连接数据库代码
DB类:

package com.book.dao;

import com.book.util.Book;
import com.book.util.BookType;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DB {
    private String name = "root";
    private String password = "123456";
    private String URL = "jdbc:mysql://localhost:3306/books?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
    private Connection conn = null;
    private Statement st = null;
    private ResultSet rs = null;
    private PreparedStatement pstm;
    private String Driver = "com.mysql.jdbc.Driver";

    public Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(Driver);
        conn = DriverManager.getConnection(URL,name,password);
        if (conn!=null){
            System.out.println("连接数据库成功!");
        }
        return conn;
    }
    /*用户登录查询记录*/
    public boolean userLogin(String name,String password) throws SQLException, ClassNotFoundException {
        String sql = "select * from user where username = ? and password = ?";
        boolean flag = false;
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,name);
        pstm.setObject(2,password);
        rs = pstm.executeQuery();
        if (rs.next()){
            flag=true;
            System.out.println(rs.next());
            System.out.println("登录成功");
        }
        return flag;
    }

    //用户注册
    public boolean userAdd(String name, String password, String type) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "insert into user(username, password,type) value (?,?,?)";
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,name);
        pstm.setObject(2,password);
        pstm.setObject(3,type);
        int r = pstm.executeUpdate();
        if (r!=0){
            flag=true;
            System.out.println("注册成功!");
        }
        return flag;
    }

    //添加图书类别信息
    public boolean addBookType(BookType bookType) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "insert into booktype value (?,?,?)";
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,bookType.getId());
        pstm.setObject(2,bookType.getTypename());
        pstm.setObject(3,bookType.getTyperemark());
        int r = pstm.executeUpdate();
        if (r!=0){
            flag=true;
            System.out.println("添加图书类别信息成功!");
        }
        return flag;
    }

    //模糊查询图书信息类型
    public List<BookType> likeQueryBookType(String typeString) throws SQLException, ClassNotFoundException {
        typeString = "%"+typeString+"%";
        List<BookType>list = new ArrayList<>();
        BookType bookType = null;
        String sql = "select * from booktype where typename like ?";
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,typeString);
        rs = pstm.executeQuery();
        while (rs.next()){
            bookType = new BookType();
            bookType.setId(rs.getString("id"));
            bookType.setTypename(rs.getString("typename"));
            bookType.setTyperemark(rs.getString("typeremark"));
            list.add(bookType);
            System.out.println(bookType.toString());
        }
       return list;
    }

    //查询图书类别全部信息
    public List<BookType> allQueryBoolType() throws SQLException, ClassNotFoundException {
        List<BookType>list = new ArrayList<>();
        BookType bookType = null;
        String sql = "select * from booktype";
        pstm = this.getConnection().prepareStatement(sql);
        rs = pstm.executeQuery();
        while (rs.next()){
            bookType = new BookType();
            bookType.setId(rs.getString("id"));
            bookType.setTypename(rs.getString("typename"));
            bookType.setTyperemark(rs.getString("typeremark"));
            list.add(bookType);
            System.out.println(bookType.toString());
        }
        return list;
    }
    /*删除图书类别*/
    public boolean deleteBookTypeOne(String id) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "delete  from booktype where id = ?";
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,id);
        int r = pstm.executeUpdate();
        if (r!=0){
            flag=true;
            System.out.println("删除图书类别信息成功!");
        }
        return flag;
    }

    /*修改图书类别*/
    public boolean UpdateBookTypeOne(BookType bookType,String id) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "update booktype set id=? , typename=? , typeremark=? where id=?";
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,bookType.getId());
        pstm.setObject(2,bookType.getTypename());
        pstm.setObject(3,bookType.getTyperemark());
        pstm.setObject(4,id);
        int r = pstm.executeUpdate();
        if (r!=0){
            flag=true;
            System.out.println("修改图书类别信息成功!");
        }
        return flag;
    }

    /*查询图书信息*/
    public List<Book> allSelectBook() throws SQLException, ClassNotFoundException {
        String sql = "select * from book";
        List<Book>list = new ArrayList<>();
        Book book = null;
        pstm = this.getConnection().prepareStatement(sql);
        rs = pstm.executeQuery();
        while (rs.next()){
            book = new Book();
            book.setId(rs.getString("id"));
            book.setBookname(rs.getString("bookname"));
            book.setAutor(rs.getString("autor"));
            book.setSex(rs.getString("sex"));
            book.setPrice(rs.getFloat("price"));
            book.setType(rs.getString("type"));
            book.setRemark(rs.getString("remark"));
            list.add(book);
            System.out.println(book.toString());
        }
        return list;

    }
    /*模糊查询*/
    public List<Book> linkSelectBook(String type,String bookname,String autor) throws SQLException, ClassNotFoundException {
        bookname = "%"+bookname+"%";
        autor = "%"+autor+"%";
        //type = "%"+type+"%";
        String sql = "select * from book where type = ? and bookname like ? and autor like ?";
        List<Book>list = new ArrayList<>();
        Book book = null;
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,type);
        pstm.setObject(2,bookname);
        pstm.setObject(3,autor);
        rs = pstm.executeQuery();
        while (rs.next()){
            book = new Book();
            book.setId(rs.getString("id"));
            book.setBookname(rs.getString("bookname"));
            book.setAutor(rs.getString("autor"));
            book.setSex(rs.getString("sex"));
            book.setPrice(rs.getFloat("price"));
            book.setType(rs.getString("type"));
            book.setRemark(rs.getString("remark"));
            list.add(book);
            System.out.println(book.toString());
        }
        return list;
    }
    /*修改图书信息*/
    public boolean updateOneBook(Book book,String id) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "update book set id=? , bookname=? , autor=? ,sex=? ,price=? ,type=? ,remark=? where id=?";
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,book.getId());
        pstm.setObject(2,book.getBookname());
        pstm.setObject(3,book.getAutor());
        pstm.setObject(4,book.getSex());
        pstm.setObject(5,book.getPrice());
        pstm.setObject(6,book.getType());
        pstm.setObject(7,book.getRemark());
        pstm.setObject(8,id);
        int r = pstm.executeUpdate();
        if (r!=0){
            flag=true;
            System.out.println("修改图书信息成功!");
        }
        return flag;
    }
    /*删除图书信息*/
    public boolean deleteOneBook(String id) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "delete  from book where id = ?";
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,id);
        int r = pstm.executeUpdate();
        if (r!=0){
            flag=true;
            System.out.println("删除图书信息成功!");
        }
        return flag;
    }
    /*添加图书信息*/
    public boolean addOneBook(Book book) throws SQLException, ClassNotFoundException {
        boolean flag = false;
        String sql = "insert into book VALUES (?,?,?,?,?,?,?)";
        pstm = this.getConnection().prepareStatement(sql);
        pstm.setObject(1,book.getId());
        pstm.setObject(2,book.getBookname());
        pstm.setObject(3,book.getAutor());
        pstm.setObject(4,book.getSex());
        pstm.setObject(5,book.getPrice());
        pstm.setObject(6,book.getType());
        pstm.setObject(7,book.getRemark());
        int r = pstm.executeUpdate();
        if (r!=0){
            flag=true;
            System.out.println("添加图书信息成功!");
        }
        return flag;
    }
    /*关闭连接*/
    public void Close() throws SQLException {
        if (conn!=null){
            conn.close();
        }
        if (pstm!=null){
            pstm.close();
        }
        if (rs!=null){
            rs.close();
        }
    }
}

资源地址:https://download.csdn.net/download/youyi_yue/85658535

Logo

本社区面向用户介绍CSDN开发云部门内部产品使用和产品迭代功能,产品功能迭代和产品建议更透明和便捷

更多推荐