基于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)
功能介绍:借阅列表、图书类别管理、图书馆里、用户管理、借阅管理、关于我们
JOptionPane.showMessageDialog(null, "图书类别添加失败");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "图书类别添加失败");
e1.printStackTrace();
}
}
}
});
jb_bookTypeAdd.setBounds(53, 238, 99, 23);
getContentPane().add(jb_bookTypeAdd);
// 重置按钮
JButton jb_bookTypeReset = new JButton("\u91CD\u7F6E");
jb_bookTypeReset.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\reset.png"));
jb_bookTypeReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
});
jb_bookTypeReset.setBounds(263, 238, 99, 23);
getContentPane().add(jb_bookTypeReset);
}
private void reset() {
bookTypeNameTxt.setText("");
bookTypeDescTxt.setText("");
}
}
public class BookDao {
public static int bookAdd(Connection con, Book book) throws Exception {
String sql = "insert into t_book(`bookname`,`author`,`price`,`bookdesc`,`booktypeid`) values(?,?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
* @throws Exception
*/
public Connection getCon() throws Exception {
Class.forName(jdbcName);
Connection con = DriverManager.getConnection(dbUrl, dbUserName,
dbUserPassword);
return con;
}
/**
* 关闭数据库连接
*
* @param con
* @throws Exception
*/
public void closeCon(Connection con) throws Exception {
if (con != null) {
con.close();
}
}
public static void main(String[] args) {
DbUtil dbUtil = new DbUtil();
try {
dbUtil.getCon();
System.out.println("数据库连接成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 图书列表类
* */
public class BookType {
private int id;
private String bookTypeName;
private String bookTypeDesc;
public BookType() {
}
}
private void init() {
scrollPane = new JScrollPane();
scrollPane.setToolTipText("");
scrollPane.setBounds(30, 91, 732, 178);
getContentPane().add(scrollPane);
bookTable = new JTable();
bookTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row = bookTable.getSelectedRow();
idTxt.setText((Integer) bookTable.getValueAt(row, 0) + "");
bookNameTxt.setText((String) bookTable.getValueAt(row, 1));
bookAuthorTxt.setText((String) bookTable.getValueAt(row, 2));
bookPriceTxt.setText((Float) bookTable.getValueAt(row, 3) + "");
bookDescTxt.setText(bookTable.getValueAt(row, 4) + "");
String bookTypeName = (String) bookTable.getValueAt(row, 5);
int n = jcb_bookType.getItemCount();
for (int i = 0; i < n; i++) {
BookType item = (BookType) jcb_bookType.getItemAt(i);
if (item.getBookTypeName().equals(bookTypeName)) {
jcb_bookType.setSelectedIndex(i);
}
}
}
});
bookTable.setToolTipText("\u56FE\u4E66");
bookTable.setSurrendersFocusOnKeystroke(true);
bookTable.setModel(new DefaultTableModel(new Object[][] { { null, null,
null, null, null, null }, }, new String[] {
"\u56FE\u4E66\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0",
"\u56FE\u4E66\u4F5C\u8005", "\u56FE\u4E66\u4EF7\u683C",
"\u56FE\u4E66\u63CF\u8FF0", "\u56FE\u4E66\u7C7B\u522B" }) {
boolean[] columnEditables = new boolean[] { true, false, false,
false, false, false };
public boolean isCellEditable(int row, int column) {
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void resetValue() {
bookNameTxt.setText("");
bookAuthorTxt.setText("");
bookPriceTxt.setText("");
jcb_bookType.setSelectedIndex(0);
bookDescTxt.setText("");
}
}
public class MainFrm extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
int bookTypeId = bookType.getId();
Book book = new Book(-1, bookName, bookAuthor, Float
.parseFloat(bookPrice), bookDesc, bookTypeId);
Connection con = null;
try {
con = dbUtil.getCon();
int addNum = BookDao.bookAdd(con, book);
if (1 == addNum) {
JOptionPane.showMessageDialog(null, "添加成功");
resetValue();
} else {
JOptionPane.showMessageDialog(null, "添加失败");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败");
} finally {
try {
con.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
});
jb_bookAdd.setBounds(115, 339, 93, 23);
getContentPane().add(jb_bookAdd);
// 重置按钮
JButton jb_bookReset = new JButton("\u91CD\u7F6E");
jb_bookReset.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\reset.png"));
jb_bookReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetValue();
}
});
jb_bookReset.setBounds(264, 339, 93, 23);
getContentPane().add(jb_bookReset);
}
// 填充下拉菜单
private void fillBookType() {
Connection con = null;
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;
}
/**
* 关闭数据库连接
*
* @param con
* @throws Exception
*/
public void closeCon(Connection con) throws Exception {
if (con != null) {
con.close();
}
}
public static void main(String[] args) {
DbUtil dbUtil = new DbUtil();
String id = idTxt.getText();
if (StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
return;
}
int n = JOptionPane.showConfirmDialog(null, "确定要删除这条记录吗?");
if (n == 0) {
Connection con = null;
try {
con = dbUtil.getCon();
int re = bookDao.bookDelete(con, id);
if (1 == re) {
JOptionPane.showMessageDialog(null, "删除成功");
resetValue();
fillTable(new Book());
} else
JOptionPane.showMessageDialog(null, "删除失败");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "删除失败");
e1.printStackTrace();
}
}
}
});
jb_delete.setBounds(257, 225, 93, 23);
panel_1.add(jb_delete);
}
private void resetValue() {
this.idTxt.setText("");
this.bookNameTxt.setText("");
this.bookAuthorTxt.setText("");
this.bookPriceTxt.setText("");
this.bookDescTxt.setText("");
if (jcb_bookType.getItemCount() > 0) {
jcb_bookType.setSelectedIndex(0);
}
}
}
public class BookTypeManageInterFrm extends JInternalFrame {
DbUtil dbUtil = new DbUtil();
BookTypeDao bookTypeDao = new BookTypeDao();
private JTextField s_bookTypeNameTxt;
private JTextField idTxt;
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);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookTypeAddInterFrm frame = new BookTypeAddInterFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BookTypeAddInterFrm() {
setIconifiable(true);
setClosable(true);
setResizable(true);
setTitle("\u6DFB\u52A0\u56FE\u4E66\u7C7B\u522B");
setBounds(100, 100, 480, 325);
setLocation(500, 100);
getContentPane().setLayout(null);
init();
}
private void init() {
BookTypeDao bookTypeDao = new BookTypeDao();
DbUtil dbUtil = new DbUtil();
JLabel lb_BookTypeName = new JLabel(
"\u56FE\u4E66\u7C7B\u522B\u540D\u79F0\uFF1A");
lb_BookTypeName.setBounds(53, 32, 99, 15);
getContentPane().add(lb_BookTypeName);
bookTypeNameTxt = new JTextField();
bookTypeNameTxt.setBounds(162, 29, 150, 21);
getContentPane().add(bookTypeNameTxt);
bookTypeNameTxt.setColumns(10);
dtm.addRow(v);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void init() {
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(UIManager
.getBorder("TitledBorder.border"), "\u641C\u7D22\u6761\u4EF6",
TitledBorder.LEADING, TitledBorder.TOP, null,
new Color(0, 0, 0)));
panel.setToolTipText("\u641C\u7D22\u6761\u4EF6");
panel.setBounds(10, 24, 655, 58);
getContentPane().add(panel);
panel.setLayout(null);
JLabel lblid = new JLabel("\u7528\u6237Id\uFF1A");
lblid.setBounds(10, 20, 70, 15);
panel.add(lblid);
userIdTxt = new JTextField();
userIdTxt.setBounds(78, 17, 150, 21);
panel.add(userIdTxt);
userIdTxt.setColumns(10);
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);
panel.add(label_1);
JLabel label_2 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
label_2.setBounds(10, 29, 67, 15);
panel.add(label_2);
bookNameTxt = new JTextField();
bookNameTxt.setColumns(10);
bookNameTxt.setBounds(84, 26, 114, 21);
panel.add(bookNameTxt);
bookAuthorTxt = new JTextField();
bookAuthorTxt.setColumns(10);
bookAuthorTxt.setBounds(285, 26, 114, 21);
panel.add(bookAuthorTxt);
// 搜索按钮
jb_search = new JButton("\u67E5\u8BE2");
jb_search.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\search.png"));
jb_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Book book = new Book();
String bookName = bookNameTxt.getText();
String bookAuthor = bookAuthorTxt.getText();
BookType bookType = (BookType) jcb_bookType.getSelectedItem();
int bookTypeId = bookType.getId();
book.setBookName(bookName);
book.setBookAuthor(bookAuthor);
book.setBookTypeId(bookTypeId);
fillTable(book);
}
});
jb_search.setBounds(629, 25, 93, 23);
panel.add(jb_search);
jcb_bookType = new JComboBox<BookType>();
jcb_bookType.setBounds(472, 26, 131, 21);
panel.add(jcb_bookType);
public BookType() {
super();
}
public BookType(String bookTypeName, String bookTypeDesc) {
super();
this.bookTypeName = bookTypeName;
this.bookTypeDesc = bookTypeDesc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookTypeName() {
return bookTypeName;
}
public void setBookTypeName(String bookTypeName) {
this.bookTypeName = bookTypeName;
}
public String getBookTypeDesc() {
return bookTypeDesc;
}
public void setBookTypeDesc(String bookTypeDesc) {
this.bookTypeDesc = bookTypeDesc;
}
@Override
public String toString() {
return this.getBookTypeName();
}
}
/**
* 字符串判断工具类
* */
public class StringUtil {
public static boolean isEmpty(String str) {
if ("".equals(str) || str == null)
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookTypeAddInterFrm frame = new BookTypeAddInterFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BookTypeAddInterFrm() {
setIconifiable(true);
setClosable(true);
setResizable(true);
setTitle("\u6DFB\u52A0\u56FE\u4E66\u7C7B\u522B");
setBounds(100, 100, 480, 325);
setLocation(500, 100);
getContentPane().setLayout(null);
init();
}
private void init() {
BookTypeDao bookTypeDao = new BookTypeDao();
DbUtil dbUtil = new DbUtil();
JLabel lb_BookTypeName = new JLabel(
"\u56FE\u4E66\u7C7B\u522B\u540D\u79F0\uFF1A");
lb_BookTypeName.setBounds(53, 32, 99, 15);
getContentPane().add(lb_BookTypeName);
bookTypeNameTxt = new JTextField();
bookTypeNameTxt.setBounds(162, 29, 150, 21);
getContentPane().add(bookTypeNameTxt);
bookTypeNameTxt.setColumns(10);
JLabel lblNewLabel_1 = new JLabel(
"\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0\uFF1A");
lblNewLabel_1.setBounds(53, 91, 99, 15);
public void actionPerformed(ActionEvent e) {
dispose();
}
});
mn_datamanager.add(menu_2);
JMenu mn_lendreturn = new JMenu("\u501F\u9605\u7BA1\u7406");
menuBar.add(mn_lendreturn);
JMenuItem menuItem = new JMenuItem(
"\u56FE\u4E66\u501F\u9605\u7BA1\u7406");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LendBookManageInterFrm lendBookManageInterFrm = new LendBookManageInterFrm();
lendBookManageInterFrm.setVisible(true);
table.add(lendBookManageInterFrm);
}
});
mn_lendreturn.add(menuItem);
JMenu mnNewMenu_2 = new JMenu("\u5173\u4E8E\u6211\u4EEC");
menuBar.add(mnNewMenu_2);
// 关于我们
JMenuItem menuItem_1 = new JMenuItem("\u5173\u4E8E\u8F6F\u4EF6");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane
.showMessageDialog(
null,
"**********************************************\n"
+ " 编写者:围观客 "
+ " 网站地址:http://www.weiguanke.cn"
+ " 谢谢大家使用我门的软件");
}
});
mnNewMenu_2.add(menuItem_1);
}
}
}
}
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;
jb_bookAdd.setBounds(115, 339, 93, 23);
getContentPane().add(jb_bookAdd);
// 重置按钮
JButton jb_bookReset = new JButton("\u91CD\u7F6E");
jb_bookReset.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\reset.png"));
jb_bookReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetValue();
}
});
jb_bookReset.setBounds(264, 339, 93, 23);
getContentPane().add(jb_bookReset);
}
// 填充下拉菜单
private void fillBookType() {
Connection con = null;
BookType bookType = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeList(con, new BookType("", ""));
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
jcb_bookType.addItem(bookType);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void resetValue() {
bookNameTxt.setText("");
bookAuthorTxt.setText("");
bookPriceTxt.setText("");
jcb_bookType.setSelectedIndex(0);
bookDescTxt.setText("");
this.bookTypeId = bookTypeId;
}
public String getBookTypeName() {
return bookTypeName;
}
public void setBookTypeName(String bookTypeName) {
this.bookTypeName = bookTypeName;
}
}
/**
* 用户类
* */
public class User {
private int id;
private String userName;
private String userPassword;
private int role = 0; // 默认为0->读者 、 1->图书管理员
private String pname;
public User() {
this.id = -1;
}
public User(int id, String userName) {
super();
this.id = id;
this.userName = userName;
}
public User(String userName, String userPassword) {
super();
this.userName = userName;
this.userPassword = userPassword;
}
public User(int id, String userName, String userPassword, int role,
String pname) {
super();
this.id = id;
this.userName = userName;
this.userPassword = userPassword;
this.role = role;
this.pname = pname;
}
public int getId() {
return id;
JMenuItem jmi_bookTypeManage = new JMenuItem(
"\u56FE\u4E66\u7C7B\u522B\u7EF4\u62A4");
jmi_bookTypeManage.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\bookTypeManager.png"));
jmi_bookTypeManage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookTypeManageInterFrm bookTypeManageInterFrm = new BookTypeManageInterFrm();
bookTypeManageInterFrm.setVisible(true);
table.add(bookTypeManageInterFrm);
}
});
menu.add(jmi_bookTypeManage);
JMenu menu_1 = new JMenu("\u56FE\u4E66\u7BA1\u7406");
mn_datamanager.add(menu_1);
// 添加图书菜单项
JMenuItem jmi_bookAdd = new JMenuItem("\u6DFB\u52A0\u56FE\u4E66");
jmi_bookAdd.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\add.png"));
jmi_bookAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookAddInterFrm bookAddInterFrm = new BookAddInterFrm();
bookAddInterFrm.setVisible(true);
table.add(bookAddInterFrm);
}
});
menu_1.add(jmi_bookAdd);
// 图书维护菜单项
JMenuItem jmi_bookManage = new JMenuItem("\u56FE\u4E66\u7EF4\u62A4");
jmi_bookManage.setIcon(new ImageIcon(
"D:\\java\\MyBookManager\\image\\bookManager.png"));
jmi_bookManage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookManageInterFrm bookManageInterFrm = new BookManageInterFrm();
bookManageInterFrm.setVisible(true);
table.add(bookManageInterFrm);
}
});
menu_1.add(jmi_bookManage);
JMenuItem menu_2 = new JMenuItem("\u9000\u51FA");
menu_2.addActionListener(new ActionListener() {