JDBC图书管理系统-vectorJH
前言
银行管理系统传送门(开源) 本文讲述了我大一耗时1个月完成的图书管理系统.我深知还有许多bug.但我并不想在java-GUI上花费更多的时间.因此我将其开源.并希望有借鉴者.完善这个系统的功能. 在此之前我想和大家分享这样三句话:
路漫漫其修远兮,吾将上下而求索。 我们都知道,风雨之后,才能见彩虹。但我们都希望能直接坐在彩虹里,他人已经为你布置好绚丽的世界。其曲弥高,其和弥寡。 成功不必有我,而功力必不唐捐!
操作设备: window11 idea2021 druid-1.2.6.jar commons-dbutils-1.7.jar mysql-connector-java-8.0.22.jar
提示:以下是本篇文章正文内容,下面案例可供参考 先给各位看客们上图:
一、MVC分包模式
2.GUI界面
1.初入界面
2.主界面
3.登录界面
4. 注册界面
5.管理员信息界面
6.管理员添加图书界面
7.管理员删除图书界面
8.图书数据界面
9.数据库信息
视图就展示这里啦,先展示部分源码.最后将给出开源链接
1.druid-1.2.6.jar 配置文件
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&rewriteBatchedStatements=true
username=root
password=123456
initialSize=10
minIdle=1
maxActive=10
maxWait=10000
timeBetweenEvictionRunsMillis=6000
minEvictableIdleTimeMillis=300000
testWhileIdle=true
testOnBorrow=true
testOnReturn=true
poolPreparedStatements=true
maxPoolPreparedStatementPerConnectionSize=20
validationQuery=select 1
filters=stat
2.Reader实例对象
package com.vector.pojo;
public class Reader {
private String ReaderId;
private String ReaderPwd;
private String ReaderTel;
private String ReaderMountBook=null;
private String Bookid=null;
private String BookName=null;
public Reader(String readerId, String readerPwd, String readerTel) {
ReaderId = readerId;
ReaderPwd = readerPwd;
ReaderTel = readerTel;
}
public Reader(String readerId, String readerPwd, String readerTel, String readerMountBook, String bookid, String bookName) {
ReaderId = readerId;
ReaderPwd = readerPwd;
ReaderTel = readerTel;
ReaderMountBook = readerMountBook;
Bookid = bookid;
BookName = bookName;
}
@Override
public String toString() {
return "Reader{" +
"ReaderId='" + ReaderId + '\'' +
", ReaderPwd='" + ReaderPwd + '\'' +
", ReaderTel='" + ReaderTel + '\'' +
", ReaderMountBook='" + ReaderMountBook + '\'' +
", Bookid='" + Bookid + '\'' +
", BookName='" + BookName + '\'' +
'}';
}
public void setReaderId(String readerId) {
ReaderId = readerId;
}
public void setReaderPwd(String readerPwd) {
ReaderPwd = readerPwd;
}
public void setReaderTel(String readerTel) {
ReaderTel = readerTel;
}
public void setReaderMountBook(String readerMountBook) {
ReaderMountBook = readerMountBook;
}
public void setBookid(String bookid) {
Bookid = bookid;
}
public void setBookName(String bookName) {
BookName = bookName;
}
public String getReaderId() {
return ReaderId;
}
public String getReaderPwd() {
return ReaderPwd;
}
public String getReaderTel() {
return ReaderTel;
}
public String getReaderMountBook() {
return ReaderMountBook;
}
public String getBookid() {
return Bookid;
}
public String getBookName() {
return BookName;
}
public Reader() {
}
}
3.管理员信息界面
package com.vector.view;
import com.vector.control.Utils;
import com.vector.control.ViewActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class AdminList extends JDialog implements ActionListener {
JPanel jPanel1, jPanel2;
JLabel label1, label2;
JTextField text1, text2, text3;
Button button1, button2, button3, button4;
public AdminList() {
this.setTitle("用户个人信息");
this.setSize(550, 350);
this.setLocationRelativeTo(null);
this.setResizable(false);
middleCon();
jPanel1.setVisible(true);
this.setVisible(true);
}
public void middleCon() {
jPanel1 = new JPanel();
jPanel1.setLayout(null);
button1 = new Button("确认");
button1.setBounds(30, 280, 60, 30);
button2 = new Button("取消");
button2.setBounds(100, 280, 60, 30);
button3 = new Button("添加图书");
button3.setBounds(170, 280, 60, 30);
button3.addActionListener(this);
button4 = new Button("删除图书");
button4.setBounds(250, 280, 60, 30);
button4.addActionListener(this);
label1 = new JLabel("管理员账号ID: ");
label1.setBounds(20, 80, 160, 30);
text1 = new JTextField();
text1.setBounds(100, 80, 160, 30);
text1.setEditable(false);
text1.setText(Utils.getAdministrator().getAdministratorId());
label2 = new JLabel("管理员姓名:");
label2.setBounds(20, 150, 160, 30);
text2 = new JTextField();
text2.setBounds(100, 140, 160, 30);
text2.setEditable(false);
text2.setText(Utils.getAdministrator().getAdministratorName());
ViewActionEvent.windowmsg(button2, this);
ViewActionEvent.windowmsg(button1);
jPanel1.add(button1);
jPanel1.add(button2);
jPanel1.add(button3);
jPanel1.add(button4);
jPanel1.add(label1);
jPanel1.add(text1);
jPanel1.add(label2);
jPanel1.add(text2);
this.add(jPanel1);
}
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "添加图书":
new addBooks();
break;
case "删除图书":
new DeleBooks();
break;
}
}
}
4 .删除数据界面
package com.vector.view;
import com.vector.control.ViewActionEvent;
import com.vector.dao.impl.BookDaoimpl;
import com.vector.pojo.Book;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
public class DeleBooks extends JDialog implements MouseListener {
JPanel jPanel;
Button button1, button2;
JScrollPane js;
JTable jTable;
DefaultTableModel tableMode;
public DeleBooks() {
this.setTitle("删除图书");
this.setSize(550, 350);
this.setLocationRelativeTo(null);
this.setResizable(false);
DeleBooksView();
jPanel.setVisible(true);
this.add(jPanel);
this.setVisible(true);
}
public void DeleBooksView() {
jPanel = new JPanel();
jPanel.setLayout(null);
button1 = new Button("确认");
button1.setBounds(40, 280, 60, 30);
button1.addMouseListener(this);
button2 = new Button("取消");
button2.setBounds(110, 280, 70, 30);
ViewActionEvent.windowmsg(button2, this);
js = new JScrollPane();
js.setBounds(0, 10, 550, 200);
Object[] clum = {"书籍名称", "编号", "数量/本", "价格/本", "书类", "所在位置"};
Object[][] data = new Object[10][6];
BookDaoimpl bookDaoimpl = new BookDaoimpl();
List<Book> list = bookDaoimpl.findAll();
for (int i = 0; i < list.size(); i++) {
data[i][0] = list.get(i).getBookName();
data[i][1] = list.get(i).getBookNum();
data[i][2] = list.get(i).getBookMany();
data[i][3] = list.get(i).getBookPrice();
data[i][4] = list.get(i).getBookClass();
data[i][5] = list.get(i).getBookPosition();
}
tableMode = new DefaultTableModel(data, clum);
jTable = new JTable(tableMode);
js.setViewportView(jTable);
jPanel.add(js);
jPanel.add(button1);
jPanel.add(button2);
this.add(jPanel);
}
@Override
public void mouseClicked(MouseEvent e) {
int selectRows = jTable.getSelectedRows().length;
TableModel tableModel = jTable.getModel();
if (selectRows == 1) {
int selectedRowIndex = jTable.getSelectedRow();
BookDaoimpl bookDaoimpl = new BookDaoimpl();
System.out.println(tableModel.getValueAt((selectedRowIndex),1));
bookDaoimpl.delete(Integer.parseInt((String) tableModel.getValueAt((selectedRowIndex),1)) );
JOptionPane.showMessageDialog(null,"删除成功!");
tableMode.removeRow(selectedRowIndex) ;
tableMode.fireTableDataChanged();
}
else return;
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
5.读者注册界面
package com.vector.view;
import com.vector.control.ViewActionEvent;
import com.vector.dao.impl.ReaderDaoimpl;
import com.vector.pojo.Reader;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ReaderRegister extends JDialog implements ActionListener {
JPanel jPanel1;
JLabel label1, label2, label3;
Button button1, button2;
JTextField text1, text3;
JPasswordField text2;
public ReaderRegister() {
this.setTitle("用户注册界面");
this.setSize(550,350);
this.setLocationRelativeTo(null);
this.setResizable(false);
middleCon();
jPanel1.setVisible(true);
this.setVisible(true);
}
public void middleCon() {
jPanel1 = new JPanel();
jPanel1.setLayout(null);
button1 = new Button("确认");
button1.setBounds(200, 240, 50, 30);
button2 = new Button("取消");
button2.setBounds(280, 240, 50, 30);
label1 = new JLabel("账号(请输入身份证号!): ");
label1.setBounds(80, 100, 140, 30);
label2 = new JLabel("密码16位以内: ");
label2.setBounds(80, 140, 140, 30);
label3 = new JLabel("手机号: ");
label3.setBounds(80, 180, 140, 30);
text1 = new JTextField();
text1.setBounds(230, 100, 130, 30);
text2 = new JPasswordField(16);
text2.setBounds(230, 140, 130, 30);
text3 = new JTextField();
text3.setBounds(230, 180, 130, 30);
button1.addActionListener(this);
ViewActionEvent.windowmsg(button2, this);
jPanel1.add(button1);
jPanel1.add(button2);
jPanel1.add(label1);
jPanel1.add(label2);
jPanel1.add(label3);
jPanel1.add(text1);
jPanel1.add(text2);
jPanel1.add(text3);
this.add(jPanel1);
}
@Override
public void actionPerformed(ActionEvent e) {
if (text1.getText().equals("")||text1.getText().length()!=18){
JOptionPane.showMessageDialog(null,"请输入18位身份证号!","提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(text2.getText().equals("")||text2.getText().length()>16){
JOptionPane.showMessageDialog(null,"请输入16位以内密码!","提示",JOptionPane.WARNING_MESSAGE);
return;
}
if (text3.getText().equals("")){
JOptionPane.showMessageDialog(null,"请输入16位数字!","提示",JOptionPane.WARNING_MESSAGE);
return;
}
if (e.getActionCommand().equals("确认")) {
Reader reader = new Reader(text1.getText(), text2.getText(), text3.getText());
ReaderDaoimpl ReaderDaoimpl = new ReaderDaoimpl();
ReaderDaoimpl.add(reader);
JOptionPane.showMessageDialog(null, "注册成功", "提示", JOptionPane.WARNING_MESSAGE);
}
this.dispose();
}
}
6.图书增删改查代码
package com.vector.dao.impl;
import com.vector.dao.BookDao;
import com.vector.pojo.Book;
import com.vector.service.ConnectionDruid;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class BookDaoimpl implements BookDao {
private QueryRunner runner = null;
public BookDaoimpl() {
runner = new QueryRunner();
}
@Override
public void add(Book p) {
Connection conn = null;
try {
conn = ConnectionDruid.getConnection();
String sql = "insert into Book(BookName,BookNum,BookPosition,BookPrice,BookClass,BookMany)values(?,?,?,?,?,?)";
runner.update(conn, sql, p.getBookName(), p.getBookNum(), p.getBookPosition(), p.getBookPrice(), p.getBookClass(), p.getBookMany());
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DbUtils.closeQuietly(conn, null, null);
}
}
@Override
public void update(Book p) {
Connection conn = null;
try {
conn = ConnectionDruid.getConnection();
String sql = "update Book set BookName=?,BookNum=?,BookPosition=?,BookPrice=? BookClass=? BookMany=? where BookNum=?";
runner.update(conn, sql, p.getBookName(), p.getBookNum(), p.getBookPosition(), p.getBookPrice(), p.getBookClass(), p.getBookMany());
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DbUtils.closeQuietly(conn, null, null);
}
}
@Override
public void delete(int id) {
Connection conn = null;
try {
conn = ConnectionDruid.getConnection();
String sql = "delete from Book where BookNum=?";
runner.update(conn, sql, id);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DbUtils.closeQuietly(conn, null, null);
}
}
@Override
public Book findById(int id) {
Connection conn = null;
try {
conn = ConnectionDruid.getConnection();
String sql = "select BookName,BookNum,BookPosition,BookPrice,BookClass,BookMany from Book where BookNum=?";
Book book = runner.query(conn, sql, new BeanHandler<Book>(Book.class), id);
return book;
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DbUtils.closeQuietly(conn, null, null);
}
return null;
}
@Override
public List<Book> findAll() {
Connection conn = null;
try {
conn = ConnectionDruid.getConnection();
String sql = "select * from Book";
List<Book> persons = runner.query(conn, sql, new BeanListHandler<Book>(Book.class));
return persons;
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DbUtils.closeQuietly(conn, null, null);
}
return null;
}
@Override
public long personCount() {
Connection conn = null;
try {
conn = ConnectionDruid.getConnection();
String sql = "select count(BookNum) from Book";
return runner.query(conn, sql, new ScalarHandler<Long>());
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DbUtils.closeQuietly(conn, null, null);
}
return 0;
}
}
以上就是部分代码展示.
该项目开源链接
JDBC图书管理系统
|