基于java+mysql的swing+mysql图书管理系统(java+swing+gui+mysql)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于java+mysql的Swing+MySQL图书管理系统(java+swing+gui+mysql)
功能介绍:借阅列表、图书类别管理、图书馆里、用户管理、借阅管理、关于我们
// 还书
public int returnBook(Connection con, String id) throws Exception {
String sql = "delete from t_lendbook where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
// 获取借阅列表
public ResultSet lendBookList(Connection con, User user, Book book)
throws Exception {
StringBuffer sb = new StringBuffer("select * from t_lendbook ");
if (user != null && user.getId() != -1) {
sb.append(" and userid = " + user.getId());
}
if (book != null && book.getId() != -1) {
sb.append(" and bookid =" + book.getId());
}
PreparedStatement pstmt = con.prepareStatement(sb.toString()
.replaceFirst("and", "where"));
return pstmt.executeQuery();
}
// 根据id获取用户名
public String getPnameById(Connection con, int userId) throws Exception {
String sql = "select * from t_user where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
return rs.getString("pname");
else
return null;
}
}
public class BookTypeDao {
// 添加图书类别
public int bookTypeAdd(Connection con, BookType bookType) throws Exception {
String sql = "insert into t_booktype values(null,?,?)";
}
// 删除图书类别
public int bookTypeDelete(Connection con, String id) throws Exception {
String sql = "delete from t_bookType where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
public String bookTypeNameById(Connection con, String id) throws Exception {
String sql = "select * from t_bookType where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeQuery().getString("bookTypeName");
}
}
/**
* 图书类
* */
public class Book {
private int id;
private String bookName;
private String bookAuthor;
private float bookPrice;
private String bookDesc;
private int bookTypeId;
private String bookTypeName;
public Book() {
super();
this.id = -1;
this.bookName = "";
this.bookAuthor = "";
this.bookPrice = 0;
this.bookDesc = "";
this.bookTypeId = -1;
}
public Book(int id, String bookName) {
super();
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
}
/**
* 数据库连接工具类
* */
public class DbUtil {
private String dbUrl = "jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull";
private String dbUserName = "root";
private String dbUserPassword = "123456";
private String jdbcName = "com.mysql.jdbc.Driver";
/**
* 获取数据库的连接
*
* @return 数据库连接对象
* @throws Exception
*/
public Connection getCon() throws Exception {
Class.forName(jdbcName);
Connection con = DriverManager.getConnection(dbUrl, dbUserName,
dbUserPassword);
return con;
}
/**
* 关闭数据库连接
setBounds(100, 100, 812, 618);
setLocation(300, 20);
getContentPane().setLayout(null);
init();
fillTable(new Book());
this.fillBookType("search");
this.fillBookType("modify");
}
// 填充下拉菜单
private void fillBookType(String type) {
Connection con = null;
BookType bookType = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
if ("search".equals(type)) {
bookType = new BookType();
bookType.setBookTypeName("请选择...");
bookType.setId(-1);
this.s_jcbBookType.addItem(bookType);
}
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
if ("search".equals(type)) {
this.s_jcbBookType.addItem(bookType);
} else if ("modify".equals(type)) {
this.jcb_bookType.addItem(bookType);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 填充表格
private void fillTable(Book book) {
DefaultTableModel dtm = (DefaultTableModel) bookTable.getModel();
dtm.setRowCount(0);
label_3.setBounds(224, 81, 65, 15);
panel_1.add(label_3);
bookAuthorTxt = new JTextField();
bookAuthorTxt.setColumns(10);
bookAuthorTxt.setBounds(299, 78, 118, 21);
panel_1.add(bookAuthorTxt);
JLabel label_4 = new JLabel("\u56FE\u4E66\u4EF7\u683C\uFF1A");
label_4.setBounds(10, 160, 76, 15);
panel_1.add(label_4);
bookPriceTxt = new JTextField();
bookPriceTxt.setBounds(96, 157, 117, 21);
panel_1.add(bookPriceTxt);
bookPriceTxt.setColumns(10);
JLabel label_5 = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
label_5.setBounds(224, 160, 65, 15);
panel_1.add(label_5);
jcb_bookType = new JComboBox<BookType>();
jcb_bookType.setBounds(299, 157, 118, 21);
panel_1.add(jcb_bookType);
JLabel label_6 = new JLabel("\u56FE\u4E66\u63CF\u8FF0\uFF1A");
label_6.setBounds(439, 81, 65, 15);
panel_1.add(label_6);
bookDescTxt = new JTextArea();
bookDescTxt.setBounds(514, 77, 208, 98);
panel_1.add(bookDescTxt);
// 修改按钮
JButton jb_modify = new JButton("\u4FEE\u6539");
jb_modify.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\modify.png"));
jb_modify.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id = idTxt.getText();
if (StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
return;
}
String bookName = bookNameTxt.getText();
String author = bookAuthorTxt.getText();
String price = bookPriceTxt.getText();
String bookDesc = bookDescTxt.getText();
if (StringUtil.isEmpty(bookName)) {
JOptionPane.showMessageDialog(null, "图书名称不能为空!");
return;
* Launch the application.
*/
/*
* public static void main(String[] args) { EventQueue.invokeLater(new
* Runnable() { public void run() { try {
*
* UserMainFrm frame = new UserMainFrm(); frame.setVisible(true); } catch
* (Exception e) { e.printStackTrace(); } } }); }
*/
/**
* Create the frame.
*/
public UserMainFrm(User user) {
setTitle("\u501F\u9605\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 835, 445);
this.user = user;
JOptionPane.showMessageDialog(null, "欢迎" + user.getPname() + "使用我们的系统");
init();
fillTable(new Book());
this.fillBookType("search");
this.fillBookType("modify");
}
// 填充下拉菜单
private void fillBookType(String type) {
Connection con = null;
BookType bookType = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
if ("search".equals(type)) {
bookType = new BookType();
bookType.setBookTypeName("请选择...");
bookType.setId(-1);
this.jcb_bookType.addItem(bookType);
}
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
if ("search".equals(type)) {
this.jcb_bookType.addItem(bookType);
} else if ("modify".equals(type)) {
this.jcb_bookType.addItem(bookType);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
private JTextField bookTypeNameTxt;
private JTable bookTypeTable;
private JTextArea bookTypeDescTxt;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookTypeManageInterFrm frame = new BookTypeManageInterFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BookTypeManageInterFrm() {
setClosable(true);
setIconifiable(true);
setTitle("\u56FE\u4E66\u7C7B\u522B\u7EF4\u62A4");
setBounds(100, 100, 547, 549);
setLocation(500, 100);
getContentPane().setLayout(null);
init();
fillTable(new BookType("", ""));
}
// 填充表格
private void fillTable(BookType bookType) {
DefaultTableModel dtm = (DefaultTableModel) bookTypeTable.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = dbUtil.getCon();
ResultSet res = bookTypeDao.bookTypeList(con, bookType);
while (res.next()) {
setClosable(true);
setIconifiable(true);
setTitle("\u56FE\u4E66\u7BA1\u7406");
setBounds(100, 100, 812, 618);
setLocation(300, 20);
getContentPane().setLayout(null);
init();
fillTable(new Book());
this.fillBookType("search");
this.fillBookType("modify");
}
// 填充下拉菜单
private void fillBookType(String type) {
Connection con = null;
BookType bookType = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
if ("search".equals(type)) {
bookType = new BookType();
bookType.setBookTypeName("请选择...");
bookType.setId(-1);
this.s_jcbBookType.addItem(bookType);
}
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
if ("search".equals(type)) {
this.s_jcbBookType.addItem(bookType);
} else if ("modify".equals(type)) {
this.jcb_bookType.addItem(bookType);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 获取图书列表
public ResultSet bookList(Connection con, Book book) throws Exception {
StringBuffer sb = new StringBuffer(
"select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
if (StringUtil.isNotEmpty(book.getBookName())) {
sb.append(" and b.bookName like '%" + book.getBookName() + "%'");
}
if (StringUtil.isNotEmpty(book.getBookAuthor())) {
sb.append(" and b.author like '%" + book.getBookAuthor() + "%'");
}
if (book.getBookTypeId() != -1) {
sb.append(" and b.bookTypeId = " + book.getBookTypeId());
}
PreparedStatement pstmt = con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
public int bookDelete(Connection con, String id) throws Exception {
String sql = "delete from t_book where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
// 修改图书
public int bookModify(Connection con, Book book) throws Exception {
String sql = "update t_book set bookName=?,author=?,price=?,bookDesc=?,bookTypeId=? where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, book.getBookName());
pstmt.setString(2, book.getBookAuthor());
pstmt.setFloat(3, book.getBookPrice());
pstmt.setString(4, book.getBookDesc());
pstmt.setInt(5, book.getBookTypeId());
pstmt.setInt(6, book.getId());
return pstmt.executeUpdate();
}
// 获取当前类别是否有图书
private void fillBookType(String type) {
Connection con = null;
BookType bookType = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
if ("search".equals(type)) {
bookType = new BookType();
bookType.setBookTypeName("请选择...");
bookType.setId(-1);
this.jcb_bookType.addItem(bookType);
}
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
if ("search".equals(type)) {
this.jcb_bookType.addItem(bookType);
} else if ("modify".equals(type)) {
this.jcb_bookType.addItem(bookType);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 填充表格
private void fillTable(Book book) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookDao.bookList(con, book);
while (rs.next()) {
Vector v = new Vector();
v.add(rs.getInt("id"));
v.add(rs.getString("bookName"));
public class Book {
private int id;
private String bookName;
private String bookAuthor;
private float bookPrice;
private String bookDesc;
private int bookTypeId;
private String bookTypeName;
public Book() {
super();
this.id = -1;
this.bookName = "";
this.bookAuthor = "";
this.bookPrice = 0;
this.bookDesc = "";
this.bookTypeId = -1;
}
public Book(int id, String bookName) {
super();
this.id = id;
this.bookName = bookName;
}
public Book(int id, String bookName, String bookAuthor, float bookPrice,
String bookDesc, int bookTypeId) {
super();
this.id = id;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookPrice = bookPrice;
this.bookDesc = bookDesc;
this.bookTypeId = bookTypeId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
public class UserMainFrm extends JFrame {
private User user;
DbUtil dbUtil = new DbUtil();
BookDao bookDao = new BookDao();
BookTypeDao bookTypeDao = new BookTypeDao();
UserDao userDao = new UserDao();
private JPanel contentPane;
private JPanel panel;
private JTextField bookNameTxt;
private JTextField bookAuthorTxt;
private JComboBox<BookType> jcb_bookType;
private JButton jb_search;
private JTable table;
private JTextField s_idTxt;
private JButton jb_lendBook;
private JTextField s_bookNameTxt;
/**
* Launch the application.
*/
/*
* public static void main(String[] args) { EventQueue.invokeLater(new
* Runnable() { public void run() { try {
*
* UserMainFrm frame = new UserMainFrm(); frame.setVisible(true); } catch
* (Exception e) { e.printStackTrace(); } } }); }
*/
/**
* Create the frame.
*/
public UserMainFrm(User user) {
setTitle("\u501F\u9605\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 835, 445);
JLabel lblid_1 = new JLabel("\u56FE\u4E66Id\r\n\uFF1A");
lblid_1.setBounds(238, 20, 70, 15);
panel.add(lblid_1);
bookIdTxt = new JTextField();
bookIdTxt.setBounds(318, 17, 155, 21);
panel.add(bookIdTxt);
bookIdTxt.setColumns(10);
// 搜索按钮
JButton jb_search = new JButton("\u641C\u7D22");
jb_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection con = null;
try {
con = dbUtil.getCon();
String userId = userIdTxt.getText();
String bookId = bookIdTxt.getText();
Book book = new Book();
User user = new User();
if (StringUtil.isNotEmpty(bookId)) {
book.setId(Integer.parseInt(bookId));
}
if (StringUtil.isNotEmpty(userId))
user.setId(Integer.parseInt(userId));
fillTable(user, book);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
jb_search.setBounds(510, 16, 93, 23);
panel.add(jb_search);
panel.setFocusTraversalPolicy(new FocusTraversalOnArray(
new Component[] { lblid, userIdTxt, lblid_1, bookIdTxt,
jb_search }));
JScrollPane scrollPane = new JScrollPane();
if (book.getBookTypeId() != -1) {
sb.append(" and b.bookTypeId = " + book.getBookTypeId());
}
PreparedStatement pstmt = con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
public int bookDelete(Connection con, String id) throws Exception {
String sql = "delete from t_book where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
// 修改图书
public int bookModify(Connection con, Book book) throws Exception {
String sql = "update t_book set bookName=?,author=?,price=?,bookDesc=?,bookTypeId=? where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, book.getBookName());
pstmt.setString(2, book.getBookAuthor());
pstmt.setFloat(3, book.getBookPrice());
pstmt.setString(4, book.getBookDesc());
pstmt.setInt(5, book.getBookTypeId());
pstmt.setInt(6, book.getId());
return pstmt.executeUpdate();
}
// 获取当前类别是否有图书
public boolean getBookByBookTypeId(Connection con, String bookTypeId)
throws Exception {
String sql = "select * from t_book where bookTypeId=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, bookTypeId);
ResultSet rs = pstmt.executeQuery();
return rs.next();
}
// 借书
public int bookLend(Connection con, int bookId) throws Exception {
String sql = "update t_book set state=? where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, 1);
pstmt.setInt(2, bookId);
return pstmt.executeUpdate();
user.setId(Integer.parseInt(userId));
fillTable(user, book);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
jb_search.setBounds(510, 16, 93, 23);
panel.add(jb_search);
panel.setFocusTraversalPolicy(new FocusTraversalOnArray(
new Component[] { lblid, userIdTxt, lblid_1, bookIdTxt,
jb_search }));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 92, 655, 174);
getContentPane().add(scrollPane);
// 借书记录表格
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row = table.getSelectedRow();
s_idTxt.setText(table.getValueAt(row, 0) + "");
String userName = table.getValueAt(row, 1) + "";
s_userNameTxt.setText(userName.replaceAll("\\(\\d*\\)", ""));
String bookName = table.getValueAt(row, 2) + "";
s_bookNameTxt.setText(bookName.replaceAll("\\(\\d*\\)", ""));
}
});
table.setModel(new DefaultTableModel(new Object[][] { { null, null,
null }, }, new String[] { "\u7F16\u53F7", "\u501F\u9605\u4EBA",
"\u501F\u9605\u56FE\u4E66" }));
table.getColumnModel().getColumn(0).setPreferredWidth(83);
table.getColumnModel().getColumn(1).setPreferredWidth(162);
table.getColumnModel().getColumn(2).setPreferredWidth(187);
scrollPane.setViewportView(table);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(10, 276, 655, 101);
getContentPane().add(panel_1);
panel_1.setLayout(null);
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
}
/**
* 数据库连接工具类
* */
public class DbUtil {
private String dbUrl = "jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull";
private String dbUserName = "root";
private String dbUserPassword = "123456";
private String jdbcName = "com.mysql.jdbc.Driver";
/**
* 获取数据库的连接
*
* @return 数据库连接对象
* @throws Exception
*/
public Connection getCon() throws Exception {
Class.forName(jdbcName);
Connection con = DriverManager.getConnection(dbUrl, dbUserName,
dbUserPassword);
return con;
}
/**
}
}
});
jb_login.setBounds(102, 191, 93, 23);
contentPane.add(jb_login);
// 重置按钮
JButton jb_reset = new JButton("\u91CD\u7F6E");
jb_reset.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\reset.png"));
jb_reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
});
jb_reset.setBounds(225, 191, 93, 23);
contentPane.add(jb_reset);
userPasswordTxt = new JPasswordField();
userPasswordTxt.setBounds(166, 126, 152, 21);
contentPane.add(userPasswordTxt);
JLabel lblNewLabel = new JLabel(" ");
lblNewLabel.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\logo.png"));
lblNewLabel.setBounds(182, 10, 99, 52);
contentPane.add(lblNewLabel);
}
}