数据库期末计划——图书管理体系

海哥  金牌会员 | 2024-8-11 05:07:54 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 880|帖子 880|积分 2640

 
目次
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,写错了代码可就跑不动了。
  1. create table t_booktype
  2. (
  3.     id           int auto_increment
  4.         primary key,
  5.     bookTypeName varchar(20)   null,
  6.     bookTpeDesc  varchar(1000) null
  7. );
  8. create table t_book
  9. (
  10.     id         int auto_increment
  11.         primary key,
  12.     bookName   varchar(20)   not null,
  13.     author     varchar(20)   null,
  14.     sex        varchar(10)   null,
  15.     price      float         null,
  16.     bookTypeId int           null,
  17.     bookDesc   varchar(1000) null,
  18.     constraint t_book_t_booktype_id_fk
  19.         foreign key (bookTypeId) references t_booktype (id)
  20. );
  21. create table t_user
  22. (
  23.     id       int auto_increment
  24.         primary key,
  25.     username varchar(20) not null,
  26.     password varchar(20) not null
  27. );
复制代码
 直接粘贴在你的数据库查询台里就行了,可以自己看看效果,数据不紧张可以自己添加。


这个其实也不是很好讲,我就一个一个来了,先给各人看下整个代码的样子(假如各人最后发现字符集什么的不行导致乱码就发起直接粘贴代码跑一下比力好):
假如有需要特殊留意的地方我会指出,其他的无脑复制即可。

 1.BookDao.java

  1. package com.java1234.dao;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import javax.swing.text.html.HTMLDocument.HTMLReader.ParagraphAction;
  6. import com.java1234.model.Book;
  7. import com.java1234.util.Stringutil;
  8. /**
  9. * book添加和删除
  10. * @author 46476
  11. *
  12. */
  13. public class BookDao {
  14.        
  15.         /**
  16.          * 图书添加
  17.          * @param con
  18.          * @param book
  19.          * @return
  20.          * @throws Exception
  21.          */
  22.         public int add(Connection con,Book book)throws Exception{
  23.                 String sql="insert into t_book values(null,?,?,?,?,?,?)";
  24.                 PreparedStatement pstmt=con.prepareStatement(sql);
  25.                 pstmt.setString(1, book.getBookName());
  26.                 pstmt.setString(2, book.getAuthor());
  27.                 pstmt.setString(3, book.getSex());
  28.                 pstmt.setFloat(4, book.getPrice());
  29.                 pstmt.setInt(5, book.getBookTypeId());
  30.                 pstmt.setString(6, book.getBookTypeDesc());
  31.                 return pstmt.executeUpdate();
  32.         }
  33.        
  34.         /**
  35.          * 图书信息查询
  36.          * @param con
  37.          * @param book
  38.          * @return
  39.          * @throws Exception
  40.          */
  41.         public ResultSet list(Connection con,Book book)throws Exception{
  42.                 StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
  43.                 if(Stringutil.isNotEmpty(book.getBookName())) {
  44.                         sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
  45.                 }
  46.                 if(Stringutil.isNotEmpty(book.getAuthor())) {
  47.                         sb.append(" and b.author like '%"+book.getAuthor()+"%'");
  48.                 }
  49.                 if(book.getBookTypeId()!=null&&book.getBookTypeId()!=-1) {
  50.                         sb.append(" and b.bookTypeId="+book.getBookTypeId());
  51.                 }
  52.                 PreparedStatement pstmt=con.prepareStatement(sb.toString());
  53.                 return pstmt.executeQuery();
  54.         }
  55.        
  56.        
  57.         /**
  58.          * 删除记录条数
  59.          * @param con
  60.          * @param id
  61.          * @return
  62.          * @throws Exception
  63.          */
  64.         public int delete(Connection con,String id)throws Exception{
  65.                 String sql="delete from t_book where id=?";
  66.                 PreparedStatement pstmt=con.prepareStatement(sql);
  67.                 pstmt.setString(1, id);
  68.                 return pstmt.executeUpdate();
  69.         }
  70.        
  71.         public int updata(Connection con,Book book)throws Exception {
  72.                 String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookTypeId=?,bookDesc=? where id=?";
  73.                 PreparedStatement pstmt=con.prepareStatement(sql);
  74.                 pstmt.setString(1, book.getBookName());
  75.                 pstmt.setString(2, book.getAuthor());
  76.                 pstmt.setString(3, book.getSex());
  77.                 pstmt.setFloat(4, book.getPrice());
  78.                 pstmt.setInt(5, book.getBookTypeId());
  79.                 pstmt.setString(6, book.getBookTypeDesc());//这边之前写错了应该是bookDesc
  80.                 pstmt.setInt(7, book.getId());
  81.                 return pstmt.executeUpdate();
  82.         }
  83.        
  84.        
  85.         /**
  86.          * 指定图书类别下是否存在图书
  87.          * @param con
  88.          * @param bookTypeId
  89.          * @return
  90.          * @throws Exception
  91.          */
  92.         public boolean existBook(Connection con,String bookTypeId)throws Exception {
  93.                 String sql="select * from t_book where bookTypeId=?";
  94.                 PreparedStatement pstmt=con.prepareStatement(sql);
  95.                 pstmt.setString(1, bookTypeId);
  96.                 ResultSet rs=pstmt.executeQuery();
  97.                 return rs.next();
  98.         }
  99. }
复制代码
2.BookTypeDao.java

  1. package com.java1234.dao;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import com.java1234.model.BookType;
  6. import com.java1234.util.Stringutil;
  7. /**
  8. * 图书类别dao类
  9. * @author 46476
  10. *
  11. */
  12. public class BookTypeDao {
  13.         /**
  14.          * 图书类别添加
  15.          * @param con
  16.          * @param bookType
  17.          * @return
  18.          * @throws Exception
  19.          */
  20.         public int add(Connection con,BookType bookType)throws Exception{
  21.                 String sql="insert into t_bookType value(null,?,?)";
  22.                 PreparedStatement pstmt=con.prepareStatement(sql);
  23.                 pstmt.setString(1, bookType.getBookTypeName());
  24.                 pstmt.setString(2,bookType.getBookTypeDesc() );
  25.                 return pstmt.executeUpdate();
  26.         }
  27.        
  28.         /**
  29.          * 查询图书类别
  30.          * @param con
  31.          * @param bookType
  32.          * @return
  33.          * @throws Exception
  34.          */
  35.         public ResultSet list(Connection con,BookType bookType)throws Exception{
  36.                 StringBuffer sb=new StringBuffer("select * from t_bookType");
  37.                 if(Stringutil.isNotEmpty(bookType.getBookTypeName())) {
  38.                         sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
  39.                 }
  40.                 PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
  41.                 return pstmt.executeQuery();
  42.         }
  43.        
  44.        
  45.         /**
  46.          * 删除图书类别
  47.          * @param con
  48.          * @param id
  49.          * @return
  50.          * @throws Exception
  51.          */
  52.         public int delete(Connection con,String id)throws Exception{
  53.                 String sql="delete from t_bookType where id=?";
  54.                 PreparedStatement pstmt=con.prepareStatement(sql);
  55.                 pstmt.setString(1, id);
  56.                 return pstmt.executeUpdate();
  57.         }
  58.        
  59.        
  60.         /**
  61.          * 跟新图书类别
  62.          * @param con
  63.          * @param bookType
  64.          * @return
  65.          * @throws Exception
  66.          */
  67.         public int updata(Connection con,BookType bookType)throws Exception{
  68.                 String sql="update t_bookType set bookTypeName=?,bookTpeDesc=? where id=?";
  69.                 PreparedStatement pstmt=con.prepareStatement(sql);
  70.                 pstmt.setString(1, bookType.getBookTypeName());
  71.                 pstmt.setString(2, bookType.getBookTypeDesc());
  72.                 pstmt.setInt(3, bookType.getId());
  73.                 return pstmt.executeUpdate();
  74.         }
  75. }
复制代码
3.UserDao.java

  1. package com.java1234.dao;
  2. import java.nio.channels.SelectableChannel;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import com.java1234.model.User;
  7. /**
  8. * 用户dao类
  9. * @author 46476
  10. *
  11. */
  12. public class UserDao {
  13.         public User login(Connection con,User user)throws Exception {
  14.                 User resultUser=null;
  15.                 String sql="select * from t_user where userName=? and password=?";
  16.                 PreparedStatement pstmt=con.prepareStatement(sql);
  17.                 pstmt.setString(1, user.getUserNameString());
  18.                 pstmt.setString(2, user.getPasswordString());
  19.                 ResultSet rs=pstmt.executeQuery();
  20.                 if(rs.next()) {
  21.                         resultUser=new User();
  22.                         resultUser.setId(rs.getInt("id"));
  23.                         resultUser.setUserNameString(rs.getString("userName"));
  24.                         resultUser.setPasswordString(rs.getString("password"));
  25.                 }
  26.                 return resultUser;
  27.         }
  28. }
复制代码
4.Book.java

  1. package com.java1234.model;
  2. import java.sql.Connection;
  3. import com.mysql.cj.protocol.a.NativeConstants.StringLengthDataType;
  4. public class Book {
  5.         private int id;
  6.         private String bookName;
  7.         private String author;
  8.         private String sex;
  9.         private Float price;
  10.         private Integer bookTypeId;
  11.         private String bookTypeName;
  12.         private String bookTypeDesc;
  13.        
  14.        
  15.         public Book() {
  16.                 super();
  17.                 // TODO 自动生成的构造函数存根
  18.         }
  19.        
  20.        
  21.        
  22.         public Book(int id, String bookName, String author, String sex, Float price, Integer bookTypeId,
  23.                         String bookTypeDesc) {
  24.                 super();
  25.                 this.id = id;
  26.                 this.bookName = bookName;
  27.                 this.author = author;
  28.                 this.sex = sex;
  29.                 this.price = price;
  30.                 this.bookTypeId = bookTypeId;
  31.                 this.bookTypeDesc = bookTypeDesc;
  32.         }
  33.         public Book(String bookName, String author, Integer bookTypeId) {
  34.                 super();
  35.                 this.bookName = bookName;
  36.                 this.author = author;
  37.                 this.bookTypeId = bookTypeId;
  38.         }
  39.         public Book(String bookName, String author, String sex, Float price, Integer bookTypeId, String bookTypeDesc) {
  40.                 super();
  41.                 this.bookName = bookName;
  42.                 this.author = author;
  43.                 this.sex = sex;
  44.                 this.price = price;
  45.                 this.bookTypeId = bookTypeId;
  46.                 this.bookTypeDesc = bookTypeDesc;
  47.         }
  48.         public int getId() {
  49.                 return id;
  50.         }
  51.         public void setId(int id) {
  52.                 this.id = id;
  53.         }
  54.         public String getBookName() {
  55.                 return bookName;
  56.         }
  57.         public void setBookName(String bookName) {
  58.                 this.bookName = bookName;
  59.         }
  60.         public String getAuthor() {
  61.                 return author;
  62.         }
  63.         public void setAuthor(String author) {
  64.                 this.author = author;
  65.         }
  66.         public String getSex() {
  67.                 return sex;
  68.         }
  69.         public void setSex(String sex) {
  70.                 this.sex = sex;
  71.         }
  72.         public Float getPrice() {
  73.                 return price;
  74.         }
  75.         public void setPrice(Float price) {
  76.                 this.price = price;
  77.         }
  78.         public Integer getBookTypeId() {
  79.                 return bookTypeId;
  80.         }
  81.         public void setBookTypeId(Integer bookTypeId) {
  82.                 this.bookTypeId = bookTypeId;
  83.         }
  84.         public String getBookTypeName() {
  85.                 return bookTypeName;
  86.         }
  87.         public void setBookTypeName(String bookTypeName) {
  88.                 this.bookTypeName = bookTypeName;
  89.         }
  90.         public String getBookTypeDesc() {
  91.                 return bookTypeDesc;
  92.         }
  93.         public void setBookTypeDesc(String bookTypeDesc) {
  94.                 this.bookTypeDesc = bookTypeDesc;
  95.         }
  96.        
  97. }
复制代码
5.BookType.java

  1. package com.java1234.model;
  2. /**
  3. * 图书类别实体
  4. * @author 46476
  5. *
  6. */
  7. public class BookType {
  8.         private int id;//编号
  9.         private String bookTypeName;//图书类别名称
  10.         private String bookTypeDesc;//图书备注
  11.         public BookType() {
  12.                 super();
  13.         }
  14.        
  15.         public BookType(int id, String bookTypeName, String bookTypeDesc) {
  16.                 super();
  17.                 this.id = id;
  18.                 this.bookTypeName = bookTypeName;
  19.                 this.bookTypeDesc = bookTypeDesc;
  20.         }
  21.         public BookType(String bookTypeName,String bookTypeDesc) {
  22.                 // TODO 自动生成的构造函数存根
  23.                 this.bookTypeName=bookTypeName;
  24.                 this.bookTypeDesc=bookTypeDesc;
  25.         }
  26.         public int getId() {
  27.                 return id;
  28.         }
  29.         public void setId(int id) {
  30.                 this.id = id;
  31.         }
  32.         public String getBookTypeName() {
  33.                 return bookTypeName;
  34.         }
  35.         public void setBookTypeName(String bookTypeName) {
  36.                 this.bookTypeName = bookTypeName;
  37.         }
  38.         public String getBookTypeDesc() {
  39.                 return bookTypeDesc;
  40.         }
  41.         public void setBookTypeDesc(String bookTypeDesc) {
  42.                 this.bookTypeDesc = bookTypeDesc;
  43.         }
  44.         public String toString() {
  45.                 return this.bookTypeName;
  46.         }
  47.        
  48. }
复制代码
6.User.java

  1. package com.java1234.model;
  2. /**
  3. * 用户实体
  4. * @author 46476
  5. *
  6. */
  7. public class User {
  8.         private int id;//编号
  9.         private String userNameString;//用户名
  10.         private String passwordString;//密码
  11.        
  12.        
  13.        
  14.         public User() {
  15.                 super();
  16.         }
  17.        
  18.        
  19.         public User(String userNameString, String passwordString) {
  20.                 super();
  21.                 this.userNameString = userNameString;
  22.                 this.passwordString = passwordString;
  23.         }
  24.         public int getId() {
  25.                 return id;
  26.         }
  27.         public void setId(int id) {
  28.                 this.id = id;
  29.         }
  30.         public String getUserNameString() {
  31.                 return userNameString;
  32.         }
  33.         public void setUserNameString(String userNameString) {
  34.                 this.userNameString = userNameString;
  35.         }
  36.         public String getPasswordString() {
  37.                 return passwordString;
  38.         }
  39.         public void setPasswordString(String passwordString) {
  40.                 this.passwordString = passwordString;
  41.         }
  42.        
  43. }
复制代码
7.DbUtil.java

这里留意啦!!!!!!!!!!!!!!!
这里面的用户名以及暗码都是要填写自己的数据库用户名以及暗码,也就是我打xxxx的地方。
  1. package com.java1234.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. /**
  5. * 数据库工具类
  6. * @author 46476
  7. *
  8. */
  9. public class DbUtil {
  10.         private String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//数据库地址
  11.         private String dbUserName="xxxxxx";//用户名
  12.         private String dbPassword="xxxxxxxx";//密码
  13.         private String jdbcNameString="com.mysql.cj.jdbc.Driver";//驱动名称
  14.        
  15.         public Connection getCon()throws Exception{
  16.                 Class.forName(jdbcNameString);
  17.                 Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
  18.                 return con;
  19.         }
  20.        
  21.         public void closeCon(Connection con)throws Exception{
  22.                 if(con!=null)
  23.                         con.close();
  24.         }
  25.        
  26.         public static void main(String[] args) {
  27.                 DbUtil dbUtil=new DbUtil();
  28.                 try {
  29.                         dbUtil.getCon();
  30.                         System.out.println("数据库连接成功");
  31.                 } catch (Exception e) {
  32.                         // TODO 自动生成的 catch 块
  33.                         e.printStackTrace();
  34.                         System.out.println("连接失败");
  35.                 }
  36.         }
  37. }
复制代码
8.Stringutil.java

  1. package com.java1234.util;
  2. /**
  3. * 字符串工具类
  4. * @author 46476
  5. *
  6. */
  7. public class Stringutil {
  8.        
  9.         //判断字符串是否为空
  10.         public static boolean isEmpty(String str) {
  11.                 if(str==null||"".equals(str.trim())) {
  12.                         return true;
  13.                 }
  14.                 return false;
  15.         }
  16.        
  17.         public static boolean isNotEmpty(String str) {
  18.                 if(str!=null&&"".equals(str.trim())==false)
  19.                         return true;
  20.                 return false;
  21.         }
  22. }
复制代码
 
从这里开始我需要提一嘴:中央的一大段代码都是不消看的,这些都是windowBuilder帮你主动生成的,你只需要知道怎么微调就行了,不会的无脑cv,会的可以自己调成喜欢的样子。 
9.BookAddInterFrm.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JInternalFrame;
  4. import javax.swing.GroupLayout;
  5. import javax.swing.GroupLayout.Alignment;
  6. import javax.swing.JLabel;
  7. import javax.swing.JOptionPane;
  8. import java.awt.Font;
  9. import java.sql.Connection;
  10. import java.sql.ResultSet;
  11. import javax.swing.JTextField;
  12. import javax.swing.LayoutStyle.ComponentPlacement;
  13. import javax.swing.border.LineBorder;
  14. import com.java1234.dao.BookDao;
  15. import com.java1234.dao.BookTypeDao;
  16. import com.java1234.model.Book;
  17. import com.java1234.model.BookType;
  18. import com.java1234.util.DbUtil;
  19. import com.java1234.util.Stringutil;
  20. import javax.swing.JRadioButton;
  21. import javax.swing.ButtonGroup;
  22. import javax.swing.JTextArea;
  23. import javax.swing.JComboBox;
  24. import javax.swing.JButton;
  25. import javax.swing.ImageIcon;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.ActionEvent;
  28. public class BookAddInterFrm extends JInternalFrame {
  29.         private static final long serialVersionUID = 1L;
  30.         private JTextField bookNametxt;
  31.         private JTextField authortxt;
  32.         private final ButtonGroup buttonGroup = new ButtonGroup();
  33.         private JTextField pricetxt;
  34.         private JTextArea bookDesctxt;
  35.         private JComboBox bookTypeJcb;
  36.         private DbUtil dbUtil=new DbUtil();
  37.         private BookTypeDao bookTypeDao=new BookTypeDao();
  38.         private BookDao bookDao=new BookDao();
  39.         private JRadioButton manjrb;
  40.         JRadioButton womenjrb;
  41.         /**
  42.          * Launch the application.
  43.          */
  44.         public static void main(String[] args) {
  45.                 EventQueue.invokeLater(new Runnable() {
  46.                         public void run() {
  47.                                 try {
  48.                                         BookAddInterFrm frame = new BookAddInterFrm();
  49.                                         frame.setVisible(true);
  50.                                 } catch (Exception e) {
  51.                                         e.printStackTrace();
  52.                                 }
  53.                         }
  54.                 });
  55.         }
  56.         /**
  57.          * Create the frame.
  58.          */
  59.         public BookAddInterFrm() {
  60.                 setClosable(true);
  61.                 setIconifiable(true);
  62.                 setTitle("图书添加");
  63.                 setBounds(100, 100, 602, 705);
  64.                
  65.                 JLabel lblNewLabel = new JLabel("图书名称:");
  66.                 lblNewLabel.setBounds(53, 82, 65, 18);
  67.                 lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  68.                
  69.                 bookNametxt = new JTextField();
  70.                 bookNametxt.setBounds(136, 79, 126, 24);
  71.                 bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  72.                 bookNametxt.setColumns(10);
  73.                
  74.                 JLabel lblNewLabel_1 = new JLabel("图书作者:");
  75.                 lblNewLabel_1.setBounds(336, 82, 55, 18);
  76.                 lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  77.                
  78.                 authortxt = new JTextField();
  79.                 authortxt.setBounds(409, 79, 126, 24);
  80.                 authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  81.                 authortxt.setColumns(10);
  82.                
  83.                 JLabel lblNewLabel_2 = new JLabel("作者性别:");
  84.                 lblNewLabel_2.setBounds(53, 168, 65, 18);
  85.                 lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  86.                
  87.                 manjrb = new JRadioButton("男");
  88.                 manjrb.setBounds(136, 164, 39, 27);
  89.                 buttonGroup.add(manjrb);
  90.                 manjrb.setSelected(true);
  91.                 manjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  92.                
  93.                 womenjrb = new JRadioButton("女");
  94.                 womenjrb.setBounds(193, 164, 39, 27);
  95.                 buttonGroup.add(womenjrb);
  96.                 womenjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  97.                
  98.                 JLabel lblNewLabel_3 = new JLabel("图书价格:");
  99.                 lblNewLabel_3.setBounds(336, 168, 65, 18);
  100.                 lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  101.                
  102.                 pricetxt = new JTextField();
  103.                 pricetxt.setBounds(411, 165, 126, 24);
  104.                 pricetxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  105.                 pricetxt.setColumns(10);
  106.                
  107.                 JLabel lblNewLabel_4 = new JLabel("图书描述:");
  108.                 lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  109.                 lblNewLabel_4.setBounds(53, 353, 65, 15);
  110.                
  111.                 bookDesctxt = new JTextArea();
  112.                 bookDesctxt.setBounds(136, 348, 399, 242);
  113.                 getContentPane().setLayout(null);
  114.                 getContentPane().add(lblNewLabel);
  115.                 getContentPane().add(bookNametxt);
  116.                 getContentPane().add(lblNewLabel_4);
  117.                 getContentPane().add(lblNewLabel_2);
  118.                 getContentPane().add(manjrb);
  119.                 getContentPane().add(womenjrb);
  120.                 getContentPane().add(bookDesctxt);
  121.                 getContentPane().add(lblNewLabel_1);
  122.                 getContentPane().add(authortxt);
  123.                 getContentPane().add(lblNewLabel_3);
  124.                 getContentPane().add(pricetxt);
  125.                
  126.                 JLabel lblNewLabel_5 = new JLabel("图书类别:");
  127.                 lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  128.                 lblNewLabel_5.setBounds(53, 273, 65, 15);
  129.                 getContentPane().add(lblNewLabel_5);
  130.                
  131.                 bookTypeJcb = new JComboBox();
  132.                 bookTypeJcb.setBounds(136, 270, 126, 23);
  133.                 getContentPane().add(bookTypeJcb);
  134.                
  135.                 JButton btnNewButton = new JButton("添加");
  136.                 btnNewButton.addActionListener(new ActionListener() {
  137.                         public void actionPerformed(ActionEvent e) {
  138.                                 bookAddActionPerformed(e);
  139.                         }
  140.                 });
  141.                 btnNewButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/添加.png")));
  142.                 btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  143.                 btnNewButton.setBounds(220, 620, 93, 23);
  144.                 getContentPane().add(btnNewButton);
  145.                
  146.                 JButton btnNewButton_1 = new JButton("重置");
  147.                 btnNewButton_1.addActionListener(new ActionListener() {
  148.                         public void actionPerformed(ActionEvent e) {
  149.                                 resetValueActionPerformed(e);
  150.                         }
  151.                 });
  152.                 btnNewButton_1.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/重置.png")));
  153.                 btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  154.                 btnNewButton_1.setBounds(370, 620, 93, 23);
  155.                 getContentPane().add(btnNewButton_1);
  156.                
  157.                 bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
  158.                 bookDesctxt.setLineWrap(true);        //激活自动换行功能
  159.                 bookDesctxt.setWrapStyleWord(true);            // 激活断行不断字功能
  160.                
  161.                 fillBookType();
  162.         }
  163.        
  164.        
  165.         protected void resetValueActionPerformed(ActionEvent e) {
  166.                 // TODO 自动生成的方法存根
  167.                 this.resetValue();
  168.         }
  169.         /**
  170.          * 图书添加处理
  171.          * @param e
  172.          */
  173.         protected void bookAddActionPerformed(ActionEvent e) {
  174.                 // TODO 自动生成的方法存根
  175.                 String bookName=this.bookNametxt.getText();
  176.                 String author=this.authortxt.getText();
  177.                 String price=this.pricetxt.getText();
  178.                 String bookDesc=this.bookDesctxt.getText();
  179.                
  180.                 if(Stringutil.isEmpty(bookName)) {
  181.                         JOptionPane.showConfirmDialog(null, "图书名称不能为空");
  182.                         return;
  183.                 }
  184.                 if(Stringutil.isEmpty(author)) {
  185.                         JOptionPane.showConfirmDialog(null, "作者不能为空");
  186.                         return;
  187.                 }
  188.                 if(Stringutil.isEmpty(price)) {
  189.                         JOptionPane.showConfirmDialog(null, "图书价格不能为空");
  190.                         return;
  191.                 }
  192.                
  193.                 String sex="";
  194.                 if(manjrb.isSelected()) {
  195.                         sex="男";
  196.                 }
  197.                 else if(womenjrb.isSelected()) {
  198.                         sex="女";
  199.                 }
  200.                
  201.                 BookType bookType=(BookType)bookTypeJcb.getSelectedItem();
  202.                 int bookTypeId = bookType.getId();
  203.                
  204.                 Book book=new Book(bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc);
  205.                 Connection con=null;
  206.                 try {
  207.                         con=dbUtil.getCon();
  208.                         int addNum=bookDao.add(con,book);
  209.                         if(addNum==1) {
  210.                                 JOptionPane.showConfirmDialog(null, "图书添加成功");
  211.                                 this.resetValue();
  212.                                 return ;
  213.                         }
  214.                         else {
  215.                             JOptionPane.showConfirmDialog(null, "图书添加失败");
  216.                         }
  217.                 } catch (Exception e2) {
  218.                         // TODO: handle exception
  219.                         JOptionPane.showConfirmDialog(null, "图书添加失败");
  220.                         e2.printStackTrace();
  221.                 }finally {
  222.                         try {
  223.                                 dbUtil.closeCon(con);
  224.                         } catch (Exception e1) {
  225.                                 // TODO 自动生成的 catch 块
  226.                                 e1.printStackTrace();
  227.                         }
  228.                 }
  229.         }
  230.        
  231.         /**
  232.          * 重置表单
  233.          */
  234.         private void resetValue() {
  235.                 this.bookNametxt.setText("");
  236.                 this.pricetxt.setText("");
  237.                 this.authortxt.setText("");
  238.                 this.manjrb.setSelected(true);
  239.                 this.bookDesctxt.setText("");
  240.                 //意思就是说如果有下拉框选项,那么就默认选回第一个
  241.                 if(this.bookTypeJcb.getItemCount()>0) {
  242.                         this.bookTypeJcb.setSelectedIndex(0);
  243.                 }
  244.         }
  245.         /**
  246.          * 初始化图书类别下拉框
  247.          */
  248.         private void fillBookType() {
  249.                 Connection con=null;
  250.                 BookType bookType=null;
  251.                 try {
  252.                         con=dbUtil.getCon();
  253.                         ResultSet rs =bookTypeDao.list(con, new BookType());
  254.                         while(rs.next()) {
  255.                                 bookType=new BookType();
  256.                                 bookType.setId(rs.getInt("id"));
  257.                                 bookType.setBookTypeName(rs.getString("bookTypeName"));
  258.                                 this.bookTypeJcb.addItem(bookType);
  259.                         }
  260.                 } catch (Exception e) {
  261.                         // TODO: handle exception
  262.                         e.printStackTrace();
  263.                 }finally {
  264.                        
  265.                 }
  266.         }
  267. }
复制代码
10.BookManageInterFrm.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JInternalFrame;
  4. import javax.swing.GroupLayout;
  5. import javax.swing.GroupLayout.Alignment;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.JTable;
  8. import javax.swing.table.DefaultTableModel;
  9. import com.java1234.dao.BookDao;
  10. import com.java1234.dao.BookTypeDao;
  11. import com.java1234.model.Book;
  12. import com.java1234.model.BookType;
  13. import com.java1234.util.DbUtil;
  14. import com.java1234.util.Stringutil;
  15. import com.mysql.cj.util.EscapeTokenizer;
  16. import javax.swing.LayoutStyle.ComponentPlacement;
  17. import javax.swing.JPanel;
  18. import javax.swing.border.TitledBorder;
  19. import javax.swing.border.EtchedBorder;
  20. import javax.swing.border.LineBorder;
  21. import java.awt.Color;
  22. import javax.swing.JLabel;
  23. import javax.swing.JOptionPane;
  24. import java.awt.Font;
  25. import java.sql.Connection;
  26. import java.sql.ResultSet;
  27. import java.util.Vector;
  28. import javax.swing.JTextField;
  29. import javax.swing.JComboBox;
  30. import javax.swing.JButton;
  31. import javax.swing.ImageIcon;
  32. import java.awt.event.ActionListener;
  33. import java.awt.event.ActionEvent;
  34. import javax.swing.JRadioButton;
  35. import javax.swing.ButtonGroup;
  36. import javax.swing.JTextArea;
  37. import java.awt.event.MouseAdapter;
  38. import java.awt.event.MouseEvent;
  39. public class BookManageInterFrm extends JInternalFrame {
  40.         private static final long serialVersionUID = 1L;
  41.         private JTable booktable;
  42.         private JTextField s_bookNametxt;
  43.         private JTextField s_authortxt;
  44.         private JComboBox s_bookTypejcb;
  45.        
  46.         private DbUtil dbUtil=new DbUtil();
  47.         private BookTypeDao bookTypeDao=new BookTypeDao();
  48.         private BookDao bookDao=new BookDao();
  49.         private JTextField idtxt;
  50.         private JTextField bookNametxt;
  51.         private final ButtonGroup buttonGroup = new ButtonGroup();
  52.         private JTextField pricetxt;
  53.         private JTextField authortxt;
  54.         private JRadioButton manjrb;
  55.         private JRadioButton womenjrb;
  56.         private JTextArea bookDesctxt;
  57.         private JComboBox bookTypejcb;
  58.         /**
  59.          * Launch the application.
  60.          */
  61.         public static void main(String[] args) {
  62.                 EventQueue.invokeLater(new Runnable() {
  63.                         public void run() {
  64.                                 try {
  65.                                         BookManageInterFrm frame = new BookManageInterFrm();
  66.                                         frame.setVisible(true);
  67.                                 } catch (Exception e) {
  68.                                         e.printStackTrace();
  69.                                 }
  70.                         }
  71.                 });
  72.         }
  73.         /**
  74.          * Create the frame.
  75.          */
  76.         public BookManageInterFrm() {
  77.                 setClosable(true);
  78.                 setIconifiable(true);
  79.                 setTitle("图书管理");
  80.                 setBounds(100, 100, 743, 728);
  81.                
  82.                 JScrollPane scrollPane = new JScrollPane();
  83.                
  84.                 JPanel panel = new JPanel();
  85.                 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)));
  86.                
  87.                 JPanel panel_1 = new JPanel();
  88.                 panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  89.                 GroupLayout groupLayout = new GroupLayout(getContentPane());
  90.                 groupLayout.setHorizontalGroup(
  91.                         groupLayout.createParallelGroup(Alignment.TRAILING)
  92.                                 .addGroup(groupLayout.createSequentialGroup()
  93.                                         .addGap(21)
  94.                                         .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
  95.                                                 .addComponent(panel_1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 673, Short.MAX_VALUE)
  96.                                                 .addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE)
  97.                                                 .addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  98.                                         .addGap(33))
  99.                 );
  100.                 groupLayout.setVerticalGroup(
  101.                         groupLayout.createParallelGroup(Alignment.LEADING)
  102.                                 .addGroup(groupLayout.createSequentialGroup()
  103.                                         .addContainerGap()
  104.                                         .addComponent(panel, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
  105.                                         .addGap(34)
  106.                                         .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
  107.                                         .addGap(18)
  108.                                         .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
  109.                                         .addContainerGap())
  110.                 );
  111.                
  112.                 JLabel lblNewLabel_3 = new JLabel("编号:");
  113.                 lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  114.                
  115.                 idtxt = new JTextField();
  116.                 idtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  117.                 idtxt.setEnabled(false);
  118.                 idtxt.setColumns(10);
  119.                
  120.                 JLabel lblNewLabel_4 = new JLabel("图书名称:");
  121.                 lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  122.                
  123.                 bookNametxt = new JTextField();
  124.                 bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  125.                 bookNametxt.setColumns(10);
  126.                
  127.                 JLabel lblNewLabel_5 = new JLabel("作者性别:");
  128.                 lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  129.                
  130.                 manjrb = new JRadioButton("男");
  131.                 buttonGroup.add(manjrb);
  132.                 manjrb.setSelected(true);
  133.                 manjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  134.                
  135.                 womenjrb = new JRadioButton("女");
  136.                 buttonGroup.add(womenjrb);
  137.                 womenjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  138.                
  139.                 JLabel lblNewLabel_6 = new JLabel("价格:");
  140.                 lblNewLabel_6.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  141.                
  142.                 pricetxt = new JTextField();
  143.                 pricetxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  144.                 pricetxt.setColumns(10);
  145.                
  146.                 JLabel lblNewLabel_7 = new JLabel("图书作者:");
  147.                 lblNewLabel_7.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  148.                
  149.                 authortxt = new JTextField();
  150.                 authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  151.                 authortxt.setColumns(10);
  152.                
  153.                 JLabel lblNewLabel_8 = new JLabel("图书类别:");
  154.                 lblNewLabel_8.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  155.                
  156.                 bookTypejcb = new JComboBox();
  157.                 bookTypejcb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  158.                
  159.                 JLabel lblNewLabel_9 = new JLabel("图书描述:");
  160.                 lblNewLabel_9.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  161.                
  162.                 bookDesctxt = new JTextArea();
  163.                
  164.                 JButton btnNewButton_1 = new JButton("修改");
  165.                 btnNewButton_1.addActionListener(new ActionListener() {
  166.                         public void actionPerformed(ActionEvent e) {
  167.                                 bookUpdataActionEvent(e);
  168.                         }
  169.                 });
  170.                 btnNewButton_1.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/修改.png")));
  171.                 btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  172.                
  173.                 JButton btnNewButton_2 = new JButton("删除");
  174.                 btnNewButton_2.addActionListener(new ActionListener() {
  175.                         public void actionPerformed(ActionEvent e) {
  176.                                 bookDeleteActionEvent(e);
  177.                         }
  178.                 });
  179.                 btnNewButton_2.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/删除.png")));
  180.                 btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  181.                 GroupLayout gl_panel_1 = new GroupLayout(panel_1);
  182.                 gl_panel_1.setHorizontalGroup(
  183.                         gl_panel_1.createParallelGroup(Alignment.LEADING)
  184.                                 .addGroup(gl_panel_1.createSequentialGroup()
  185.                                         .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
  186.                                                 .addGroup(gl_panel_1.createSequentialGroup()
  187.                                                         .addGap(28)
  188.                                                         .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
  189.                                                                 .addGroup(gl_panel_1.createSequentialGroup()
  190.                                                                         .addComponent(lblNewLabel_9)
  191.                                                                         .addPreferredGap(ComponentPlacement.RELATED)
  192.                                                                         .addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 523, GroupLayout.PREFERRED_SIZE))
  193.                                                                 .addGroup(gl_panel_1.createSequentialGroup()
  194.                                                                         .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
  195.                                                                                 .addGroup(gl_panel_1.createSequentialGroup()
  196.                                                                                         .addComponent(lblNewLabel_3)
  197.                                                                                         .addPreferredGap(ComponentPlacement.RELATED)
  198.                                                                                         .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  199.                                                                                         .addGap(18)
  200.                                                                                         .addComponent(lblNewLabel_4)
  201.                                                                                         .addPreferredGap(ComponentPlacement.RELATED)
  202.                                                                                         .addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  203.                                                                                 .addGroup(gl_panel_1.createSequentialGroup()
  204.                                                                                         .addComponent(lblNewLabel_6)
  205.                                                                                         .addPreferredGap(ComponentPlacement.RELATED)
  206.                                                                                         .addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  207.                                                                                         .addGap(18)
  208.                                                                                         .addComponent(lblNewLabel_7)
  209.                                                                                         .addPreferredGap(ComponentPlacement.RELATED)
  210.                                                                                         .addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
  211.                                                                         .addGap(40)
  212.                                                                         .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
  213.                                                                                 .addGroup(gl_panel_1.createSequentialGroup()
  214.                                                                                         .addComponent(lblNewLabel_5)
  215.                                                                                         .addPreferredGap(ComponentPlacement.RELATED)
  216.                                                                                         .addComponent(manjrb)
  217.                                                                                         .addGap(18)
  218.                                                                                         .addComponent(womenjrb))
  219.                                                                                 .addGroup(gl_panel_1.createSequentialGroup()
  220.                                                                                         .addComponent(lblNewLabel_8)
  221.                                                                                         .addPreferredGap(ComponentPlacement.RELATED)
  222.                                                                                         .addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE))))))
  223.                                                 .addGroup(gl_panel_1.createSequentialGroup()
  224.                                                         .addGap(167)
  225.                                                         .addComponent(btnNewButton_1)
  226.                                                         .addGap(129)
  227.                                                         .addComponent(btnNewButton_2)))
  228.                                         .addContainerGap(8, Short.MAX_VALUE))
  229.                 );
  230.                 gl_panel_1.setVerticalGroup(
  231.                         gl_panel_1.createParallelGroup(Alignment.LEADING)
  232.                                 .addGroup(gl_panel_1.createSequentialGroup()
  233.                                         .addContainerGap()
  234.                                         .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
  235.                                                 .addComponent(lblNewLabel_5)
  236.                                                 .addComponent(lblNewLabel_4)
  237.                                                 .addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  238.                                                 .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  239.                                                 .addComponent(lblNewLabel_3)
  240.                                                 .addComponent(manjrb)
  241.                                                 .addComponent(womenjrb))
  242.                                         .addGap(57)
  243.                                         .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
  244.                                                 .addComponent(lblNewLabel_6)
  245.                                                 .addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  246.                                                 .addComponent(lblNewLabel_7)
  247.                                                 .addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  248.                                                 .addComponent(lblNewLabel_8)
  249.                                                 .addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  250.                                         .addGap(46)
  251.                                         .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
  252.                                                 .addComponent(lblNewLabel_9)
  253.                                                 .addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
  254.                                         .addGap(18)
  255.                                         .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
  256.                                                 .addComponent(btnNewButton_1)
  257.                                                 .addComponent(btnNewButton_2))
  258.                                         .addContainerGap(23, Short.MAX_VALUE))
  259.                 );
  260.                 panel_1.setLayout(gl_panel_1);
  261.                
  262.                 JLabel lblNewLabel = new JLabel("图书名称:");
  263.                 lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  264.                
  265.                 s_bookNametxt = new JTextField();
  266.                 s_bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  267.                 s_bookNametxt.setColumns(10);
  268.                
  269.                 JLabel lblNewLabel_1 = new JLabel("作者:");
  270.                 lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  271.                
  272.                 s_authortxt = new JTextField();
  273.                 s_authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  274.                 s_authortxt.setColumns(10);
  275.                
  276.                 JLabel lblNewLabel_2 = new JLabel("图书类别:");
  277.                 lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  278.                
  279.                 s_bookTypejcb = new JComboBox();
  280.                 s_bookTypejcb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  281.                
  282.                 JButton btnNewButton = new JButton("查询");
  283.                 btnNewButton.addActionListener(new ActionListener() {
  284.                         public void actionPerformed(ActionEvent e) {
  285.                                 bookSearchActionPerformed(e);
  286.                         }
  287.                 });
  288.                 btnNewButton.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/查询.png")));
  289.                 btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  290.                 GroupLayout gl_panel = new GroupLayout(panel);
  291.                 gl_panel.setHorizontalGroup(
  292.                         gl_panel.createParallelGroup(Alignment.LEADING)
  293.                                 .addGroup(gl_panel.createSequentialGroup()
  294.                                         .addContainerGap()
  295.                                         .addComponent(lblNewLabel)
  296.                                         .addPreferredGap(ComponentPlacement.RELATED)
  297.                                         .addComponent(s_bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  298.                                         .addGap(24)
  299.                                         .addComponent(lblNewLabel_1)
  300.                                         .addPreferredGap(ComponentPlacement.RELATED)
  301.                                         .addComponent(s_authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  302.                                         .addGap(32)
  303.                                         .addComponent(lblNewLabel_2)
  304.                                         .addPreferredGap(ComponentPlacement.RELATED)
  305.                                         .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
  306.                                                 .addComponent(btnNewButton)
  307.                                                 .addComponent(s_bookTypejcb, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE))
  308.                                         .addContainerGap(31, Short.MAX_VALUE))
  309.                 );
  310.                 gl_panel.setVerticalGroup(
  311.                         gl_panel.createParallelGroup(Alignment.LEADING)
  312.                                 .addGroup(gl_panel.createSequentialGroup()
  313.                                         .addContainerGap()
  314.                                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  315.                                                 .addComponent(lblNewLabel)
  316.                                                 .addComponent(s_bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  317.                                                 .addComponent(lblNewLabel_1)
  318.                                                 .addComponent(s_authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  319.                                                 .addComponent(lblNewLabel_2)
  320.                                                 .addComponent(s_bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  321.                                         .addPreferredGap(ComponentPlacement.UNRELATED)
  322.                                         .addComponent(btnNewButton)
  323.                                         .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  324.                 );
  325.                 panel.setLayout(gl_panel);
  326.                
  327.                 booktable = new JTable();
  328.                 booktable.addMouseListener(new MouseAdapter() {
  329.                         @Override
  330.                         public void mousePressed(MouseEvent e) {
  331.                                 bookTableMousePressed(e);
  332.                         }
  333.                 });
  334.                 booktable.setShowGrid(false);
  335.                 scrollPane.setViewportView(booktable);
  336.                 booktable.setModel(new DefaultTableModel(
  337.                         new Object[][] {
  338.                         },
  339.                         new String[] {
  340.                                 "\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"
  341.                         }
  342.                 ) {
  343.                         boolean[] columnEditables = new boolean[] {
  344.                                 true, false, false, false, false, false, false
  345.                         };
  346.                         public boolean isCellEditable(int row, int column) {
  347.                                 return columnEditables[column];
  348.                         }
  349.                 });
  350.                 booktable.getColumnModel().getColumn(5).setPreferredWidth(173);
  351.                 getContentPane().setLayout(groupLayout);
  352.                
  353.                 this.fillBookType("search");
  354.                 this.fillBookType("modify");
  355.                 this.fillTable(new Book());
  356.                 bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
  357.                 bookDesctxt.setLineWrap(true);        //激活自动换行功能
  358.                 bookDesctxt.setWrapStyleWord(true);            // 激活断行不断字功能
  359.         }
  360.        
  361.         /**
  362.          * 图书删除事件处理
  363.          * @param e
  364.          */
  365.         protected void bookDeleteActionEvent(ActionEvent e) {
  366.                 // TODO 自动生成的方法存根
  367.                 String idString=idtxt.getText();
  368.                 if(Stringutil.isEmpty(idString)) {
  369.                         JOptionPane.showConfirmDialog(null, "请选择要删除的记录");
  370.                         return;
  371.                 }
  372.                 int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗");
  373.                 if(n==0) {
  374.                         Connection con=null;
  375.                         try {
  376.                                 con=dbUtil.getCon();
  377.                                 int deteleNum=bookDao.delete(con, idString);
  378.                                 if(deteleNum==1) {
  379.                                         JOptionPane.showConfirmDialog(null, "删除成功");
  380.                                         this.fillTable(new Book());
  381.                                         this.resetValue();
  382.                                 }
  383.                                 else {
  384.                                         JOptionPane.showConfirmDialog(null, "删除失败");
  385.                                 }
  386.                         } catch (Exception e2) {
  387.                                 // TODO: handle exception
  388.                                 e2.printStackTrace();
  389.                         }finally {
  390.                                 try {
  391.                                         dbUtil.closeCon(con);
  392.                                 } catch (Exception e1) {
  393.                                         // TODO 自动生成的 catch 块
  394.                                         e1.printStackTrace();
  395.                                 }
  396.                         }
  397.                 }
  398.         }
  399.         /**
  400.          * 图书修改事件处理
  401.          * @param e
  402.          */
  403.         protected void bookUpdataActionEvent(ActionEvent e) {
  404.                 // TODO 自动生成的方法存根
  405.                 String id=this.idtxt.getText();
  406.                 if(Stringutil.isEmpty(id)) {
  407.                         JOptionPane.showConfirmDialog(null, "请选择一条记录");
  408.                         return;
  409.                 }
  410.                 String bookName=this.bookNametxt.getText();
  411.                 String author=this.authortxt.getText();
  412.                 String price=this.pricetxt.getText();
  413.                 String bookDesc=this.bookDesctxt.getText();
  414.                
  415.                 if(Stringutil.isEmpty(bookName)) {
  416.                         JOptionPane.showConfirmDialog(null, "图书名称不能为空");
  417.                         return;
  418.                 }
  419.                 if(Stringutil.isEmpty(author)) {
  420.                         JOptionPane.showConfirmDialog(null, "作者不能为空");
  421.                         return;
  422.                 }
  423.                 if(Stringutil.isEmpty(price)) {
  424.                         JOptionPane.showConfirmDialog(null, "图书价格不能为空");
  425.                         return;
  426.                 }
  427.                
  428.                 String sex=null;
  429.                 if(manjrb.isSelected()) {
  430.                         sex="男";
  431.                 }
  432.                 else {
  433.                         sex="女";
  434.                 }
  435.                 BookType bookType=(BookType) bookTypejcb.getSelectedItem();
  436.                 int bookTypeId=bookType.getId();
  437.                
  438.                 Book book=new Book(Integer.parseInt(id), bookName, author, sex, Float.parseFloat(price), bookTypeId,
  439.                                 bookDesc);
  440.                
  441.                
  442.                 Connection con=null;
  443.                 try {
  444.                         con=dbUtil.getCon();
  445.                         int addNum=bookDao.updata(con,book);
  446.                         if(addNum==1) {
  447.                                 JOptionPane.showConfirmDialog(null, "图书修改成功");
  448.                                 this.resetValue();
  449.                                 this.fillTable(new Book());
  450.                                 return ;
  451.                         }
  452.                         else {
  453.                             JOptionPane.showConfirmDialog(null, "图书修改失败");
  454.                         }
  455.                 } catch (Exception e2) {
  456.                         // TODO: handle exception
  457.                         JOptionPane.showConfirmDialog(null, "图书修改失败");
  458.                         e2.printStackTrace();
  459.                 }finally {
  460.                         try {
  461.                                 dbUtil.closeCon(con);
  462.                         } catch (Exception e1) {
  463.                                 // TODO 自动生成的 catch 块
  464.                                 e1.printStackTrace();
  465.                         }
  466.                 }
  467.         }
  468.         private void resetValue() {
  469.                 // TODO 自动生成的方法存根
  470.                 this.idtxt.setText("");
  471.                 this.bookNametxt.setText("");
  472.                 this.pricetxt.setText("");
  473.                 this.authortxt.setText("");
  474.                 this.manjrb.setSelected(true);
  475.                 this.bookDesctxt.setText("");
  476.                 //意思就是说如果有下拉框选项,那么就默认选回第一个
  477.                 if(this.bookTypejcb.getItemCount()>0) {
  478.                         this.bookTypejcb.setSelectedIndex(0);
  479.                 }
  480.         }
  481.         /**
  482.          * 表格点击事件处理
  483.          * @param e
  484.          */
  485.         protected void bookTableMousePressed(MouseEvent e) {
  486.                 // TODO 自动生成的方法存根
  487.                 int row=this.booktable.getSelectedRow();
  488.                 this.idtxt.setText((String) booktable.getValueAt(row, 0));
  489.                 this.bookNametxt.setText((String) booktable.getValueAt(row, 1));
  490.                 this.authortxt.setText((String) booktable.getValueAt(row, 2));
  491.                 String sex=(String) booktable.getValueAt(row, 3);
  492.                 if(sex.equals("男")) {
  493.                         this.manjrb.setSelected(true);
  494.                 }
  495.                 else {
  496.                         this.womenjrb.setSelected(true);
  497.                 }
  498.                 this.pricetxt.setText((Float) booktable.getValueAt(row, 4)+"");
  499.                 this.bookDesctxt.setText((String) booktable.getValueAt(row, 5));
  500.                 String bookTypeName=(String) this.booktable.getValueAt(row, 6);
  501.                 int n=this.bookTypejcb.getItemCount();
  502.                 for(int i=0;i<n;i++) {
  503.                         BookType item=(BookType) this.bookTypejcb.getItemAt(i);
  504.                         if(item.getBookTypeName().equals(bookTypeName)) {
  505.                                 this.bookTypejcb.setSelectedIndex(i);
  506.                         }
  507.                 }
  508.         }
  509.         /**
  510.          * 图书查询事件
  511.          * @param e
  512.          */
  513.         protected void bookSearchActionPerformed(ActionEvent e) {
  514.                 // TODO 自动生成的方法存根
  515.                 String bookName=this.s_bookNametxt.getText();
  516.                 String authorString=this.s_authortxt.getText();
  517.                 BookType bookType=(BookType)this.s_bookTypejcb.getSelectedItem();
  518.                 int bookTypeId=bookType.getId();
  519.                
  520.                 Book book=new Book(bookName,authorString,bookTypeId);
  521.                 this.fillTable(book);
  522.         }
  523.         /**
  524.          * 初始化下拉框
  525.          * @param Type
  526.          */
  527.         private void fillBookType(String Type) {
  528.                 Connection con=null;
  529.                 BookType bookType=null;
  530.                 try {
  531.                         con=dbUtil.getCon();
  532.                         ResultSet rs=bookTypeDao.list(con, new BookType());
  533.                         //设置默认
  534.                         if("search".equals(Type)) {
  535.                                 bookType=new BookType();
  536.                                 bookType.setBookTypeName("请选择...");
  537.                                 bookType.setId(-1);
  538.                                 this.s_bookTypejcb.addItem(bookType);
  539.                         }
  540.                         while(rs.next()) {
  541.                                 bookType=new BookType();
  542.                                 bookType.setBookTypeName(rs.getString("bookTypeName"));
  543.                                 bookType.setId(rs.getInt("id"));
  544.                                 if("search".equals(Type)) {
  545.                                         this.s_bookTypejcb.addItem(bookType);
  546.                                 }
  547.                                 else if("modify".equals(Type)) {
  548.                                     this.bookTypejcb.addItem(bookType);
  549.                                 }
  550.                         }
  551.                 } catch (Exception e) {
  552.                         // TODO: handle exception
  553.                         e.printStackTrace();
  554.                 }finally {
  555.                         try {
  556.                                 dbUtil.closeCon(con);
  557.                         } catch (Exception e) {
  558.                                 // TODO 自动生成的 catch 块
  559.                                 e.printStackTrace();
  560.                         }
  561.                 }
  562.         }
  563.        
  564.        
  565.         /**
  566.          * 初始化表格数据
  567.          * @param book
  568.          */
  569.         private void fillTable(Book book) {
  570.                 DefaultTableModel dtm=(DefaultTableModel)booktable.getModel();
  571.                 dtm.setRowCount(0);//设置成0行
  572.                 Connection con=null;
  573.                 try {
  574.                         con = dbUtil.getCon();
  575.                        
  576.                 } catch (Exception e3) {
  577.                         // TODO 自动生成的 catch 块
  578.                         e3.printStackTrace();
  579.                 }
  580.                 try {
  581.                         ResultSet rs=bookDao.list(con, book);
  582.                         while(rs.next()) {
  583.                                 Vector v=new Vector();
  584.                                 v.add(rs.getString("id"));
  585.                                 v.add(rs.getString("bookName"));
  586.                                 v.add(rs.getString("author"));
  587.                                 v.add(rs.getString("sex"));
  588.                                 v.add(rs.getFloat("price"));
  589.                                 v.add(rs.getString("bookDesc"));
  590.                                 v.add(rs.getString("bookTypeName"));
  591.                                 dtm.addRow(v);
  592.                         }
  593.                 } catch (Exception e) {
  594.                         // TODO: handle exception
  595.                         e.printStackTrace();
  596.                 }finally {
  597.                         try {
  598.                                 dbUtil.closeCon(con);
  599.                         } catch (Exception e) {
  600.                                 // TODO 自动生成的 catch 块
  601.                                 e.printStackTrace();
  602.                         }
  603.                 }
  604.         }
  605. }
复制代码
11.BookTypeAddInterFrm.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JInternalFrame;
  4. import javax.swing.GroupLayout;
  5. import javax.swing.GroupLayout.Alignment;
  6. import javax.swing.JLabel;
  7. import javax.swing.JOptionPane;
  8. import javax.swing.JTextField;
  9. import javax.swing.JTextArea;
  10. import javax.swing.JButton;
  11. import javax.swing.LayoutStyle.ComponentPlacement;
  12. import javax.swing.border.LineBorder;
  13. import com.java1234.dao.BookTypeDao;
  14. import com.java1234.model.BookType;
  15. import com.java1234.util.DbUtil;
  16. import com.java1234.util.Stringutil;
  17. import java.awt.Font;
  18. import javax.swing.ImageIcon;
  19. import java.awt.event.ActionListener;
  20. import java.sql.Connection;
  21. import java.awt.event.ActionEvent;
  22. public class BookTypeAddInterFrm extends JInternalFrame {
  23.         private static final long serialVersionUID = 1L;
  24.         private JTextField bookTypeNametxt;
  25.         private JTextArea bookTypeDesctxt;
  26.         private DbUtil dbUtil = new DbUtil();
  27.         private BookTypeDao bookTypeDao=new BookTypeDao();
  28.         /**
  29.          * Launch the application.
  30.          */
  31.         public static void main(String[] args) {
  32.                 EventQueue.invokeLater(new Runnable() {
  33.                         public void run() {
  34.                                 try {
  35.                                         BookTypeAddInterFrm frame = new BookTypeAddInterFrm();
  36.                                         frame.setVisible(true);
  37.                                 } catch (Exception e) {
  38.                                         e.printStackTrace();
  39.                                 }
  40.                         }
  41.                 });
  42.         }
  43.         /**
  44.          * Create the frame.
  45.          */
  46.         public BookTypeAddInterFrm() {
  47.                 setClosable(true);
  48.                 setTitle("图书类别添加");
  49.                 setBounds(100, 100, 537, 300);
  50.                
  51.                 JLabel lblNewLabel = new JLabel("图书类别名称:");
  52.                 lblNewLabel.setBounds(78, 64, 111, 18);
  53.                 lblNewLabel.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/导入.png")));
  54.                 lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  55.                
  56.                 JLabel lblNewLabel_1 = new JLabel("图书类别描述:");
  57.                 lblNewLabel_1.setBounds(78, 137, 111, 18);
  58.                 lblNewLabel_1.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/project.png")));
  59.                 lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  60.                
  61.                 bookTypeNametxt = new JTextField();
  62.                 bookTypeNametxt.setBounds(199, 61, 216, 24);
  63.                 bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  64.                 bookTypeNametxt.setColumns(10);
  65.                
  66.                 bookTypeDesctxt = new JTextArea();
  67.                 bookTypeDesctxt.setBounds(199, 135, 216, 79);
  68.                 bookTypeDesctxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  69.                
  70.                 JButton btnNewButton = new JButton("添加");
  71.                 btnNewButton.addActionListener(new ActionListener() {
  72.                         public void actionPerformed(ActionEvent e) {
  73.                                 bookTypeAddActionPerformed(e);
  74.                         }
  75.                 });
  76.                 btnNewButton.setBounds(124, 233, 79, 27);
  77.                 btnNewButton.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/添加.png")));
  78.                 btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  79.                
  80.                 JButton btnNewButton_1 = new JButton("重置");
  81.                 btnNewButton_1.addActionListener(new ActionListener() {
  82.                         public void actionPerformed(ActionEvent e) {
  83.                                 resetValueActionPerformed(e);
  84.                         }
  85.                 });
  86.                 btnNewButton_1.setBounds(253, 233, 79, 27);
  87.                 btnNewButton_1.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/重置.png")));
  88.                 btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  89.                 getContentPane().setLayout(null);
  90.                 getContentPane().add(lblNewLabel);
  91.                 getContentPane().add(bookTypeNametxt);
  92.                 getContentPane().add(lblNewLabel_1);
  93.                 getContentPane().add(btnNewButton);
  94.                 getContentPane().add(btnNewButton_1);
  95.                 getContentPane().add(bookTypeDesctxt);
  96.                
  97.                
  98.                 //设置文本域边框
  99.                 bookTypeDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
  100.                 bookTypeDesctxt.setLineWrap(true);        //激活自动换行功能
  101.                 bookTypeDesctxt.setWrapStyleWord(true);            // 激活断行不断字功能
  102.         }
  103.        
  104.         /**
  105.          * 图书类别添加事件
  106.          * @param e
  107.          */
  108.         protected void bookTypeAddActionPerformed(ActionEvent e) {
  109.                 // TODO 自动生成的方法存根
  110.                 String bookTypeName=this.bookTypeNametxt.getText();
  111.                 String bookTypeDesc=this.bookTypeDesctxt.getText();
  112.                 if(Stringutil.isEmpty(bookTypeName)) {
  113.                         JOptionPane.showConfirmDialog(null, "图书类别名称不能为空");
  114.                         return;
  115.                 }
  116.                 BookType bookType=new BookType(bookTypeName,bookTypeDesc);
  117.                 Connection con = null;
  118.                 try {
  119.                         con = dbUtil.getCon();
  120.                 } catch (Exception e3) {
  121.                         // TODO 自动生成的 catch 块
  122.                         e3.printStackTrace();
  123.                 }
  124.                 try {
  125.                         int n=bookTypeDao.add(con, bookType);
  126.                         if(n==1) {
  127.                                 JOptionPane.showConfirmDialog(null, "图书类别添加成功");
  128.                                 resetValue();
  129.                                 return;
  130.                         }
  131.                         else {
  132.                                 JOptionPane.showConfirmDialog(null, "添加失败");
  133.                         }
  134.                 } catch (Exception e2) {
  135.                         // TODO: handle exception
  136.                         e2.printStackTrace();
  137.                         JOptionPane.showConfirmDialog(null, "添加失败");
  138.                 }finally {
  139.                         try {
  140.                                 dbUtil.closeCon(con);
  141.                         } catch (Exception e1) {
  142.                                 // TODO 自动生成的 catch 块
  143.                                 e1.printStackTrace();
  144.                         }
  145.                 }
  146.         }
  147.         //重置事件处理
  148.         protected void resetValueActionPerformed(ActionEvent e) {
  149.                 // TODO 自动生成的方法存根
  150.                 this.resetValue();
  151.         }
  152.         /**
  153.          * 重置表单
  154.          */
  155.         private void resetValue() {
  156.                 this.bookTypeNametxt.setText("");
  157.                 this.bookTypeDesctxt.setText("");
  158.         }
  159. }
复制代码
12.BookTypeManagerInterFrm.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.util.Vector;
  6. import javax.swing.JInternalFrame;
  7. import javax.swing.JScrollPane;
  8. import javax.swing.GroupLayout;
  9. import javax.swing.GroupLayout.Alignment;
  10. import javax.swing.JTable;
  11. import javax.swing.table.DefaultTableModel;
  12. import com.java1234.dao.BookDao;
  13. import com.java1234.dao.BookTypeDao;
  14. import com.java1234.model.BookType;
  15. import com.java1234.util.DbUtil;
  16. import com.java1234.util.Stringutil;
  17. import javax.swing.border.LineBorder;
  18. import java.awt.Color;
  19. import javax.swing.JLabel;
  20. import javax.swing.JOptionPane;
  21. import java.awt.Font;
  22. import javax.swing.JTextField;
  23. import javax.swing.LayoutStyle.ComponentPlacement;
  24. import javax.swing.JButton;
  25. import javax.swing.ImageIcon;
  26. import java.awt.event.ActionListener;
  27. import java.awt.event.ActionEvent;
  28. import javax.swing.JPanel;
  29. import javax.swing.border.TitledBorder;
  30. import javax.swing.JTextArea;
  31. import java.awt.event.MouseAdapter;
  32. import java.awt.event.MouseEvent;
  33. public class BookTypeManagerInterFrm extends JInternalFrame {
  34.         private static final long serialVersionUID = 1L;
  35.         private JTable bookTypeTable;
  36.         private DbUtil dbUtil = new DbUtil();
  37.         private BookTypeDao bookTypeDao=new BookTypeDao();
  38.         private BookDao bookDao=new BookDao();
  39.         private JTextField s_bookTypeNametxt;
  40.         private JTextField idtxt;
  41.         private JTextField bookTypeNametxt;
  42.         private JTextArea bookTypeDesctxt;
  43.         /**
  44.          * Launch the application.
  45.          */
  46.         public static void main(String[] args) {
  47.                 EventQueue.invokeLater(new Runnable() {
  48.                         public void run() {
  49.                                 try {
  50.                                         BookTypeManagerInterFrm frame = new BookTypeManagerInterFrm();
  51.                                         frame.setVisible(true);
  52.                                 } catch (Exception e) {
  53.                                         e.printStackTrace();
  54.                                 }
  55.                         }
  56.                 });
  57.         }
  58.         /**
  59.          * Create the frame.
  60.          */
  61.         public BookTypeManagerInterFrm() {
  62.                 setClosable(true);
  63.                 setIconifiable(true);
  64.                 setTitle("图书类别管理");
  65.                 setBounds(100, 100, 678, 622);
  66.                
  67.                 JScrollPane scrollPane = new JScrollPane();
  68.                
  69.                 JLabel lblNewLabel = new JLabel("图书类别名称:");
  70.                 lblNewLabel.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/project.png")));
  71.                 lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  72.                
  73.                 s_bookTypeNametxt = new JTextField();
  74.                 s_bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  75.                 s_bookTypeNametxt.setColumns(10);
  76.                
  77.                 JButton btnNewButton = new JButton("查询");
  78.                 btnNewButton.addActionListener(new ActionListener() {
  79.                         public void actionPerformed(ActionEvent e) {
  80.                                 bookTypeSearchActionPerformed(e);
  81.                         }
  82.                 });
  83.                 btnNewButton.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/查询.png")));
  84.                 btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  85.                
  86.                 JPanel panel = new JPanel();
  87.                 panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
  88.                 GroupLayout groupLayout = new GroupLayout(getContentPane());
  89.                 groupLayout.setHorizontalGroup(
  90.                         groupLayout.createParallelGroup(Alignment.LEADING)
  91.                                 .addGroup(groupLayout.createSequentialGroup()
  92.                                         .addGap(48)
  93.                                         .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
  94.                                                 .addGroup(groupLayout.createSequentialGroup()
  95.                                                         .addComponent(lblNewLabel)
  96.                                                         .addPreferredGap(ComponentPlacement.UNRELATED)
  97.                                                         .addComponent(s_bookTypeNametxt, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
  98.                                                         .addGap(45)
  99.                                                         .addComponent(btnNewButton))
  100.                                                 .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
  101.                                                 .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  102.                                         .addContainerGap(57, Short.MAX_VALUE))
  103.                 );
  104.                 groupLayout.setVerticalGroup(
  105.                         groupLayout.createParallelGroup(Alignment.LEADING)
  106.                                 .addGroup(groupLayout.createSequentialGroup()
  107.                                         .addGap(42)
  108.                                         .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
  109.                                                 .addComponent(lblNewLabel)
  110.                                                 .addComponent(s_bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  111.                                                 .addComponent(btnNewButton))
  112.                                         .addGap(27)
  113.                                         .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
  114.                                         .addGap(38)
  115.                                         .addComponent(panel, GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
  116.                                         .addContainerGap())
  117.                 );
  118.                
  119.                 JLabel lblNewLabel_1 = new JLabel("编号:");
  120.                 lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  121.                
  122.                 idtxt = new JTextField();
  123.                 idtxt.setEditable(false);
  124.                 idtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  125.                 idtxt.setColumns(10);
  126.                
  127.                 JLabel lblNewLabel_2 = new JLabel("图书类别名称:");
  128.                 lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  129.                
  130.                 bookTypeNametxt = new JTextField();
  131.                 bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  132.                 bookTypeNametxt.setColumns(10);
  133.                
  134.                 JLabel lblNewLabel_3 = new JLabel("描述:");
  135.                 lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  136.                
  137.                 bookTypeDesctxt = new JTextArea();
  138.                
  139.                 JButton btnNewButton_1 = new JButton("修改");
  140.                 btnNewButton_1.addActionListener(new ActionListener() {
  141.                         public void actionPerformed(ActionEvent e) {
  142.                                 bookTypeUpdataActionEvent(e);
  143.                         }
  144.                 });
  145.                 btnNewButton_1.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/修改.png")));
  146.                 btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  147.                
  148.                 JButton btnNewButton_2 = new JButton("删除");
  149.                 btnNewButton_2.addActionListener(new ActionListener() {
  150.                         public void actionPerformed(ActionEvent e) {
  151.                                 bookTypeDeleteActionEvent(e);
  152.                         }
  153.                 });
  154.                 btnNewButton_2.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/删除.png")));
  155.                 btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  156.                 GroupLayout gl_panel = new GroupLayout(panel);
  157.                 gl_panel.setHorizontalGroup(
  158.                         gl_panel.createParallelGroup(Alignment.LEADING)
  159.                                 .addGroup(gl_panel.createSequentialGroup()
  160.                                         .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
  161.                                                 .addGroup(gl_panel.createSequentialGroup()
  162.                                                         .addComponent(lblNewLabel_1)
  163.                                                         .addGap(18)
  164.                                                         .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  165.                                                         .addGap(44)
  166.                                                         .addComponent(lblNewLabel_2)
  167.                                                         .addPreferredGap(ComponentPlacement.UNRELATED)
  168.                                                         .addComponent(bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  169.                                                 .addGroup(gl_panel.createSequentialGroup()
  170.                                                         .addComponent(lblNewLabel_3)
  171.                                                         .addGap(18)
  172.                                                         .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
  173.                                                                 .addGroup(gl_panel.createSequentialGroup()
  174.                                                                         .addComponent(btnNewButton_1)
  175.                                                                         .addGap(105)
  176.                                                                         .addComponent(btnNewButton_2))
  177.                                                                 .addComponent(bookTypeDesctxt, GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE))))
  178.                                         .addContainerGap(81, Short.MAX_VALUE))
  179.                 );
  180.                 gl_panel.setVerticalGroup(
  181.                         gl_panel.createParallelGroup(Alignment.LEADING)
  182.                                 .addGroup(gl_panel.createSequentialGroup()
  183.                                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  184.                                                 .addComponent(lblNewLabel_1)
  185.                                                 .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  186.                                                 .addComponent(lblNewLabel_2)
  187.                                                 .addComponent(bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  188.                                         .addGap(50)
  189.                                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  190.                                                 .addComponent(lblNewLabel_3)
  191.                                                 .addComponent(bookTypeDesctxt, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE))
  192.                                         .addGap(29)
  193.                                         .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
  194.                                                 .addComponent(btnNewButton_1)
  195.                                                 .addComponent(btnNewButton_2))
  196.                                         .addContainerGap(18, Short.MAX_VALUE))
  197.                 );
  198.                 panel.setLayout(gl_panel);
  199.                
  200.                 bookTypeTable = new JTable();
  201.                 bookTypeTable.addMouseListener(new MouseAdapter() {
  202.                         @Override
  203.                         public void mousePressed(MouseEvent e) {
  204.                                 bookTypeMousePressed(e);
  205.                         }
  206.                 });
  207.                 bookTypeTable.setShowGrid(false);
  208.                 bookTypeTable.setModel(new DefaultTableModel(
  209.                         new Object[][] {
  210.                         },
  211.                         new String[] {
  212.                                 "\u7F16\u53F7", "\u56FE\u4E66\u7C7B\u522B\u540D\u79F0", "\u56FE\u4E66\u7C7B\u522B\u5185\u5BB9"
  213.                         }
  214.                 ) {
  215.                         boolean[] columnEditables = new boolean[] {
  216.                                 false, false, false
  217.                         };
  218.                         public boolean isCellEditable(int row, int column) {
  219.                                 return columnEditables[column];
  220.                         }
  221.                 });
  222.                 bookTypeTable.getColumnModel().getColumn(0).setPreferredWidth(49);
  223.                 bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(103);
  224.                 bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(142);
  225.                 scrollPane.setViewportView(bookTypeTable);
  226.                 getContentPane().setLayout(groupLayout);
  227.                 //初始化表格
  228.                 this.fillTable(new BookType());
  229.                 bookTypeDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
  230.                 bookTypeDesctxt.setLineWrap(true);        //激活自动换行功能
  231.                 bookTypeDesctxt.setWrapStyleWord(true);            // 激活断行不断字功能
  232.         }
  233.        
  234.         /**
  235.          * 删除事件处理
  236.          * @param e
  237.          */
  238.         protected void bookTypeDeleteActionEvent(ActionEvent e) {
  239.                 // TODO 自动生成的方法存根
  240.                 String idString=idtxt.getText();
  241.                 if(Stringutil.isEmpty(idString)) {
  242.                         JOptionPane.showConfirmDialog(null, "请选择要删除的记录");
  243.                         return;
  244.                 }
  245.                 int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗");
  246.                 if(n==0) {
  247.                         Connection con=null;
  248.                         try {
  249.                                 con=dbUtil.getCon();
  250.                                 boolean fl=bookDao.existBook(con, idString);
  251.                                 if(fl==true) {
  252.                                         JOptionPane.showConfirmDialog(null, "当前图书类型下有图书无法删除");
  253.                                         return ;
  254.                                 }
  255.                                 int deteleNum=bookTypeDao.delete(con, idString);
  256.                                 if(deteleNum==1) {
  257.                                         JOptionPane.showConfirmDialog(null, "删除成功");
  258.                                         this.fillTable(new BookType());
  259.                                         this.resetValue();
  260.                                 }
  261.                                 else {
  262.                                         JOptionPane.showConfirmDialog(null, "删除失败");
  263.                                 }
  264.                         } catch (Exception e2) {
  265.                                 // TODO: handle exception
  266.                                 JOptionPane.showConfirmDialog(null, "删除失败");
  267.                                 e2.printStackTrace();
  268.                         }finally {
  269.                                 try {
  270.                                         dbUtil.closeCon(con);
  271.                                 } catch (Exception e1) {
  272.                                         // TODO 自动生成的 catch 块
  273.                                         e1.printStackTrace();
  274.                                 }
  275.                         }
  276.                 }
  277.         }
  278.        
  279.         /**
  280.          * 图书类别修改
  281.          * @param e
  282.          */
  283.         protected void bookTypeUpdataActionEvent(ActionEvent e) {
  284.                 // TODO 自动生成的方法存根
  285.                 String id=idtxt.getText();
  286.                 String bookTypeName=bookTypeNametxt.getText();
  287.                 String bookTypeDesc=bookTypeDesctxt.getText();
  288.                 if(Stringutil.isEmpty(id)) {
  289.                         JOptionPane.showConfirmDialog(null, "请选择要修改的记录");
  290.                         return;
  291.                 }
  292.                 else if(Stringutil.isEmpty(bookTypeName)) {
  293.                         JOptionPane.showConfirmDialog(null, "类别不能为空");
  294.                         return;
  295.                 }
  296.                 else {
  297.                         BookType bookType=new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);
  298.                         Connection con=null;
  299.                         try {
  300.                                 con=dbUtil.getCon();
  301.                                 int modifNum=bookTypeDao.updata(con, bookType);
  302.                                 if(modifNum==1) {
  303.                                         JOptionPane.showConfirmDialog(null, "修改成功");
  304.                                         this.resetValue();
  305.                                         //这里是为了直接刷新结果
  306.                                         this.fillTable(new BookType());
  307.                                 }
  308.                                 else {
  309.                                         JOptionPane.showConfirmDialog(null, "修改失败");
  310.                                 }
  311.                         } catch (Exception e2) {
  312.                                 // TODO: handle exception
  313.                                 e2.printStackTrace();
  314.                         }finally {
  315.                                 try {
  316.                                         dbUtil.closeCon(con);
  317.                                 } catch (Exception e3) {
  318.                                         // TODO: handle exception
  319.                                         e3.printStackTrace();
  320.                                 }
  321.                         }
  322.                 }
  323.         }
  324.         /**
  325.          * 表格行点击事件处理
  326.          * @param e
  327.          */
  328.         protected void bookTypeMousePressed(MouseEvent e) {
  329.                 // TODO 自动生成的方法存根
  330.                 int row=bookTypeTable.getSelectedRow();
  331.                 idtxt.setText((String)bookTypeTable.getValueAt(row, 0));
  332.                 bookTypeNametxt.setText((String)bookTypeTable.getValueAt(row, 1));
  333.                 bookTypeDesctxt.setText((String)bookTypeTable.getValueAt(row, 2));
  334.         }
  335.         //图书类别查询事件
  336.         protected void bookTypeSearchActionPerformed(ActionEvent evt) {
  337.                 // TODO 自动生成的方法存根
  338.                 String s_bookTypeName=this.s_bookTypeNametxt.getText();
  339.                 BookType bookType=new BookType();
  340.                 bookType.setBookTypeName(s_bookTypeName);
  341.                 this.fillTable(bookType);
  342.         }
  343.         private void fillTable(BookType bookType) {
  344.                 DefaultTableModel dtm=(DefaultTableModel)bookTypeTable.getModel();
  345.                 dtm.setRowCount(0);//设置成0行
  346.                 Connection con=null;
  347.                 try {
  348.                         con = dbUtil.getCon();
  349.                        
  350.                 } catch (Exception e3) {
  351.                         // TODO 自动生成的 catch 块
  352.                         e3.printStackTrace();
  353.                 }
  354.                 try {
  355.                         ResultSet rs=bookTypeDao.list(con, bookType);
  356.                         while(rs.next()) {
  357.                                 Vector v=new Vector();
  358.                                 v.add(rs.getString("id"));
  359.                                 v.add(rs.getString("bookTypeName"));
  360.                                 v.add(rs.getString("bookTpeDesc"));
  361.                                 dtm.addRow(v);
  362.                         }
  363.                 } catch (Exception e) {
  364.                         // TODO: handle exception
  365.                         e.printStackTrace();
  366.                 }finally {
  367.                         try {
  368.                                 dbUtil.closeCon(con);
  369.                         } catch (Exception e) {
  370.                                 // TODO 自动生成的 catch 块
  371.                                 e.printStackTrace();
  372.                         }
  373.                 }
  374.         }
  375.        
  376.        
  377.         /**
  378.          * 重置表单
  379.          */
  380.         private  void  resetValue() {
  381.                 this.idtxt.setText("");
  382.                 this.bookTypeDesctxt.setText("");
  383.                 this.bookTypeNametxt.setText("");
  384.         }
  385. }
复制代码
13.Java666interframe.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JInternalFrame;
  4. import java.awt.BorderLayout;
  5. import java.awt.Color;
  6. import javax.swing.GroupLayout;
  7. import javax.swing.GroupLayout.Alignment;
  8. import javax.swing.JLabel;
  9. import javax.swing.ImageIcon;
  10. public class Java666interframe extends JInternalFrame {
  11.         private static final long serialVersionUID = 1L;
  12.         /**
  13.          * Launch the application.
  14.          */
  15.         public static void main(String[] args) {
  16.                 EventQueue.invokeLater(new Runnable() {
  17.                         public void run() {
  18.                                 try {
  19.                                         Java666interframe frame = new Java666interframe();
  20.                                         frame.setVisible(true);
  21.                                 } catch (Exception e) {
  22.                                         e.printStackTrace();
  23.                                 }
  24.                         }
  25.                 });
  26.         }
  27.         /**
  28.          * Create the frame.
  29.          */
  30.         public Java666interframe() {
  31.                 getContentPane().setBackground(new Color(255, 255, 255));
  32.                
  33.                 JLabel lblNewLabel = new JLabel("");
  34.                 lblNewLabel.setIcon(new ImageIcon(Java666interframe.class.getResource("/images/原神  启动!!!!.png")));
  35.                 GroupLayout groupLayout = new GroupLayout(getContentPane());
  36.                 groupLayout.setHorizontalGroup(
  37.                         groupLayout.createParallelGroup(Alignment.LEADING)
  38.                                 .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
  39.                                         .addContainerGap(95, Short.MAX_VALUE)
  40.                                         .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 265, GroupLayout.PREFERRED_SIZE)
  41.                                         .addGap(74))
  42.                 );
  43.                 groupLayout.setVerticalGroup(
  44.                         groupLayout.createParallelGroup(Alignment.LEADING)
  45.                                 .addGroup(groupLayout.createSequentialGroup()
  46.                                         .addGap(75)
  47.                                         .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
  48.                                         .addContainerGap(80, Short.MAX_VALUE))
  49.                 );
  50.                 getContentPane().setLayout(groupLayout);
  51.                 setIconifiable(true);
  52.                 setClosable(true);
  53.                 setTitle("关于我们");
  54.                 setBounds(100, 100, 450, 300);
  55.         }
  56. }
复制代码
14.Login.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import com.java1234.dao.UserDao;
  7. import com.java1234.model.User;
  8. import com.java1234.util.DbUtil;
  9. import com.java1234.util.Stringutil;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.GroupLayout;
  13. import javax.swing.GroupLayout.Alignment;
  14. import javax.swing.ImageIcon;
  15. import java.awt.Font;
  16. import javax.swing.LayoutStyle.ComponentPlacement;
  17. import javax.swing.UIManager;
  18. import javax.swing.UnsupportedLookAndFeelException;
  19. import javax.swing.JTextField;
  20. import javax.swing.JButton;
  21. import java.awt.event.ActionListener;
  22. import java.sql.Connection;
  23. import java.awt.event.ActionEvent;
  24. import javax.swing.JComboBox;
  25. public class Login extends JFrame {
  26.         private static final long serialVersionUID = 1L;
  27.         private JPanel contentPane;
  28.         private JTextField userNameTxt;
  29.         private JTextField passwordtxt;
  30.         private DbUtil dbUtil=new DbUtil();
  31.         private UserDao userDao=new UserDao();
  32.         /**
  33.          * Launch the application.
  34.          */
  35.         public static void main(String[] args) {
  36.                 String lookAndFeel ="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  37.                 try {
  38.                         UIManager.setLookAndFeel(lookAndFeel);
  39.                 } catch (ClassNotFoundException e1) {
  40.                         // TODO 自动生成的 catch 块
  41.                         e1.printStackTrace();
  42.                 } catch (InstantiationException e1) {
  43.                         // TODO 自动生成的 catch 块
  44.                         e1.printStackTrace();
  45.                 } catch (IllegalAccessException e1) {
  46.                         // TODO 自动生成的 catch 块
  47.                         e1.printStackTrace();
  48.                 } catch (UnsupportedLookAndFeelException e1) {
  49.                         // TODO 自动生成的 catch 块
  50.                         e1.printStackTrace();
  51.                 }
  52.                 EventQueue.invokeLater(new Runnable() {
  53.                         public void run() {
  54.                                 try {
  55.                                         Login frame = new Login();
  56.                                         frame.setVisible(true);
  57.                                 } catch (Exception e) {
  58.                                         e.printStackTrace();
  59.                                 }
  60.                         }
  61.                 });
  62.         }
  63.         /**
  64.          * Create the frame.
  65.          */
  66.         public Login() {
  67.                 setTitle("管理员登录");
  68.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  69.                 setBounds(100, 100, 601, 355);
  70.                 contentPane = new JPanel();
  71.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  72.                 setContentPane(contentPane);
  73.                
  74.                 JLabel lblNewLabel = new JLabel("图书管理系统");
  75.                 lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 19));
  76.                 lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/images/图书 (1).png")));
  77.                
  78.                 JLabel lblNewLabel_1 = new JLabel("用户名:");
  79.                 lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  80.                 lblNewLabel_1.setIcon(new ImageIcon(Login.class.getResource("/images/用户名-登录页.png")));
  81.                
  82.                 JLabel lblNewLabel_2 = new JLabel("密  码:");
  83.                 lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  84.                 lblNewLabel_2.setIcon(new ImageIcon(Login.class.getResource("/images/密码.png")));
  85.                
  86.                 userNameTxt = new JTextField();
  87.                 userNameTxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  88.                 userNameTxt.setColumns(10);
  89.                
  90.                 passwordtxt = new JTextField();
  91.                 passwordtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  92.                 passwordtxt.setColumns(10);
  93.                
  94.                 JButton btnNewButton = new JButton("登录");
  95.                 btnNewButton.addActionListener(new ActionListener() {
  96.                         public void actionPerformed(ActionEvent e) {
  97.                                 loginActionPerformed(e);
  98.                         }
  99.                 });
  100.                 btnNewButton.setFocusable(false);
  101.                 btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  102.                 btnNewButton.setIcon(new ImageIcon(Login.class.getResource("/images/登录.png")));
  103.                
  104.                 JButton btnNewButton_1 = new JButton("重置");
  105.                 btnNewButton_1.addActionListener(new ActionListener() {
  106.                         public void actionPerformed(ActionEvent e) {
  107.                                 resetValueActionPerformed(e);
  108.                         }
  109.                 });
  110.                 btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  111.                 btnNewButton_1.setFocusable(false);
  112.                 btnNewButton_1.setIcon(new ImageIcon(Login.class.getResource("/images/重置.png")));
  113.                 GroupLayout gl_contentPane = new GroupLayout(contentPane);
  114.                 gl_contentPane.setHorizontalGroup(
  115.                         gl_contentPane.createParallelGroup(Alignment.LEADING)
  116.                                 .addGroup(gl_contentPane.createSequentialGroup()
  117.                                         .addGap(96)
  118.                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  119.                                                 .addGroup(gl_contentPane.createSequentialGroup()
  120.                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  121.                                                                 .addComponent(lblNewLabel_2)
  122.                                                                 .addComponent(lblNewLabel_1))
  123.                                                         .addGap(38)
  124.                                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
  125.                                                                 .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  126.                                                                 .addComponent(userNameTxt)
  127.                                                                 .addComponent(passwordtxt)))
  128.                                                 .addGroup(gl_contentPane.createSequentialGroup()
  129.                                                         .addGap(47)
  130.                                                         .addComponent(btnNewButton)
  131.                                                         .addGap(135)
  132.                                                         .addComponent(btnNewButton_1)))
  133.                                         .addContainerGap(164, Short.MAX_VALUE))
  134.                 );
  135.                 gl_contentPane.setVerticalGroup(
  136.                         gl_contentPane.createParallelGroup(Alignment.LEADING)
  137.                                 .addGroup(gl_contentPane.createSequentialGroup()
  138.                                         .addGap(25)
  139.                                         .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
  140.                                         .addPreferredGap(ComponentPlacement.RELATED)
  141.                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
  142.                                                 .addComponent(lblNewLabel_1)
  143.                                                 .addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  144.                                         .addGap(49)
  145.                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
  146.                                                 .addComponent(lblNewLabel_2)
  147.                                                 .addComponent(passwordtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  148.                                         .addGap(40)
  149.                                         .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
  150.                                                 .addComponent(btnNewButton)
  151.                                                 .addComponent(btnNewButton_1))
  152.                                         .addContainerGap(49, Short.MAX_VALUE))
  153.                 );
  154.                 contentPane.setLayout(gl_contentPane);
  155.                
  156.                
  157.                 //窗口居中
  158.                 this.setLocationRelativeTo(null);
  159.         }
  160.        
  161. /**
  162. * 登录事件处理       
  163. * @param e
  164. */
  165.    protected void loginActionPerformed(ActionEvent e) {
  166.                 // TODO 自动生成的方法存根
  167.                 String userName=this.userNameTxt.getText();
  168.                 String password=this.passwordtxt.getText();
  169.                 if(Stringutil.isEmpty(userName)) {
  170.                         JOptionPane.showConfirmDialog(null, "用户名不能为空");
  171.                         return ;
  172.                 }
  173.                 if(Stringutil.isEmpty(password)) {
  174.                         JOptionPane.showConfirmDialog(null, "密码不能为空");
  175.                         return ;
  176.                 }
  177.                 User user=new User(userName,password);
  178.                 Connection con=null;
  179.                 try {
  180.                         con=dbUtil.getCon();
  181.                         User currentUser=userDao.login(con, user);
  182.                         if(currentUser!=null) {
  183.                                 dispose();
  184.                                 new Mainframe().setVisible(true);
  185.                         }
  186.                         else {
  187.                                 JOptionPane.showConfirmDialog(null, "用户名或者密码错误");
  188.                         }
  189.                 } catch (Exception e1) {
  190.                         // TODO 自动生成的 catch 块
  191.                         e1.printStackTrace();
  192.                 }finally{
  193.                         try {
  194.                                 dbUtil.closeCon(con);
  195.                         } catch (Exception e1) {
  196.                                 // TODO 自动生成的 catch 块
  197.                                 e1.printStackTrace();
  198.                         }
  199.                 }
  200.                
  201.         }
  202. /**
  203. * 重置事件
  204. * @param e
  205. */
  206.         protected void resetValueActionPerformed(ActionEvent e) {
  207.                 // TODO 自动生成的方法存根
  208.                 this.userNameTxt.setText("");
  209.                 this.passwordtxt.setText("");
  210.         }
  211. }
复制代码
15.Mainframe.java

  1. package com.java1234.view;
  2. import java.awt.EventQueue;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.border.EmptyBorder;
  6. import com.mysql.cj.xdevapi.Table;
  7. import javax.swing.GroupLayout;
  8. import javax.swing.GroupLayout.Alignment;
  9. import javax.swing.JMenuBar;
  10. import javax.swing.JMenu;
  11. import javax.swing.JMenuItem;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JDesktopPane;
  15. import java.awt.BorderLayout;
  16. import javax.swing.JLayeredPane;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ActionEvent;
  19. import java.awt.Color;
  20. public class Mainframe extends JFrame {
  21.         private static final long serialVersionUID = 1L;
  22.         private JPanel contentPane;
  23.         private JDesktopPane table = null;
  24.         /**
  25.          * Launch the application.
  26.          */
  27.         public static void main(String[] args) {
  28.                 EventQueue.invokeLater(new Runnable() {
  29.                         public void run() {
  30.                                 try {
  31.                                         Mainframe frame = new Mainframe();
  32.                                         frame.setVisible(true);
  33.                                 } catch (Exception e) {
  34.                                         e.printStackTrace();
  35.                                 }
  36.                         }
  37.                 });
  38.         }
  39.         /**
  40.          * Create the frame.
  41.          */
  42.         public Mainframe() {
  43.                 setTitle("图书管理系统");
  44.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  45.                 setBounds(100, 100, 450, 300);
  46.                
  47.                 JMenuBar menuBar = new JMenuBar();
  48.                 setJMenuBar(menuBar);
  49.                
  50.                 JMenu mnNewMenu = new JMenu("基本数据维护");
  51.                 mnNewMenu.setIcon(new ImageIcon(Mainframe.class.getResource("/images/数据维护-基础数据.png")));
  52.                 menuBar.add(mnNewMenu);
  53.                
  54.                 JMenu mnNewMenu_2 = new JMenu("图书类别管理");
  55.                 mnNewMenu_2.setIcon(new ImageIcon(Mainframe.class.getResource("/images/图书类别管理.png")));
  56.                 mnNewMenu.add(mnNewMenu_2);
  57.                
  58.                 JMenuItem mntmNewMenuItem_1 = new JMenuItem("图书类别添加");
  59.                 mntmNewMenuItem_1.addActionListener(new ActionListener() {
  60.                         public void actionPerformed(ActionEvent e) {
  61.                                 BookTypeAddInterFrm bookTypeAddInterFrm=new BookTypeAddInterFrm();
  62.                                 bookTypeAddInterFrm.setVisible(true);
  63.                                 table.add(bookTypeAddInterFrm);
  64.                         }
  65.                 });
  66.                 mntmNewMenuItem_1.setIcon(new ImageIcon(Mainframe.class.getResource("/images/添加.png")));
  67.                 mnNewMenu_2.add(mntmNewMenuItem_1);
  68.                
  69.                 JMenuItem mntmNewMenuItem_5 = new JMenuItem("图书类别维护");
  70.                 mntmNewMenuItem_5.addActionListener(new ActionListener() {
  71.                         public void actionPerformed(ActionEvent e) {
  72.                                 BookTypeManagerInterFrm bookTypeManageInterFrm=new BookTypeManagerInterFrm();
  73.                                 bookTypeManageInterFrm.setVisible(true);
  74.                                 table.add(bookTypeManageInterFrm);
  75.                         }
  76.                 });
  77.                 mntmNewMenuItem_5.setIcon(new ImageIcon(Mainframe.class.getResource("/images/维护.png")));
  78.                 mnNewMenu_2.add(mntmNewMenuItem_5);
  79.                
  80.                 JMenu mnNewMenu_3 = new JMenu("图书管理");
  81.                 mnNewMenu_3.setIcon(new ImageIcon(Mainframe.class.getResource("/images/图书管理.png")));
  82.                 mnNewMenu.add(mnNewMenu_3);
  83.                
  84.                 JMenuItem mntmNewMenuItem_2 = new JMenuItem("添加图书");
  85.                 mntmNewMenuItem_2.addActionListener(new ActionListener() {
  86.                         public void actionPerformed(ActionEvent e) {
  87.                                 BookAddInterFrm bookAddInterFrm=new BookAddInterFrm();
  88.                                 bookAddInterFrm.setVisible(true);
  89.                                 table.add(bookAddInterFrm);
  90.                         }
  91.                 });
  92.                 mntmNewMenuItem_2.setIcon(new ImageIcon(Mainframe.class.getResource("/images/添加.png")));
  93.                 mnNewMenu_3.add(mntmNewMenuItem_2);
  94.                
  95.                 JMenuItem mntmNewMenuItem_3 = new JMenuItem("图书维护");
  96.                 mntmNewMenuItem_3.addActionListener(new ActionListener() {
  97.                         public void actionPerformed(ActionEvent e) {
  98.                                 BookManageInterFrm bookAddInterFrm=new BookManageInterFrm();
  99.                                 bookAddInterFrm.setVisible(true);
  100.                                 table.add(bookAddInterFrm);
  101.                         }
  102.                 });
  103.                 mntmNewMenuItem_3.setIcon(new ImageIcon(Mainframe.class.getResource("/images/维护.png")));
  104.                 mnNewMenu_3.add(mntmNewMenuItem_3);
  105.                
  106.                 JMenuItem mntmNewMenuItem_4 = new JMenuItem("退出");
  107.                 mntmNewMenuItem_4.addActionListener(new ActionListener() {
  108.                         public void actionPerformed(ActionEvent e) {
  109.                                 int result=JOptionPane.showConfirmDialog(null,"是否退出");
  110.                                 if(result==0) {
  111.                                         dispose();
  112.                                 }
  113.                         }
  114.                 });
  115.                 mntmNewMenuItem_4.setIcon(new ImageIcon(Mainframe.class.getResource("/images/退出.png")));
  116.                 mnNewMenu.add(mntmNewMenuItem_4);
  117.                
  118.                 JMenu mnNewMenu_1 = new JMenu("关于我们");
  119.                 mnNewMenu_1.setIcon(new ImageIcon(Mainframe.class.getResource("/images/关于我们.png")));
  120.                 menuBar.add(mnNewMenu_1);
  121.                
  122.                 JMenuItem mntmNewMenuItem = new JMenuItem("关于Java");
  123.                 mntmNewMenuItem.addActionListener(new ActionListener() {
  124.                         public void actionPerformed(ActionEvent e) {
  125.                                 Java666interframe java666interframe=new Java666interframe();
  126.                                 java666interframe.setVisible(true);
  127.                                 table.add(java666interframe);
  128.                         }
  129.                 });
  130.                 mntmNewMenuItem.setIcon(new ImageIcon(Mainframe.class.getResource("/images/关于我们.png")));
  131.                 mnNewMenu_1.add(mntmNewMenuItem);
  132.                 contentPane = new JPanel();
  133.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  134.                 setContentPane(contentPane);
  135.                 contentPane.setLayout(new BorderLayout(0, 0));
  136.                 table = new JDesktopPane();
  137.                 table.setBackground(new Color(255, 255, 255));
  138.                 contentPane.add(table, BorderLayout.CENTER);
  139.                
  140.                 //设置最大化
  141.                 this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  142.         }
  143. }
复制代码
好的代码到这里就竣事了,末端我会给出整个包的连接,里面有图片等等。
这边启动是在Login界面开始启动的喔。。。。
3.效果展示:


 

 没有做过多的可视化啊,有爱好的小搭档可以自己修改(期末考试太多了要复习没空做了

4.末端体会心得:

这边想看我啰嗦的小搭档可以看看,不想看的就跳。
1.这个实战项目实现了一个小小的前后端分离的操作,固然很基础也很简单,但是紧张是让我学会了怎么进行这种类似项目的开发,以及为日后的毕设做准备,假如说各人想看详细的教程,我是仿照b站上一个视频写的,这边紧张是放源码并进行了一些修改。
java +swing(gui) +mysql 实现的图书管理体系_哔哩哔哩_bilibili
up主讲的有些快,盼望各人尽力跟上就好了。
2.这边实际上我是不发起各人直接cv的,固然要是时间紧迫嘛也没什么。各人做这个课程计划固然很大的目的是为了合格,但是我照旧盼望各人可以或许手动写一遍,以提升技术为目的,每次做完一个小项目都会让你受益良多。
3.这个开发的过程中呢,就算你是跟着视频写的,也很可能会写漏或者是写错什么变量。我这个代码里面就写错了一个变量但是不影响使用,有哪位细心的小搭档可以指出来并在评论区留言。紧张是因为我的这个eclipse主动填充变量名导致的,因此各人在开发项目之前对自己的编译器进行一定程度上的调节也是十分紧张的,齐备以自己用的舒服为先。
有什么问题也可以私聊或者直接@我都可以我会尽力帮各人解决。

5.网盘地址分享:

代码文件包:
链接: https://pan.baidu.com/s/1lulyGlbA50O5e82Kfdhkzw?pwd=ersp 提取码: ersp 复制这段内容后打开百度网盘手机App,操作更方便哦
驱动文件包(代码里面应该有了,保险起见照旧再发一份):
链接: https://pan.baidu.com/s/1KQMqJrSeLRDXryO72agOWw?pwd=dhte 提取码: dhte 复制这段内容后打开百度网盘手机App,操作更方便哦

最后祝各人都可以或许度过一个完美的暑假!!!!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

海哥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表