数据库期末设计——图书管理系统
目录
1.前置软件以及开发环境:
2.开发过程讲解
代码环节:
数据库代码
1.BookDao.java
2.BookTypeDao.java
3.UserDao.java
4.Book.java
5.BookType.java
6.User.java
7.DbUtil.java
8.Stringutil.java
9.BookAddInterFrm.java
10.BookManageInterFrm.java
11.BookTypeAddInterFrm.java
12.BookTypeManagerInterFrm.java
13.Java666interframe.java
14.Login.java
15.Mainframe.java
3.结果展示:
4.结尾体会心得:
5.网盘地址分享:
本次设计基于eclipse+Javaswing+windowBuilder+JDBC+mysql实现,我会从头到尾对整个实现过程进行详细的讲解,最后我也会献上我的全部文件希望对大家有所帮助!!
1.前置软件以及开发环境:
安装流程我就不在这里多说了,网上都有,我这里将一些要点。
eclipse:博主选用的是2022-12的版本比较稳定,其他版本也可以,IDEA也是可以的,不是很建议用vscode,虽然集成但是终归没有专门的软件好使。
Java swing以及window Builder:这些都是插件,具体步骤可以去网上搜,Javaswing是一个库,安装完毕不需要管,这边着重讲一下window Builder。
首先就是安装:很多人安装的时候看软件右下角的进度条读完或者等不及了就直接退出eclipse了,这样子是不对的,一定要等安装成功后会有一个小弹窗出来让你重启eclipse,这样子才会成功,如果你要是安装失败了,要么重装eclipse,要么就打开eclipse等待一会儿,看看能否继续安装,博主就是第二种情况,打开后等待几分钟就安装好了。
然后是使用:
找到你们的项目位置,右键src选择新建,然后最底下有个选项“其他”,找到其中的windowBuilder就可以创建窗口了,具体可以自己操作下。
Mysql:这个应该不用我多说,大家都有安装的肯定,这边讲一下好用的操作端,博主用的是datagrip,但是这个要付费大家可以去搜搜破解版(博主自己就是破解的)。还有就是Navicat也是不错的,实际上都是可以的只要能够建立数据库即可。
注意:有很多小伙伴开始做的时候啥也不会,认为eclipse一定要连接数据库什么的,实际上完全不需要,这些我们会在代码里面进行操作。
2.开发过程讲解
首先我们需要建立至少四个包,如下图:
第一次写这个可能都不是很了解我就简单讲一下我的理解:
1.dao层,就是用于导入导出数据的,简单点来说我们要在里面写一些数据库的sql语句
2.model层,就是模型层,就是在里面写具体的实体类,再明白点就是你可以把你数据库里面的一个表看成一种类,几个表就建几个类。
3.util层,这个是工具层,你可以在里面写一些方法以便里面的使用,我后续也会进行讲解。
4.view层,这个就是视图层,你的窗口都写在这个里面,一般来说你的程序也会从这个里面的主界面开始运行。
5.至于image:这个就是用来存放你的一些图片方便调用
最后一点:一定要下载最后的那个引用的库,这边可以去网上找然后直接拖拽进去就好了,当然最后我也会将文件分享给大家,这个是数据库连接驱动,没下可连接不了哦。
代码环节:
数据库代码
首先先给大家一个数据库生成代码防止大家搞不出数据库:
下面没写创建数据库的语句,额就自己创建一下,数据库名称叫db_book,写错了代码可就跑不动了。
create table t_booktype ( id int auto_increment primary key, bookTypeName varchar(20) null, bookTpeDesc varchar(1000) null ); create table t_book ( id int auto_increment primary key, bookName varchar(20) not null, author varchar(20) null, sex varchar(10) null, price float null, bookTypeId int null, bookDesc varchar(1000) null, constraint t_book_t_booktype_id_fk foreign key (bookTypeId) references t_booktype (id) ); create table t_user ( id int auto_increment primary key, username varchar(20) not null, password varchar(20) not null );
直接粘贴在你的数据库查询台里就行了,可以自己看看效果,数据不重要可以自己添加。
这个其实也不是很好讲,我就一个一个来了,先给大家看下整个代码的样子(如果大家最后发现字符集什么的不行导致乱码就建议直接粘贴代码跑一下比较好):
如果有需要特别注意的地方我会指出,其他的无脑复制即可。
1.BookDao.java
package com.java1234.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.swing.text.html.HTMLDocument.HTMLReader.ParagraphAction; import com.java1234.model.Book; import com.java1234.util.Stringutil; /** * book添加和删除 * @author 46476 * */ public class BookDao { /** * 图书添加 * @param con * @param book * @return * @throws Exception */ public int add(Connection con,Book book)throws Exception{ String sql="insert into t_book values(null,?,?,?,?,?,?)"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, book.getBookName()); pstmt.setString(2, book.getAuthor()); pstmt.setString(3, book.getSex()); pstmt.setFloat(4, book.getPrice()); pstmt.setInt(5, book.getBookTypeId()); pstmt.setString(6, book.getBookTypeDesc()); return pstmt.executeUpdate(); } /** * 图书信息查询 * @param con * @param book * @return * @throws Exception */ public ResultSet list(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.getAuthor())) { sb.append(" and b.author like '%"+book.getAuthor()+"%'"); } if(book.getBookTypeId()!=null&&book.getBookTypeId()!=-1) { sb.append(" and b.bookTypeId="+book.getBookTypeId()); } PreparedStatement pstmt=con.prepareStatement(sb.toString()); return pstmt.executeQuery(); } /** * 删除记录条数 * @param con * @param id * @return * @throws Exception */ public int delete(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 updata(Connection con,Book book)throws Exception { String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookTypeId=?,bookDesc=? where id=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, book.getBookName()); pstmt.setString(2, book.getAuthor()); pstmt.setString(3, book.getSex()); pstmt.setFloat(4, book.getPrice()); pstmt.setInt(5, book.getBookTypeId()); pstmt.setString(6, book.getBookTypeDesc());//这边之前写错了应该是bookDesc pstmt.setInt(7, book.getId()); return pstmt.executeUpdate(); } /** * 指定图书类别下是否存在图书 * @param con * @param bookTypeId * @return * @throws Exception */ public boolean existBook(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(); } }
2.BookTypeDao.java
package com.java1234.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.java1234.model.BookType; import com.java1234.util.Stringutil; /** * 图书类别dao类 * @author 46476 * */ public class BookTypeDao { /** * 图书类别添加 * @param con * @param bookType * @return * @throws Exception */ public int add(Connection con,BookType bookType)throws Exception{ String sql="insert into t_bookType value(null,?,?)"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, bookType.getBookTypeName()); pstmt.setString(2,bookType.getBookTypeDesc() ); return pstmt.executeUpdate(); } /** * 查询图书类别 * @param con * @param bookType * @return * @throws Exception */ public ResultSet list(Connection con,BookType bookType)throws Exception{ StringBuffer sb=new StringBuffer("select * from t_bookType"); if(Stringutil.isNotEmpty(bookType.getBookTypeName())) { sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'"); } PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where")); return pstmt.executeQuery(); } /** * 删除图书类别 * @param con * @param id * @return * @throws Exception */ public int delete(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(); } /** * 跟新图书类别 * @param con * @param bookType * @return * @throws Exception */ public int updata(Connection con,BookType bookType)throws Exception{ String sql="update t_bookType set bookTypeName=?,bookTpeDesc=? where id=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, bookType.getBookTypeName()); pstmt.setString(2, bookType.getBookTypeDesc()); pstmt.setInt(3, bookType.getId()); return pstmt.executeUpdate(); } }
3.UserDao.java
package com.java1234.dao; import java.nio.channels.SelectableChannel; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.java1234.model.User; /** * 用户dao类 * @author 46476 * */ public class UserDao { public User login(Connection con,User user)throws Exception { User resultUser=null; String sql="select * from t_user where userName=? and password=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, user.getUserNameString()); pstmt.setString(2, user.getPasswordString()); ResultSet rs=pstmt.executeQuery(); if(rs.next()) { resultUser=new User(); resultUser.setId(rs.getInt("id")); resultUser.setUserNameString(rs.getString("userName")); resultUser.setPasswordString(rs.getString("password")); } return resultUser; } }
4.Book.java
package com.java1234.model; import java.sql.Connection; import com.mysql.cj.protocol.a.NativeConstants.StringLengthDataType; public class Book { private int id; private String bookName; private String author; private String sex; private Float price; private Integer bookTypeId; private String bookTypeName; private String bookTypeDesc; public Book() { super(); // TODO 自动生成的构造函数存根 } public Book(int id, String bookName, String author, String sex, Float price, Integer bookTypeId, String bookTypeDesc) { super(); this.id = id; this.bookName = bookName; this.author = author; this.sex = sex; this.price = price; this.bookTypeId = bookTypeId; this.bookTypeDesc = bookTypeDesc; } public Book(String bookName, String author, Integer bookTypeId) { super(); this.bookName = bookName; this.author = author; this.bookTypeId = bookTypeId; } public Book(String bookName, String author, String sex, Float price, Integer bookTypeId, String bookTypeDesc) { super(); this.bookName = bookName; this.author = author; this.sex = sex; this.price = price; this.bookTypeId = bookTypeId; this.bookTypeDesc = bookTypeDesc; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } 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 Integer getBookTypeId() { return bookTypeId; } public void setBookTypeId(Integer bookTypeId) { this.bookTypeId = bookTypeId; } 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; } }
5.BookType.java
package com.java1234.model; /** * 图书类别实体 * @author 46476 * */ public class BookType { private int id;//编号 private String bookTypeName;//图书类别名称 private String bookTypeDesc;//图书备注 public BookType() { super(); } public BookType(int id, String bookTypeName, String bookTypeDesc) { super(); this.id = id; this.bookTypeName = bookTypeName; this.bookTypeDesc = bookTypeDesc; } public BookType(String bookTypeName,String bookTypeDesc) { // TODO 自动生成的构造函数存根 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; } public String toString() { return this.bookTypeName; } }
6.User.java
package com.java1234.model; /** * 用户实体 * @author 46476 * */ public class User { private int id;//编号 private String userNameString;//用户名 private String passwordString;//密码 public User() { super(); } public User(String userNameString, String passwordString) { super(); this.userNameString = userNameString; this.passwordString = passwordString; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserNameString() { return userNameString; } public void setUserNameString(String userNameString) { this.userNameString = userNameString; } public String getPasswordString() { return passwordString; } public void setPasswordString(String passwordString) { this.passwordString = passwordString; } }
7.DbUtil.java
这里注意啦!!!!!!!!!!!!!!!
这里面的用户名以及密码都是要填写自己的数据库用户名以及密码,也就是我打xxxx的地方。
package com.java1234.util; import java.sql.Connection; import java.sql.DriverManager; /** * 数据库工具类 * @author 46476 * */ public class DbUtil { private String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//数据库地址 private String dbUserName="xxxxxx";//用户名 private String dbPassword="xxxxxxxx";//密码 private String jdbcNameString="com.mysql.cj.jdbc.Driver";//驱动名称 public Connection getCon()throws Exception{ Class.forName(jdbcNameString); Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword); return con; } 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) { // TODO 自动生成的 catch 块 e.printStackTrace(); System.out.println("连接失败"); } } }
8.Stringutil.java
package com.java1234.util; /** * 字符串工具类 * @author 46476 * */ public class Stringutil { //判断字符串是否为空 public static boolean isEmpty(String str) { if(str==null||"".equals(str.trim())) { return true; } return false; } public static boolean isNotEmpty(String str) { if(str!=null&&"".equals(str.trim())==false) return true; return false; } }
从这里开始我需要提一嘴:中间的一大段代码都是不用看的,这些都是windowBuilder帮你自动生成的,你只需要知道怎么微调就行了,不会的无脑cv,会的可以自己调成喜欢的样子。
9.BookAddInterFrm.java
package com.java1234.view; import java.awt.EventQueue; import javax.swing.JInternalFrame; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import java.sql.Connection; import java.sql.ResultSet; import javax.swing.JTextField; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.border.LineBorder; import com.java1234.dao.BookDao; import com.java1234.dao.BookTypeDao; import com.java1234.model.Book; import com.java1234.model.BookType; import com.java1234.util.DbUtil; import com.java1234.util.Stringutil; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JTextArea; import javax.swing.JComboBox; import javax.swing.JButton; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class BookAddInterFrm extends JInternalFrame { private static final long serialVersionUID = 1L; private JTextField bookNametxt; private JTextField authortxt; private final ButtonGroup buttonGroup = new ButtonGroup(); private JTextField pricetxt; private JTextArea bookDesctxt; private JComboBox bookTypeJcb; private DbUtil dbUtil=new DbUtil(); private BookTypeDao bookTypeDao=new BookTypeDao(); private BookDao bookDao=new BookDao(); private JRadioButton manjrb; JRadioButton womenjrb; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { BookAddInterFrm frame = new BookAddInterFrm(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public BookAddInterFrm() { setClosable(true); setIconifiable(true); setTitle("图书添加"); setBounds(100, 100, 602, 705); JLabel lblNewLabel = new JLabel("图书名称:"); lblNewLabel.setBounds(53, 82, 65, 18); lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13)); bookNametxt = new JTextField(); bookNametxt.setBounds(136, 79, 126, 24); bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); bookNametxt.setColumns(10); JLabel lblNewLabel_1 = new JLabel("图书作者:"); lblNewLabel_1.setBounds(336, 82, 55, 18); lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13)); authortxt = new JTextField(); authortxt.setBounds(409, 79, 126, 24); authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); authortxt.setColumns(10); JLabel lblNewLabel_2 = new JLabel("作者性别:"); lblNewLabel_2.setBounds(53, 168, 65, 18); lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13)); manjrb = new JRadioButton("男"); manjrb.setBounds(136, 164, 39, 27); buttonGroup.add(manjrb); manjrb.setSelected(true); manjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13)); womenjrb = new JRadioButton("女"); womenjrb.setBounds(193, 164, 39, 27); buttonGroup.add(womenjrb); womenjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13)); JLabel lblNewLabel_3 = new JLabel("图书价格:"); lblNewLabel_3.setBounds(336, 168, 65, 18); lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13)); pricetxt = new JTextField(); pricetxt.setBounds(411, 165, 126, 24); pricetxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); pricetxt.setColumns(10); JLabel lblNewLabel_4 = new JLabel("图书描述:"); lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 13)); lblNewLabel_4.setBounds(53, 353, 65, 15); bookDesctxt = new JTextArea(); bookDesctxt.setBounds(136, 348, 399, 242); getContentPane().setLayout(null); getContentPane().add(lblNewLabel); getContentPane().add(bookNametxt); getContentPane().add(lblNewLabel_4); getContentPane().add(lblNewLabel_2); getContentPane().add(manjrb); getContentPane().add(womenjrb); getContentPane().add(bookDesctxt); getContentPane().add(lblNewLabel_1); getContentPane().add(authortxt); getContentPane().add(lblNewLabel_3); getContentPane().add(pricetxt); JLabel lblNewLabel_5 = new JLabel("图书类别:"); lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 13)); lblNewLabel_5.setBounds(53, 273, 65, 15); getContentPane().add(lblNewLabel_5); bookTypeJcb = new JComboBox(); bookTypeJcb.setBounds(136, 270, 126, 23); getContentPane().add(bookTypeJcb); JButton btnNewButton = new JButton("添加"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bookAddActionPerformed(e); } }); btnNewButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/添加.png"))); btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13)); btnNewButton.setBounds(220, 620, 93, 23); getContentPane().add(btnNewButton); JButton btnNewButton_1 = new JButton("重置"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { resetValueActionPerformed(e); } }); btnNewButton_1.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/重置.png"))); btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13)); btnNewButton_1.setBounds(370, 620, 93, 23); getContentPane().add(btnNewButton_1); bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false)); bookDesctxt.setLineWrap(true); //激活自动换行功能 bookDesctxt.setWrapStyleWord(true); // 激活断行不断字功能 fillBookType(); } protected void resetValueActionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 this.resetValue(); } /** * 图书添加处理 * @param e */ protected void bookAddActionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String bookName=this.bookNametxt.getText(); String author=this.authortxt.getText(); String price=this.pricetxt.getText(); String bookDesc=this.bookDesctxt.getText(); if(Stringutil.isEmpty(bookName)) { JOptionPane.showConfirmDialog(null, "图书名称不能为空"); return; } if(Stringutil.isEmpty(author)) { JOptionPane.showConfirmDialog(null, "作者不能为空"); return; } if(Stringutil.isEmpty(price)) { JOptionPane.showConfirmDialog(null, "图书价格不能为空"); return; } String sex=""; if(manjrb.isSelected()) { sex="男"; } else if(womenjrb.isSelected()) { sex="女"; } BookType bookType=(BookType)bookTypeJcb.getSelectedItem(); int bookTypeId = bookType.getId(); Book book=new Book(bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc); Connection con=null; try { con=dbUtil.getCon(); int addNum=bookDao.add(con,book); if(addNum==1) { JOptionPane.showConfirmDialog(null, "图书添加成功"); this.resetValue(); return ; } else { JOptionPane.showConfirmDialog(null, "图书添加失败"); } } catch (Exception e2) { // TODO: handle exception JOptionPane.showConfirmDialog(null, "图书添加失败"); e2.printStackTrace(); }finally { try { dbUtil.closeCon(con); } catch (Exception e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } } } /** * 重置表单 */ private void resetValue() { this.bookNametxt.setText(""); this.pricetxt.setText(""); this.authortxt.setText(""); this.manjrb.setSelected(true); this.bookDesctxt.setText(""); //意思就是说如果有下拉框选项,那么就默认选回第一个 if(this.bookTypeJcb.getItemCount()>0) { this.bookTypeJcb.setSelectedIndex(0); } } /** * 初始化图书类别下拉框 */ private void fillBookType() { Connection con=null; BookType bookType=null; try { con=dbUtil.getCon(); ResultSet rs =bookTypeDao.list(con, new BookType()); while(rs.next()) { bookType=new BookType(); bookType.setId(rs.getInt("id")); bookType.setBookTypeName(rs.getString("bookTypeName")); this.bookTypeJcb.addItem(bookType); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally { } } }
10.BookManageInterFrm.java
package com.java1234.view; import java.awt.EventQueue; import javax.swing.JInternalFrame; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import com.java1234.dao.BookDao; import com.java1234.dao.BookTypeDao; import com.java1234.model.Book; import com.java1234.model.BookType; import com.java1234.util.DbUtil; import com.java1234.util.Stringutil; import com.mysql.cj.util.EscapeTokenizer; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.JPanel; import javax.swing.border.TitledBorder; import javax.swing.border.EtchedBorder; import javax.swing.border.LineBorder; import java.awt.Color; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import java.sql.Connection; import java.sql.ResultSet; import java.util.Vector; import javax.swing.JTextField; import javax.swing.JComboBox; import javax.swing.JButton; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JTextArea; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class BookManageInterFrm extends JInternalFrame { private static final long serialVersionUID = 1L; private JTable booktable; private JTextField s_bookNametxt; private JTextField s_authortxt; private JComboBox s_bookTypejcb; private DbUtil dbUtil=new DbUtil(); private BookTypeDao bookTypeDao=new BookTypeDao(); private BookDao bookDao=new BookDao(); private JTextField idtxt; private JTextField bookNametxt; private final ButtonGroup buttonGroup = new ButtonGroup(); private JTextField pricetxt; private JTextField authortxt; private JRadioButton manjrb; private JRadioButton womenjrb; private JTextArea bookDesctxt; private JComboBox bookTypejcb; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { BookManageInterFrm frame = new BookManageInterFrm(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public BookManageInterFrm() { setClosable(true); setIconifiable(true); setTitle("图书管理"); setBounds(100, 100, 743, 728); JScrollPane scrollPane = new JScrollPane(); JPanel panel = new JPanel(); panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0))); JPanel panel_1 = new JPanel(); panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null)); GroupLayout groupLayout = new GroupLayout(getContentPane()); groupLayout.setHorizontalGroup( groupLayout.createParallelGroup(Alignment.TRAILING) .addGroup(groupLayout.createSequentialGroup() .addGap(21) .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING) .addComponent(panel_1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 673, Short.MAX_VALUE) .addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE) .addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(33)) ); groupLayout.setVerticalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGroup(groupLayout.createSequentialGroup() .addContainerGap() .addComponent(panel, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE) .addGap(34) .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE) .addGap(18) .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE) .addContainerGap()) ); JLabel lblNewLabel_3 = new JLabel("编号:"); lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13)); idtxt = new JTextField(); idtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); idtxt.setEnabled(false); idtxt.setColumns(10); JLabel lblNewLabel_4 = new JLabel("图书名称:"); lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 13)); bookNametxt = new JTextField(); bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); bookNametxt.setColumns(10); JLabel lblNewLabel_5 = new JLabel("作者性别:"); lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 13)); manjrb = new JRadioButton("男"); buttonGroup.add(manjrb); manjrb.setSelected(true); manjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13)); womenjrb = new JRadioButton("女"); buttonGroup.add(womenjrb); womenjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13)); JLabel lblNewLabel_6 = new JLabel("价格:"); lblNewLabel_6.setFont(new Font("微软雅黑", Font.PLAIN, 13)); pricetxt = new JTextField(); pricetxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); pricetxt.setColumns(10); JLabel lblNewLabel_7 = new JLabel("图书作者:"); lblNewLabel_7.setFont(new Font("微软雅黑", Font.PLAIN, 13)); authortxt = new JTextField(); authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); authortxt.setColumns(10); JLabel lblNewLabel_8 = new JLabel("图书类别:"); lblNewLabel_8.setFont(new Font("微软雅黑", Font.PLAIN, 13)); bookTypejcb = new JComboBox(); bookTypejcb.setFont(new Font("微软雅黑", Font.PLAIN, 13)); JLabel lblNewLabel_9 = new JLabel("图书描述:"); lblNewLabel_9.setFont(new Font("微软雅黑", Font.PLAIN, 13)); bookDesctxt = new JTextArea(); JButton btnNewButton_1 = new JButton("修改"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bookUpdataActionEvent(e); } }); btnNewButton_1.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/修改.png"))); btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13)); JButton btnNewButton_2 = new JButton("删除"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bookDeleteActionEvent(e); } }); btnNewButton_2.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/删除.png"))); btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 13)); GroupLayout gl_panel_1 = new GroupLayout(panel_1); gl_panel_1.setHorizontalGroup( gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addGap(28) .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(lblNewLabel_9) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 523, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_1.createSequentialGroup() .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(lblNewLabel_3) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(18) .addComponent(lblNewLabel_4) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(lblNewLabel_6) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(18) .addComponent(lblNewLabel_7) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) .addGap(40) .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(lblNewLabel_5) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(manjrb) .addGap(18) .addComponent(womenjrb)) .addGroup(gl_panel_1.createSequentialGroup() .addComponent(lblNewLabel_8) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)))))) .addGroup(gl_panel_1.createSequentialGroup() .addGap(167) .addComponent(btnNewButton_1) .addGap(129) .addComponent(btnNewButton_2))) .addContainerGap(8, Short.MAX_VALUE)) ); gl_panel_1.setVerticalGroup( gl_panel_1.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_1.createSequentialGroup() .addContainerGap() .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE) .addComponent(lblNewLabel_5) .addComponent(lblNewLabel_4) .addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(lblNewLabel_3) .addComponent(manjrb) .addComponent(womenjrb)) .addGap(57) .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE) .addComponent(lblNewLabel_6) .addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(lblNewLabel_7) .addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(lblNewLabel_8) .addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(46) .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE) .addComponent(lblNewLabel_9) .addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)) .addGap(18) .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE) .addComponent(btnNewButton_1) .addComponent(btnNewButton_2)) .addContainerGap(23, Short.MAX_VALUE)) ); panel_1.setLayout(gl_panel_1); JLabel lblNewLabel = new JLabel("图书名称:"); lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13)); s_bookNametxt = new JTextField(); s_bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); s_bookNametxt.setColumns(10); JLabel lblNewLabel_1 = new JLabel("作者:"); lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13)); s_authortxt = new JTextField(); s_authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13)); s_authortxt.setColumns(10); JLabel lblNewLabel_2 = new JLabel("图书类别:"); lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13)); s_bookTypejcb = new JComboBox(); s_bookTypejcb.setFont(new Font("微软雅黑", Font.PLAIN, 13)); JButton btnNewButton = new JButton("查询"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { bookSearchActionPerformed(e); } }); btnNewButton.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/查询.png"))); btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13)); GroupLayout gl_panel = new GroupLayout(panel); gl_panel.setHorizontalGroup( gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel.createSequentialGroup() .addContainerGap() .addComponent(lblNewLabel) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(s_bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(24) .addComponent(lblNewLabel_1) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(s_authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(32) .addComponent(lblNewLabel_2) .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_panel.createParallelGroup(Alignment.LEADING) .addComponent(btnNewButton) .addComponent(s_bookTypejcb, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)) .addContainerGap(31, Short.MAX_VALUE)) ); gl_panel.setVerticalGroup( gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel.createSequentialGroup() .addContainerGap() .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE) .addComponent(lblNewLabel) .addComponent(s_bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(lblNewLabel_1) .addComponent(s_authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(lblNewLabel_2) .addComponent(s_bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(btnNewButton) .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); panel.setLayout(gl_panel); booktable = new JTable(); booktable.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { bookTableMousePressed(e); } }); booktable.setShowGrid(false); scrollPane.setViewportView(booktable); booktable.setModel(new DefaultTableModel( new Object[][] { }, new String[] { "\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u56FE\u4E66\u4F5C\u8005", "\u4F5C\u8005\u6027\u522B", "\u56FE\u4E66\u4EF7\u683C", "\u56FE\u4E66\u63CF\u8FF0", "\u56FE\u4E66\u7C7B\u522B" } ) { boolean[] columnEditables = new boolean[] { true, false, false, false, false, false, false }; public boolean isCellEditable(int row, int column) { return columnEditables[column]; } }); booktable.getColumnModel().getColumn(5).setPreferredWidth(173); getContentPane().setLayout(groupLayout); this.fillBookType("search"); this.fillBookType("modify"); this.fillTable(new Book()); bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false)); bookDesctxt.setLineWrap(true); //激活自动换行功能 bookDesctxt.setWrapStyleWord(true); // 激活断行不断字功能 } /** * 图书删除事件处理 * @param e */ protected void bookDeleteActionEvent(ActionEvent e) { // TODO 自动生成的方法存根 String idString=idtxt.getText(); if(Stringutil.isEmpty(idString)) { JOptionPane.showConfirmDialog(null, "请选择要删除的记录"); return; } int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗"); if(n==0) { Connection con=null; try { con=dbUtil.getCon(); int deteleNum=bookDao.delete(con, idString); if(deteleNum==1) { JOptionPane.showConfirmDialog(null, "删除成功"); this.fillTable(new Book()); this.resetValue(); } else { JOptionPane.showConfirmDialog(null, "删除失败"); } } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); }finally { try { dbUtil.closeCon(con); } catch (Exception e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } } } } /** * 图书修改事件处理 * @param e */ protected void bookUpdataActionEvent(ActionEvent e) { // TODO 自动生成的方法存根 String id=this.idtxt.getText(); if(Stringutil.isEmpty(id)) { JOptionPane.showConfirmDialog(null, "请选择一条记录"); return; } String bookName=this.bookNametxt.getText(); String author=this.authortxt.getText(); String price=this.pricetxt.getText(); String bookDesc=this.bookDesctxt.getText(); if(Stringutil.isEmpty(bookName)) { JOptionPane.showConfirmDialog(null, "图书名称不能为空"); return; } if(Stringutil.isEmpty(author)) { JOptionPane.showConfirmDialog(null, "作者不能为空"); return; } if(Stringutil.isEmpty(price)) { JOptionPane.showConfirmDialog(null, "图书价格不能为空"); return; } String sex=null; if(manjrb.isSelected()) { sex="男"; } else { sex="女"; } BookType bookType=(BookType) bookTypejcb.getSelectedItem(); int bookTypeId=bookType.getId(); Book book=new Book(Integer.parseInt(id), bookName, author, sex, Float.parseFloat(price), bookTypeId, bookDesc); Connection con=null; try { con=dbUtil.getCon(); int addNum=bookDao.updata(con,book); if(addNum==1) { JOptionPane.showConfirmDialog(null, "图书修改成功"); this.resetValue(); this.fillTable(new Book()); return ; } else { JOptionPane.showConfirmDialog(null, "图书修改失败"); } } catch (Exception e2) { // TODO: handle exception JOptionPane.showConfirmDialog(null, "图书修改失败"); e2.printStackTrace(); }finally { try { dbUtil.closeCon(con); } catch (Exception e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } } } private void resetValue() { // TODO 自动生成的方法存根 this.idtxt.setText(""); this.bookNametxt.setText(""); this.pricetxt.setText(""); this.authortxt.setText(""); this.manjrb.setSelected(true); this.bookDesctxt.setText(""); //意思就是说如果有下拉框选项,那么就默认选回第一个 if(this.bookTypejcb.getItemCount()>0) { this.bookTypejcb.setSelectedIndex(0); } } /** * 表格点击事件处理 * @param e */ protected void bookTableMousePressed(MouseEvent e) { // TODO 自动生成的方法存根 int row=this.booktable.getSelectedRow(); this.idtxt.setText((String) booktable.getValueAt(row, 0)); this.bookNametxt.setText((String) booktable.getValueAt(row, 1)); this.authortxt.setText((String) booktable.getValueAt(row, 2)); String sex=(String) booktable.getValueAt(row, 3); if(sex.equals("男")) { this.manjrb.setSelected(true); } else { this.womenjrb.setSelected(true); } this.pricetxt.setText((Float) booktable.getValueAt(row, 4)+""); this.bookDesctxt.setText((String) booktable.getValueAt(row, 5)); String bookTypeName=(String) this.booktable.getValueAt(row, 6); int n=this.bookTypejcb.getItemCount(); for(int i=0;i