赞
踩
目录
12.BookTypeManagerInterFrm.java
本次设计基于eclipse+Javaswing+windowBuilder+JDBC+mysql实现,我会从头到尾对整个实现过程进行详细的讲解,最后我也会献上我的全部文件希望对大家有所帮助!!
安装流程我就不在这里多说了,网上都有,我这里将一些要点。
eclipse:博主选用的是2022-12的版本比较稳定,其他版本也可以,IDEA也是可以的,不是很建议用vscode,虽然集成但是终归没有专门的软件好使。
Java swing以及window Builder:这些都是插件,具体步骤可以去网上搜,Javaswing是一个库,安装完毕不需要管,这边着重讲一下window Builder。
首先就是安装:很多人安装的时候看软件右下角的进度条读完或者等不及了就直接退出eclipse了,这样子是不对的,一定要等安装成功后会有一个小弹窗出来让你重启eclipse,这样子才会成功,如果你要是安装失败了,要么重装eclipse,要么就打开eclipse等待一会儿,看看能否继续安装,博主就是第二种情况,打开后等待几分钟就安装好了。
然后是使用:
找到你们的项目位置,右键src选择新建,然后最底下有个选项“其他”,找到其中的windowBuilder就可以创建窗口了,具体可以自己操作下。
Mysql:这个应该不用我多说,大家都有安装的肯定,这边讲一下好用的操作端,博主用的是datagrip,但是这个要付费大家可以去搜搜破解版(博主自己就是破解的)。还有就是Navicat也是不错的,实际上都是可以的只要能够建立数据库即可。
注意:有很多小伙伴开始做的时候啥也不会,认为eclipse一定要连接数据库什么的,实际上完全不需要,这些我们会在代码里面进行操作。
首先我们需要建立至少四个包,如下图:
第一次写这个可能都不是很了解我就简单讲一下我的理解:
1.dao层,就是用于导入导出数据的,简单点来说我们要在里面写一些数据库的sql语句
2.model层,就是模型层,就是在里面写具体的实体类,再明白点就是你可以把你数据库里面的一个表看成一种类,几个表就建几个类。
3.util层,这个是工具层,你可以在里面写一些方法以便里面的使用,我后续也会进行讲解。
4.view层,这个就是视图层,你的窗口都写在这个里面,一般来说你的程序也会从这个里面的主界面开始运行。
5.至于image:这个就是用来存放你的一些图片方便调用
最后一点:一定要下载最后的那个引用的库,这边可以去网上找然后直接拖拽进去就好了,当然最后我也会将文件分享给大家,这个是数据库连接驱动,没下可连接不了哦。
首先先给大家一个数据库生成代码防止大家搞不出数据库:
下面没写创建数据库的语句,额就自己创建一下,数据库名称叫db_book,写错了代码可就跑不动了。
- create table t_booktype
- (
- id int auto_increment
- primary key,
- bookTypeName varchar(20) null,
- bookTpeDesc varchar(1000) null
- );
-
- create table t_book
- (
- id int auto_increment
- primary key,
- bookName varchar(20) not null,
- author varchar(20) null,
- sex varchar(10) null,
- price float null,
- bookTypeId int null,
- bookDesc varchar(1000) null,
- constraint t_book_t_booktype_id_fk
- foreign key (bookTypeId) references t_booktype (id)
- );
-
- create table t_user
- (
- id int auto_increment
- primary key,
- username varchar(20) not null,
- password varchar(20) not null
- );
直接粘贴在你的数据库查询台里就行了,可以自己看看效果,数据不重要可以自己添加。
这个其实也不是很好讲,我就一个一个来了,先给大家看下整个代码的样子(如果大家最后发现字符集什么的不行导致乱码就建议直接粘贴代码跑一下比较好):
如果有需要特别注意的地方我会指出,其他的无脑复制即可。
- package com.java1234.dao;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- import javax.swing.text.html.HTMLDocument.HTMLReader.ParagraphAction;
-
- import com.java1234.model.Book;
- import com.java1234.util.Stringutil;
-
- /**
- * book添加和删除
- * @author 46476
- *
- */
- public class BookDao {
-
- /**
- * 图书添加
- * @param con
- * @param book
- * @return
- * @throws Exception
- */
- public int add(Connection con,Book book)throws Exception{
- String sql="insert into t_book values(null,?,?,?,?,?,?)";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1, book.getBookName());
- pstmt.setString(2, book.getAuthor());
- pstmt.setString(3, book.getSex());
- pstmt.setFloat(4, book.getPrice());
- pstmt.setInt(5, book.getBookTypeId());
- pstmt.setString(6, book.getBookTypeDesc());
- return pstmt.executeUpdate();
- }
-
- /**
- * 图书信息查询
- * @param con
- * @param book
- * @return
- * @throws Exception
- */
- public ResultSet list(Connection con,Book book)throws Exception{
- StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
- if(Stringutil.isNotEmpty(book.getBookName())) {
- sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
- }
- if(Stringutil.isNotEmpty(book.getAuthor())) {
- sb.append(" and b.author like '%"+book.getAuthor()+"%'");
- }
- if(book.getBookTypeId()!=null&&book.getBookTypeId()!=-1) {
- sb.append(" and b.bookTypeId="+book.getBookTypeId());
- }
- PreparedStatement pstmt=con.prepareStatement(sb.toString());
- return pstmt.executeQuery();
- }
-
-
- /**
- * 删除记录条数
- * @param con
- * @param id
- * @return
- * @throws Exception
- */
- public int delete(Connection con,String id)throws Exception{
- String sql="delete from t_book where id=?";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1, id);
- return pstmt.executeUpdate();
- }
-
- public int updata(Connection con,Book book)throws Exception {
- String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookTypeId=?,bookDesc=? where id=?";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1, book.getBookName());
- pstmt.setString(2, book.getAuthor());
- pstmt.setString(3, book.getSex());
- pstmt.setFloat(4, book.getPrice());
- pstmt.setInt(5, book.getBookTypeId());
- pstmt.setString(6, book.getBookTypeDesc());//这边之前写错了应该是bookDesc
- pstmt.setInt(7, book.getId());
- return pstmt.executeUpdate();
- }
-
-
- /**
- * 指定图书类别下是否存在图书
- * @param con
- * @param bookTypeId
- * @return
- * @throws Exception
- */
- public boolean existBook(Connection con,String bookTypeId)throws Exception {
- String sql="select * from t_book where bookTypeId=?";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1, bookTypeId);
- ResultSet rs=pstmt.executeQuery();
- return rs.next();
- }
- }
- package com.java1234.dao;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- import com.java1234.model.BookType;
- import com.java1234.util.Stringutil;
-
- /**
- * 图书类别dao类
- * @author 46476
- *
- */
- public class BookTypeDao {
- /**
- * 图书类别添加
- * @param con
- * @param bookType
- * @return
- * @throws Exception
- */
- public int add(Connection con,BookType bookType)throws Exception{
- String sql="insert into t_bookType value(null,?,?)";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1, bookType.getBookTypeName());
- pstmt.setString(2,bookType.getBookTypeDesc() );
- return pstmt.executeUpdate();
- }
-
- /**
- * 查询图书类别
- * @param con
- * @param bookType
- * @return
- * @throws Exception
- */
- public ResultSet list(Connection con,BookType bookType)throws Exception{
- StringBuffer sb=new StringBuffer("select * from t_bookType");
- if(Stringutil.isNotEmpty(bookType.getBookTypeName())) {
- sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
- }
- PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
- return pstmt.executeQuery();
- }
-
-
- /**
- * 删除图书类别
- * @param con
- * @param id
- * @return
- * @throws Exception
- */
- public int delete(Connection con,String id)throws Exception{
- String sql="delete from t_bookType where id=?";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1, id);
- return pstmt.executeUpdate();
- }
-
-
- /**
- * 跟新图书类别
- * @param con
- * @param bookType
- * @return
- * @throws Exception
- */
- public int updata(Connection con,BookType bookType)throws Exception{
- String sql="update t_bookType set bookTypeName=?,bookTpeDesc=? where id=?";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1, bookType.getBookTypeName());
- pstmt.setString(2, bookType.getBookTypeDesc());
- pstmt.setInt(3, bookType.getId());
- return pstmt.executeUpdate();
- }
- }
- package com.java1234.dao;
-
- import java.nio.channels.SelectableChannel;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- import com.java1234.model.User;
-
- /**
- * 用户dao类
- * @author 46476
- *
- */
- public class UserDao {
- public User login(Connection con,User user)throws Exception {
- User resultUser=null;
- String sql="select * from t_user where userName=? and password=?";
- PreparedStatement pstmt=con.prepareStatement(sql);
- pstmt.setString(1, user.getUserNameString());
- pstmt.setString(2, user.getPasswordString());
- ResultSet rs=pstmt.executeQuery();
- if(rs.next()) {
- resultUser=new User();
- resultUser.setId(rs.getInt("id"));
- resultUser.setUserNameString(rs.getString("userName"));
- resultUser.setPasswordString(rs.getString("password"));
- }
- return resultUser;
- }
- }
- package com.java1234.model;
-
- import java.sql.Connection;
-
- import com.mysql.cj.protocol.a.NativeConstants.StringLengthDataType;
-
- public class Book {
- private int id;
- private String bookName;
- private String author;
- private String sex;
- private Float price;
- private Integer bookTypeId;
- private String bookTypeName;
- private String bookTypeDesc;
-
-
- public Book() {
- super();
- // TODO 自动生成的构造函数存根
- }
-
-
-
- public Book(int id, String bookName, String author, String sex, Float price, Integer bookTypeId,
- String bookTypeDesc) {
- super();
- this.id = id;
- this.bookName = bookName;
- this.author = author;
- this.sex = sex;
- this.price = price;
- this.bookTypeId = bookTypeId;
- this.bookTypeDesc = bookTypeDesc;
- }
-
-
-
- public Book(String bookName, String author, Integer bookTypeId) {
- super();
- this.bookName = bookName;
- this.author = author;
- this.bookTypeId = bookTypeId;
- }
-
- public Book(String bookName, String author, String sex, Float price, Integer bookTypeId, String bookTypeDesc) {
- super();
- this.bookName = bookName;
- this.author = author;
- this.sex = sex;
- this.price = price;
- this.bookTypeId = bookTypeId;
- this.bookTypeDesc = bookTypeDesc;
- }
-
-
-
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getBookName() {
- return bookName;
- }
- public void setBookName(String bookName) {
- this.bookName = bookName;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public Float getPrice() {
- return price;
- }
- public void setPrice(Float price) {
- this.price = price;
- }
- public Integer getBookTypeId() {
- return bookTypeId;
- }
- public void setBookTypeId(Integer bookTypeId) {
- this.bookTypeId = bookTypeId;
- }
- public String getBookTypeName() {
- return bookTypeName;
- }
- public void setBookTypeName(String bookTypeName) {
- this.bookTypeName = bookTypeName;
- }
- public String getBookTypeDesc() {
- return bookTypeDesc;
- }
- public void setBookTypeDesc(String bookTypeDesc) {
- this.bookTypeDesc = bookTypeDesc;
- }
-
- }
- package com.java1234.model;
- /**
- * 图书类别实体
- * @author 46476
- *
- */
- public class BookType {
- private int id;//编号
- private String bookTypeName;//图书类别名称
- private String bookTypeDesc;//图书备注
- public BookType() {
- super();
- }
-
- public BookType(int id, String bookTypeName, String bookTypeDesc) {
- super();
- this.id = id;
- this.bookTypeName = bookTypeName;
- this.bookTypeDesc = bookTypeDesc;
- }
-
- public BookType(String bookTypeName,String bookTypeDesc) {
- // TODO 自动生成的构造函数存根
- this.bookTypeName=bookTypeName;
- this.bookTypeDesc=bookTypeDesc;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getBookTypeName() {
- return bookTypeName;
- }
- public void setBookTypeName(String bookTypeName) {
- this.bookTypeName = bookTypeName;
- }
- public String getBookTypeDesc() {
- return bookTypeDesc;
- }
- public void setBookTypeDesc(String bookTypeDesc) {
- this.bookTypeDesc = bookTypeDesc;
- }
- public String toString() {
- return this.bookTypeName;
- }
-
- }
- package com.java1234.model;
- /**
- * 用户实体
- * @author 46476
- *
- */
- public class User {
- private int id;//编号
- private String userNameString;//用户名
- private String passwordString;//密码
-
-
-
- public User() {
- super();
- }
-
-
- public User(String userNameString, String passwordString) {
- super();
- this.userNameString = userNameString;
- this.passwordString = passwordString;
- }
-
-
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUserNameString() {
- return userNameString;
- }
- public void setUserNameString(String userNameString) {
- this.userNameString = userNameString;
- }
- public String getPasswordString() {
- return passwordString;
- }
- public void setPasswordString(String passwordString) {
- this.passwordString = passwordString;
- }
-
- }
这里注意啦!!!!!!!!!!!!!!!
这里面的用户名以及密码都是要填写自己的数据库用户名以及密码,也就是我打xxxx的地方。
- package com.java1234.util;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
-
- /**
- * 数据库工具类
- * @author 46476
- *
- */
- public class DbUtil {
- private String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";//数据库地址
- private String dbUserName="xxxxxx";//用户名
- private String dbPassword="xxxxxxxx";//密码
- private String jdbcNameString="com.mysql.cj.jdbc.Driver";//驱动名称
-
- public Connection getCon()throws Exception{
- Class.forName(jdbcNameString);
- Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
- return con;
- }
-
- public void closeCon(Connection con)throws Exception{
- if(con!=null)
- con.close();
- }
-
- public static void main(String[] args) {
- DbUtil dbUtil=new DbUtil();
- try {
- dbUtil.getCon();
- System.out.println("数据库连接成功");
- } catch (Exception e) {
- // TODO 自动生成的 catch 块
- e.printStackTrace();
- System.out.println("连接失败");
- }
- }
- }
-
-
- package com.java1234.util;
- /**
- * 字符串工具类
- * @author 46476
- *
- */
- public class Stringutil {
-
- //判断字符串是否为空
- public static boolean isEmpty(String str) {
- if(str==null||"".equals(str.trim())) {
- return true;
- }
- return false;
- }
-
- public static boolean isNotEmpty(String str) {
- if(str!=null&&"".equals(str.trim())==false)
- return true;
- return false;
- }
- }
从这里开始我需要提一嘴:中间的一大段代码都是不用看的,这些都是windowBuilder帮你自动生成的,你只需要知道怎么微调就行了,不会的无脑cv,会的可以自己调成喜欢的样子。
- package com.java1234.view;
-
- import java.awt.EventQueue;
-
- import javax.swing.JInternalFrame;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
-
- import java.awt.Font;
- import java.sql.Connection;
- import java.sql.ResultSet;
-
- import javax.swing.JTextField;
- import javax.swing.LayoutStyle.ComponentPlacement;
- import javax.swing.border.LineBorder;
-
- import com.java1234.dao.BookDao;
- import com.java1234.dao.BookTypeDao;
- import com.java1234.model.Book;
- import com.java1234.model.BookType;
- import com.java1234.util.DbUtil;
- import com.java1234.util.Stringutil;
-
- import javax.swing.JRadioButton;
- import javax.swing.ButtonGroup;
- import javax.swing.JTextArea;
- import javax.swing.JComboBox;
- import javax.swing.JButton;
- import javax.swing.ImageIcon;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
-
- public class BookAddInterFrm extends JInternalFrame {
-
- private static final long serialVersionUID = 1L;
- private JTextField bookNametxt;
- private JTextField authortxt;
- private final ButtonGroup buttonGroup = new ButtonGroup();
- private JTextField pricetxt;
- private JTextArea bookDesctxt;
- private JComboBox bookTypeJcb;
- private DbUtil dbUtil=new DbUtil();
- private BookTypeDao bookTypeDao=new BookTypeDao();
- private BookDao bookDao=new BookDao();
- private JRadioButton manjrb;
- JRadioButton womenjrb;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- BookAddInterFrm frame = new BookAddInterFrm();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public BookAddInterFrm() {
- setClosable(true);
- setIconifiable(true);
- setTitle("图书添加");
- setBounds(100, 100, 602, 705);
-
- JLabel lblNewLabel = new JLabel("图书名称:");
- lblNewLabel.setBounds(53, 82, 65, 18);
- lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- bookNametxt = new JTextField();
- bookNametxt.setBounds(136, 79, 126, 24);
- bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- bookNametxt.setColumns(10);
-
- JLabel lblNewLabel_1 = new JLabel("图书作者:");
- lblNewLabel_1.setBounds(336, 82, 55, 18);
- lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- authortxt = new JTextField();
- authortxt.setBounds(409, 79, 126, 24);
- authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- authortxt.setColumns(10);
-
- JLabel lblNewLabel_2 = new JLabel("作者性别:");
- lblNewLabel_2.setBounds(53, 168, 65, 18);
- lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- manjrb = new JRadioButton("男");
- manjrb.setBounds(136, 164, 39, 27);
- buttonGroup.add(manjrb);
- manjrb.setSelected(true);
- manjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- womenjrb = new JRadioButton("女");
- womenjrb.setBounds(193, 164, 39, 27);
- buttonGroup.add(womenjrb);
- womenjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JLabel lblNewLabel_3 = new JLabel("图书价格:");
- lblNewLabel_3.setBounds(336, 168, 65, 18);
- lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- pricetxt = new JTextField();
- pricetxt.setBounds(411, 165, 126, 24);
- pricetxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- pricetxt.setColumns(10);
-
- JLabel lblNewLabel_4 = new JLabel("图书描述:");
- lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- lblNewLabel_4.setBounds(53, 353, 65, 15);
-
- bookDesctxt = new JTextArea();
- bookDesctxt.setBounds(136, 348, 399, 242);
- getContentPane().setLayout(null);
- getContentPane().add(lblNewLabel);
- getContentPane().add(bookNametxt);
- getContentPane().add(lblNewLabel_4);
- getContentPane().add(lblNewLabel_2);
- getContentPane().add(manjrb);
- getContentPane().add(womenjrb);
- getContentPane().add(bookDesctxt);
- getContentPane().add(lblNewLabel_1);
- getContentPane().add(authortxt);
- getContentPane().add(lblNewLabel_3);
- getContentPane().add(pricetxt);
-
- JLabel lblNewLabel_5 = new JLabel("图书类别:");
- lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- lblNewLabel_5.setBounds(53, 273, 65, 15);
- getContentPane().add(lblNewLabel_5);
-
- bookTypeJcb = new JComboBox();
- bookTypeJcb.setBounds(136, 270, 126, 23);
- getContentPane().add(bookTypeJcb);
-
- JButton btnNewButton = new JButton("添加");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- bookAddActionPerformed(e);
- }
- });
- btnNewButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/添加.png")));
- btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- btnNewButton.setBounds(220, 620, 93, 23);
- getContentPane().add(btnNewButton);
-
- JButton btnNewButton_1 = new JButton("重置");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- resetValueActionPerformed(e);
- }
- });
- btnNewButton_1.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/重置.png")));
- btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- btnNewButton_1.setBounds(370, 620, 93, 23);
- getContentPane().add(btnNewButton_1);
-
- bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
- bookDesctxt.setLineWrap(true); //激活自动换行功能
- bookDesctxt.setWrapStyleWord(true); // 激活断行不断字功能
-
- fillBookType();
- }
-
-
- protected void resetValueActionPerformed(ActionEvent e) {
- // TODO 自动生成的方法存根
- this.resetValue();
- }
-
- /**
- * 图书添加处理
- * @param e
- */
- protected void bookAddActionPerformed(ActionEvent e) {
- // TODO 自动生成的方法存根
- String bookName=this.bookNametxt.getText();
- String author=this.authortxt.getText();
- String price=this.pricetxt.getText();
- String bookDesc=this.bookDesctxt.getText();
-
- if(Stringutil.isEmpty(bookName)) {
- JOptionPane.showConfirmDialog(null, "图书名称不能为空");
- return;
- }
- if(Stringutil.isEmpty(author)) {
- JOptionPane.showConfirmDialog(null, "作者不能为空");
- return;
- }
- if(Stringutil.isEmpty(price)) {
- JOptionPane.showConfirmDialog(null, "图书价格不能为空");
- return;
- }
-
- String sex="";
- if(manjrb.isSelected()) {
- sex="男";
- }
- else if(womenjrb.isSelected()) {
- sex="女";
- }
-
- BookType bookType=(BookType)bookTypeJcb.getSelectedItem();
- int bookTypeId = bookType.getId();
-
- Book book=new Book(bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc);
- Connection con=null;
- try {
- con=dbUtil.getCon();
- int addNum=bookDao.add(con,book);
- if(addNum==1) {
- JOptionPane.showConfirmDialog(null, "图书添加成功");
- this.resetValue();
- return ;
- }
- else {
- JOptionPane.showConfirmDialog(null, "图书添加失败");
- }
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showConfirmDialog(null, "图书添加失败");
- e2.printStackTrace();
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }
- }
- }
-
- /**
- * 重置表单
- */
- private void resetValue() {
- this.bookNametxt.setText("");
- this.pricetxt.setText("");
- this.authortxt.setText("");
- this.manjrb.setSelected(true);
- this.bookDesctxt.setText("");
- //意思就是说如果有下拉框选项,那么就默认选回第一个
- if(this.bookTypeJcb.getItemCount()>0) {
- this.bookTypeJcb.setSelectedIndex(0);
- }
- }
-
- /**
- * 初始化图书类别下拉框
- */
- private void fillBookType() {
- Connection con=null;
- BookType bookType=null;
- try {
- con=dbUtil.getCon();
- ResultSet rs =bookTypeDao.list(con, new BookType());
- while(rs.next()) {
- bookType=new BookType();
- bookType.setId(rs.getInt("id"));
- bookType.setBookTypeName(rs.getString("bookTypeName"));
- this.bookTypeJcb.addItem(bookType);
- }
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }finally {
-
- }
- }
- }
- package com.java1234.view;
-
- import java.awt.EventQueue;
-
- import javax.swing.JInternalFrame;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.table.DefaultTableModel;
-
- import com.java1234.dao.BookDao;
- import com.java1234.dao.BookTypeDao;
- import com.java1234.model.Book;
- import com.java1234.model.BookType;
- import com.java1234.util.DbUtil;
- import com.java1234.util.Stringutil;
- import com.mysql.cj.util.EscapeTokenizer;
-
- import javax.swing.LayoutStyle.ComponentPlacement;
- import javax.swing.JPanel;
- import javax.swing.border.TitledBorder;
- import javax.swing.border.EtchedBorder;
- import javax.swing.border.LineBorder;
-
- import java.awt.Color;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
-
- import java.awt.Font;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.util.Vector;
-
- import javax.swing.JTextField;
- import javax.swing.JComboBox;
- import javax.swing.JButton;
- import javax.swing.ImageIcon;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JRadioButton;
- import javax.swing.ButtonGroup;
- import javax.swing.JTextArea;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
-
- public class BookManageInterFrm extends JInternalFrame {
-
- private static final long serialVersionUID = 1L;
- private JTable booktable;
- private JTextField s_bookNametxt;
- private JTextField s_authortxt;
- private JComboBox s_bookTypejcb;
-
- private DbUtil dbUtil=new DbUtil();
- private BookTypeDao bookTypeDao=new BookTypeDao();
- private BookDao bookDao=new BookDao();
- private JTextField idtxt;
- private JTextField bookNametxt;
- private final ButtonGroup buttonGroup = new ButtonGroup();
- private JTextField pricetxt;
- private JTextField authortxt;
- private JRadioButton manjrb;
- private JRadioButton womenjrb;
- private JTextArea bookDesctxt;
- private JComboBox bookTypejcb;
-
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- BookManageInterFrm frame = new BookManageInterFrm();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public BookManageInterFrm() {
- setClosable(true);
- setIconifiable(true);
- setTitle("图书管理");
- setBounds(100, 100, 743, 728);
-
- JScrollPane scrollPane = new JScrollPane();
-
- JPanel panel = new JPanel();
- panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
-
- JPanel panel_1 = new JPanel();
- panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
- GroupLayout groupLayout = new GroupLayout(getContentPane());
- groupLayout.setHorizontalGroup(
- groupLayout.createParallelGroup(Alignment.TRAILING)
- .addGroup(groupLayout.createSequentialGroup()
- .addGap(21)
- .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
- .addComponent(panel_1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 673, Short.MAX_VALUE)
- .addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE)
- .addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(33))
- );
- groupLayout.setVerticalGroup(
- groupLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(groupLayout.createSequentialGroup()
- .addContainerGap()
- .addComponent(panel, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
- .addGap(34)
- .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
- .addGap(18)
- .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
- .addContainerGap())
- );
-
- JLabel lblNewLabel_3 = new JLabel("编号:");
- lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- idtxt = new JTextField();
- idtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- idtxt.setEnabled(false);
- idtxt.setColumns(10);
-
- JLabel lblNewLabel_4 = new JLabel("图书名称:");
- lblNewLabel_4.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- bookNametxt = new JTextField();
- bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- bookNametxt.setColumns(10);
-
- JLabel lblNewLabel_5 = new JLabel("作者性别:");
- lblNewLabel_5.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- manjrb = new JRadioButton("男");
- buttonGroup.add(manjrb);
- manjrb.setSelected(true);
- manjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- womenjrb = new JRadioButton("女");
- buttonGroup.add(womenjrb);
- womenjrb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JLabel lblNewLabel_6 = new JLabel("价格:");
- lblNewLabel_6.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- pricetxt = new JTextField();
- pricetxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- pricetxt.setColumns(10);
-
- JLabel lblNewLabel_7 = new JLabel("图书作者:");
- lblNewLabel_7.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- authortxt = new JTextField();
- authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- authortxt.setColumns(10);
-
- JLabel lblNewLabel_8 = new JLabel("图书类别:");
- lblNewLabel_8.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- bookTypejcb = new JComboBox();
- bookTypejcb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JLabel lblNewLabel_9 = new JLabel("图书描述:");
- lblNewLabel_9.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- bookDesctxt = new JTextArea();
-
- JButton btnNewButton_1 = new JButton("修改");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- bookUpdataActionEvent(e);
- }
- });
- btnNewButton_1.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/修改.png")));
- btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JButton btnNewButton_2 = new JButton("删除");
- btnNewButton_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- bookDeleteActionEvent(e);
- }
- });
- btnNewButton_2.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/删除.png")));
- btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- GroupLayout gl_panel_1 = new GroupLayout(panel_1);
- gl_panel_1.setHorizontalGroup(
- gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_1.createSequentialGroup()
- .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_1.createSequentialGroup()
- .addGap(28)
- .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_1.createSequentialGroup()
- .addComponent(lblNewLabel_9)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 523, GroupLayout.PREFERRED_SIZE))
- .addGroup(gl_panel_1.createSequentialGroup()
- .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_1.createSequentialGroup()
- .addComponent(lblNewLabel_3)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(18)
- .addComponent(lblNewLabel_4)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGroup(gl_panel_1.createSequentialGroup()
- .addComponent(lblNewLabel_6)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(18)
- .addComponent(lblNewLabel_7)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
- .addGap(40)
- .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_1.createSequentialGroup()
- .addComponent(lblNewLabel_5)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(manjrb)
- .addGap(18)
- .addComponent(womenjrb))
- .addGroup(gl_panel_1.createSequentialGroup()
- .addComponent(lblNewLabel_8)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE))))))
- .addGroup(gl_panel_1.createSequentialGroup()
- .addGap(167)
- .addComponent(btnNewButton_1)
- .addGap(129)
- .addComponent(btnNewButton_2)))
- .addContainerGap(8, Short.MAX_VALUE))
- );
- gl_panel_1.setVerticalGroup(
- gl_panel_1.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel_1.createSequentialGroup()
- .addContainerGap()
- .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel_5)
- .addComponent(lblNewLabel_4)
- .addComponent(bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(lblNewLabel_3)
- .addComponent(manjrb)
- .addComponent(womenjrb))
- .addGap(57)
- .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel_6)
- .addComponent(pricetxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(lblNewLabel_7)
- .addComponent(authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(lblNewLabel_8)
- .addComponent(bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(46)
- .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel_9)
- .addComponent(bookDesctxt, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
- .addGap(18)
- .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
- .addComponent(btnNewButton_1)
- .addComponent(btnNewButton_2))
- .addContainerGap(23, Short.MAX_VALUE))
- );
- panel_1.setLayout(gl_panel_1);
-
- JLabel lblNewLabel = new JLabel("图书名称:");
- lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- s_bookNametxt = new JTextField();
- s_bookNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- s_bookNametxt.setColumns(10);
-
- JLabel lblNewLabel_1 = new JLabel("作者:");
- lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- s_authortxt = new JTextField();
- s_authortxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- s_authortxt.setColumns(10);
-
- JLabel lblNewLabel_2 = new JLabel("图书类别:");
- lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- s_bookTypejcb = new JComboBox();
- s_bookTypejcb.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JButton btnNewButton = new JButton("查询");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- bookSearchActionPerformed(e);
- }
- });
- btnNewButton.setIcon(new ImageIcon(BookManageInterFrm.class.getResource("/images/查询.png")));
- btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- GroupLayout gl_panel = new GroupLayout(panel);
- gl_panel.setHorizontalGroup(
- gl_panel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel.createSequentialGroup()
- .addContainerGap()
- .addComponent(lblNewLabel)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(s_bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(24)
- .addComponent(lblNewLabel_1)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(s_authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(32)
- .addComponent(lblNewLabel_2)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
- .addComponent(btnNewButton)
- .addComponent(s_bookTypejcb, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE))
- .addContainerGap(31, Short.MAX_VALUE))
- );
- gl_panel.setVerticalGroup(
- gl_panel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel.createSequentialGroup()
- .addContainerGap()
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel)
- .addComponent(s_bookNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(lblNewLabel_1)
- .addComponent(s_authortxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(lblNewLabel_2)
- .addComponent(s_bookTypejcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(ComponentPlacement.UNRELATED)
- .addComponent(btnNewButton)
- .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- panel.setLayout(gl_panel);
-
- booktable = new JTable();
- booktable.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- bookTableMousePressed(e);
- }
- });
- booktable.setShowGrid(false);
- scrollPane.setViewportView(booktable);
- booktable.setModel(new DefaultTableModel(
- new Object[][] {
- },
- new String[] {
- "\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u56FE\u4E66\u4F5C\u8005", "\u4F5C\u8005\u6027\u522B", "\u56FE\u4E66\u4EF7\u683C", "\u56FE\u4E66\u63CF\u8FF0", "\u56FE\u4E66\u7C7B\u522B"
- }
- ) {
- boolean[] columnEditables = new boolean[] {
- true, false, false, false, false, false, false
- };
- public boolean isCellEditable(int row, int column) {
- return columnEditables[column];
- }
- });
- booktable.getColumnModel().getColumn(5).setPreferredWidth(173);
- getContentPane().setLayout(groupLayout);
-
- this.fillBookType("search");
- this.fillBookType("modify");
- this.fillTable(new Book());
- bookDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
- bookDesctxt.setLineWrap(true); //激活自动换行功能
- bookDesctxt.setWrapStyleWord(true); // 激活断行不断字功能
- }
-
- /**
- * 图书删除事件处理
- * @param e
- */
- protected void bookDeleteActionEvent(ActionEvent e) {
- // TODO 自动生成的方法存根
- String idString=idtxt.getText();
- if(Stringutil.isEmpty(idString)) {
- JOptionPane.showConfirmDialog(null, "请选择要删除的记录");
- return;
- }
- int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗");
- if(n==0) {
- Connection con=null;
- try {
- con=dbUtil.getCon();
- int deteleNum=bookDao.delete(con, idString);
- if(deteleNum==1) {
- JOptionPane.showConfirmDialog(null, "删除成功");
- this.fillTable(new Book());
- this.resetValue();
- }
- else {
- JOptionPane.showConfirmDialog(null, "删除失败");
- }
- } catch (Exception e2) {
- // TODO: handle exception
- e2.printStackTrace();
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }
- }
- }
- }
-
- /**
- * 图书修改事件处理
- * @param e
- */
- protected void bookUpdataActionEvent(ActionEvent e) {
- // TODO 自动生成的方法存根
- String id=this.idtxt.getText();
- if(Stringutil.isEmpty(id)) {
- JOptionPane.showConfirmDialog(null, "请选择一条记录");
- return;
- }
- String bookName=this.bookNametxt.getText();
- String author=this.authortxt.getText();
- String price=this.pricetxt.getText();
- String bookDesc=this.bookDesctxt.getText();
-
- if(Stringutil.isEmpty(bookName)) {
- JOptionPane.showConfirmDialog(null, "图书名称不能为空");
- return;
- }
- if(Stringutil.isEmpty(author)) {
- JOptionPane.showConfirmDialog(null, "作者不能为空");
- return;
- }
- if(Stringutil.isEmpty(price)) {
- JOptionPane.showConfirmDialog(null, "图书价格不能为空");
- return;
- }
-
- String sex=null;
- if(manjrb.isSelected()) {
- sex="男";
- }
- else {
- sex="女";
- }
- BookType bookType=(BookType) bookTypejcb.getSelectedItem();
- int bookTypeId=bookType.getId();
-
- Book book=new Book(Integer.parseInt(id), bookName, author, sex, Float.parseFloat(price), bookTypeId,
- bookDesc);
-
-
- Connection con=null;
- try {
- con=dbUtil.getCon();
- int addNum=bookDao.updata(con,book);
- if(addNum==1) {
- JOptionPane.showConfirmDialog(null, "图书修改成功");
- this.resetValue();
- this.fillTable(new Book());
- return ;
- }
- else {
- JOptionPane.showConfirmDialog(null, "图书修改失败");
- }
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showConfirmDialog(null, "图书修改失败");
- e2.printStackTrace();
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }
- }
- }
-
- private void resetValue() {
- // TODO 自动生成的方法存根
- this.idtxt.setText("");
- this.bookNametxt.setText("");
- this.pricetxt.setText("");
- this.authortxt.setText("");
- this.manjrb.setSelected(true);
- this.bookDesctxt.setText("");
- //意思就是说如果有下拉框选项,那么就默认选回第一个
- if(this.bookTypejcb.getItemCount()>0) {
- this.bookTypejcb.setSelectedIndex(0);
- }
- }
-
- /**
- * 表格点击事件处理
- * @param e
- */
- protected void bookTableMousePressed(MouseEvent e) {
- // TODO 自动生成的方法存根
- int row=this.booktable.getSelectedRow();
- this.idtxt.setText((String) booktable.getValueAt(row, 0));
- this.bookNametxt.setText((String) booktable.getValueAt(row, 1));
- this.authortxt.setText((String) booktable.getValueAt(row, 2));
- String sex=(String) booktable.getValueAt(row, 3);
- if(sex.equals("男")) {
- this.manjrb.setSelected(true);
- }
- else {
- this.womenjrb.setSelected(true);
- }
- this.pricetxt.setText((Float) booktable.getValueAt(row, 4)+"");
- this.bookDesctxt.setText((String) booktable.getValueAt(row, 5));
- String bookTypeName=(String) this.booktable.getValueAt(row, 6);
- int n=this.bookTypejcb.getItemCount();
- for(int i=0;i<n;i++) {
- BookType item=(BookType) this.bookTypejcb.getItemAt(i);
- if(item.getBookTypeName().equals(bookTypeName)) {
- this.bookTypejcb.setSelectedIndex(i);
- }
- }
- }
-
- /**
- * 图书查询事件
- * @param e
- */
- protected void bookSearchActionPerformed(ActionEvent e) {
- // TODO 自动生成的方法存根
- String bookName=this.s_bookNametxt.getText();
- String authorString=this.s_authortxt.getText();
- BookType bookType=(BookType)this.s_bookTypejcb.getSelectedItem();
- int bookTypeId=bookType.getId();
-
- Book book=new Book(bookName,authorString,bookTypeId);
- this.fillTable(book);
- }
-
- /**
- * 初始化下拉框
- * @param Type
- */
- private void fillBookType(String Type) {
- Connection con=null;
- BookType bookType=null;
- try {
- con=dbUtil.getCon();
- ResultSet rs=bookTypeDao.list(con, new BookType());
- //设置默认
- if("search".equals(Type)) {
- bookType=new BookType();
- bookType.setBookTypeName("请选择...");
- bookType.setId(-1);
- this.s_bookTypejcb.addItem(bookType);
- }
- while(rs.next()) {
- bookType=new BookType();
- bookType.setBookTypeName(rs.getString("bookTypeName"));
- bookType.setId(rs.getInt("id"));
- if("search".equals(Type)) {
- this.s_bookTypejcb.addItem(bookType);
- }
- else if("modify".equals(Type)) {
- this.bookTypejcb.addItem(bookType);
- }
- }
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e) {
- // TODO 自动生成的 catch 块
- e.printStackTrace();
- }
- }
- }
-
-
- /**
- * 初始化表格数据
- * @param book
- */
- private void fillTable(Book book) {
- DefaultTableModel dtm=(DefaultTableModel)booktable.getModel();
- dtm.setRowCount(0);//设置成0行
- Connection con=null;
- try {
- con = dbUtil.getCon();
-
- } catch (Exception e3) {
- // TODO 自动生成的 catch 块
- e3.printStackTrace();
- }
- try {
- ResultSet rs=bookDao.list(con, book);
- while(rs.next()) {
- Vector v=new Vector();
- v.add(rs.getString("id"));
- v.add(rs.getString("bookName"));
- v.add(rs.getString("author"));
- v.add(rs.getString("sex"));
- v.add(rs.getFloat("price"));
- v.add(rs.getString("bookDesc"));
- v.add(rs.getString("bookTypeName"));
- dtm.addRow(v);
- }
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e) {
- // TODO 自动生成的 catch 块
- e.printStackTrace();
- }
- }
- }
- }
- package com.java1234.view;
-
- import java.awt.EventQueue;
-
- import javax.swing.JInternalFrame;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import javax.swing.JTextArea;
- import javax.swing.JButton;
- import javax.swing.LayoutStyle.ComponentPlacement;
- import javax.swing.border.LineBorder;
-
- import com.java1234.dao.BookTypeDao;
- import com.java1234.model.BookType;
- import com.java1234.util.DbUtil;
- import com.java1234.util.Stringutil;
-
- import java.awt.Font;
- import javax.swing.ImageIcon;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.awt.event.ActionEvent;
-
- public class BookTypeAddInterFrm extends JInternalFrame {
-
- private static final long serialVersionUID = 1L;
- private JTextField bookTypeNametxt;
- private JTextArea bookTypeDesctxt;
-
- private DbUtil dbUtil = new DbUtil();
- private BookTypeDao bookTypeDao=new BookTypeDao();
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- BookTypeAddInterFrm frame = new BookTypeAddInterFrm();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public BookTypeAddInterFrm() {
- setClosable(true);
- setTitle("图书类别添加");
- setBounds(100, 100, 537, 300);
-
- JLabel lblNewLabel = new JLabel("图书类别名称:");
- lblNewLabel.setBounds(78, 64, 111, 18);
- lblNewLabel.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/导入.png")));
- lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JLabel lblNewLabel_1 = new JLabel("图书类别描述:");
- lblNewLabel_1.setBounds(78, 137, 111, 18);
- lblNewLabel_1.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/project.png")));
- lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- bookTypeNametxt = new JTextField();
- bookTypeNametxt.setBounds(199, 61, 216, 24);
- bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- bookTypeNametxt.setColumns(10);
-
- bookTypeDesctxt = new JTextArea();
- bookTypeDesctxt.setBounds(199, 135, 216, 79);
- bookTypeDesctxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JButton btnNewButton = new JButton("添加");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- bookTypeAddActionPerformed(e);
- }
- });
- btnNewButton.setBounds(124, 233, 79, 27);
- btnNewButton.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/添加.png")));
- btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JButton btnNewButton_1 = new JButton("重置");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- resetValueActionPerformed(e);
- }
- });
- btnNewButton_1.setBounds(253, 233, 79, 27);
- btnNewButton_1.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/重置.png")));
- btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- getContentPane().setLayout(null);
- getContentPane().add(lblNewLabel);
- getContentPane().add(bookTypeNametxt);
- getContentPane().add(lblNewLabel_1);
- getContentPane().add(btnNewButton);
- getContentPane().add(btnNewButton_1);
- getContentPane().add(bookTypeDesctxt);
-
-
- //设置文本域边框
- bookTypeDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
- bookTypeDesctxt.setLineWrap(true); //激活自动换行功能
- bookTypeDesctxt.setWrapStyleWord(true); // 激活断行不断字功能
- }
-
- /**
- * 图书类别添加事件
- * @param e
- */
- protected void bookTypeAddActionPerformed(ActionEvent e) {
- // TODO 自动生成的方法存根
- String bookTypeName=this.bookTypeNametxt.getText();
- String bookTypeDesc=this.bookTypeDesctxt.getText();
- if(Stringutil.isEmpty(bookTypeName)) {
- JOptionPane.showConfirmDialog(null, "图书类别名称不能为空");
- return;
- }
- BookType bookType=new BookType(bookTypeName,bookTypeDesc);
- Connection con = null;
- try {
- con = dbUtil.getCon();
- } catch (Exception e3) {
- // TODO 自动生成的 catch 块
- e3.printStackTrace();
- }
- try {
- int n=bookTypeDao.add(con, bookType);
- if(n==1) {
- JOptionPane.showConfirmDialog(null, "图书类别添加成功");
- resetValue();
- return;
- }
- else {
- JOptionPane.showConfirmDialog(null, "添加失败");
- }
- } catch (Exception e2) {
- // TODO: handle exception
- e2.printStackTrace();
- JOptionPane.showConfirmDialog(null, "添加失败");
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }
- }
- }
-
- //重置事件处理
- protected void resetValueActionPerformed(ActionEvent e) {
- // TODO 自动生成的方法存根
- this.resetValue();
- }
-
- /**
- * 重置表单
- */
- private void resetValue() {
- this.bookTypeNametxt.setText("");
- this.bookTypeDesctxt.setText("");
- }
- }
- package com.java1234.view;
-
- import java.awt.EventQueue;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.util.Vector;
-
- import javax.swing.JInternalFrame;
- import javax.swing.JScrollPane;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JTable;
- import javax.swing.table.DefaultTableModel;
-
- import com.java1234.dao.BookDao;
- import com.java1234.dao.BookTypeDao;
- import com.java1234.model.BookType;
- import com.java1234.util.DbUtil;
- import com.java1234.util.Stringutil;
-
- import javax.swing.border.LineBorder;
- import java.awt.Color;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
-
- import java.awt.Font;
- import javax.swing.JTextField;
- import javax.swing.LayoutStyle.ComponentPlacement;
- import javax.swing.JButton;
- import javax.swing.ImageIcon;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JPanel;
- import javax.swing.border.TitledBorder;
- import javax.swing.JTextArea;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
-
- public class BookTypeManagerInterFrm extends JInternalFrame {
-
- private static final long serialVersionUID = 1L;
- private JTable bookTypeTable;
- private DbUtil dbUtil = new DbUtil();
- private BookTypeDao bookTypeDao=new BookTypeDao();
- private BookDao bookDao=new BookDao();
- private JTextField s_bookTypeNametxt;
- private JTextField idtxt;
- private JTextField bookTypeNametxt;
- private JTextArea bookTypeDesctxt;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- BookTypeManagerInterFrm frame = new BookTypeManagerInterFrm();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public BookTypeManagerInterFrm() {
- setClosable(true);
- setIconifiable(true);
- setTitle("图书类别管理");
- setBounds(100, 100, 678, 622);
-
- JScrollPane scrollPane = new JScrollPane();
-
- JLabel lblNewLabel = new JLabel("图书类别名称:");
- lblNewLabel.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/project.png")));
- lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- s_bookTypeNametxt = new JTextField();
- s_bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- s_bookTypeNametxt.setColumns(10);
-
- JButton btnNewButton = new JButton("查询");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- bookTypeSearchActionPerformed(e);
- }
- });
- btnNewButton.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/查询.png")));
- btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JPanel panel = new JPanel();
- panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
- GroupLayout groupLayout = new GroupLayout(getContentPane());
- groupLayout.setHorizontalGroup(
- groupLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(groupLayout.createSequentialGroup()
- .addGap(48)
- .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
- .addGroup(groupLayout.createSequentialGroup()
- .addComponent(lblNewLabel)
- .addPreferredGap(ComponentPlacement.UNRELATED)
- .addComponent(s_bookTypeNametxt, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
- .addGap(45)
- .addComponent(btnNewButton))
- .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 547, Short.MAX_VALUE)
- .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addContainerGap(57, Short.MAX_VALUE))
- );
- groupLayout.setVerticalGroup(
- groupLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(groupLayout.createSequentialGroup()
- .addGap(42)
- .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel)
- .addComponent(s_bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(btnNewButton))
- .addGap(27)
- .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
- .addGap(38)
- .addComponent(panel, GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
- .addContainerGap())
- );
-
- JLabel lblNewLabel_1 = new JLabel("编号:");
- lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- idtxt = new JTextField();
- idtxt.setEditable(false);
- idtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- idtxt.setColumns(10);
-
- JLabel lblNewLabel_2 = new JLabel("图书类别名称:");
- lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- bookTypeNametxt = new JTextField();
- bookTypeNametxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- bookTypeNametxt.setColumns(10);
-
- JLabel lblNewLabel_3 = new JLabel("描述:");
- lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- bookTypeDesctxt = new JTextArea();
-
- JButton btnNewButton_1 = new JButton("修改");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- bookTypeUpdataActionEvent(e);
- }
- });
- btnNewButton_1.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/修改.png")));
- btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
-
- JButton btnNewButton_2 = new JButton("删除");
- btnNewButton_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- bookTypeDeleteActionEvent(e);
- }
- });
- btnNewButton_2.setIcon(new ImageIcon(BookTypeManagerInterFrm.class.getResource("/images/删除.png")));
- btnNewButton_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- GroupLayout gl_panel = new GroupLayout(panel);
- gl_panel.setHorizontalGroup(
- gl_panel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel.createSequentialGroup()
- .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel.createSequentialGroup()
- .addComponent(lblNewLabel_1)
- .addGap(18)
- .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(44)
- .addComponent(lblNewLabel_2)
- .addPreferredGap(ComponentPlacement.UNRELATED)
- .addComponent(bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGroup(gl_panel.createSequentialGroup()
- .addComponent(lblNewLabel_3)
- .addGap(18)
- .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel.createSequentialGroup()
- .addComponent(btnNewButton_1)
- .addGap(105)
- .addComponent(btnNewButton_2))
- .addComponent(bookTypeDesctxt, GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE))))
- .addContainerGap(81, Short.MAX_VALUE))
- );
- gl_panel.setVerticalGroup(
- gl_panel.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_panel.createSequentialGroup()
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel_1)
- .addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addComponent(lblNewLabel_2)
- .addComponent(bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(50)
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel_3)
- .addComponent(bookTypeDesctxt, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE))
- .addGap(29)
- .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
- .addComponent(btnNewButton_1)
- .addComponent(btnNewButton_2))
- .addContainerGap(18, Short.MAX_VALUE))
- );
- panel.setLayout(gl_panel);
-
- bookTypeTable = new JTable();
- bookTypeTable.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- bookTypeMousePressed(e);
- }
- });
- bookTypeTable.setShowGrid(false);
- bookTypeTable.setModel(new DefaultTableModel(
- new Object[][] {
- },
- new String[] {
- "\u7F16\u53F7", "\u56FE\u4E66\u7C7B\u522B\u540D\u79F0", "\u56FE\u4E66\u7C7B\u522B\u5185\u5BB9"
- }
- ) {
- boolean[] columnEditables = new boolean[] {
- false, false, false
- };
- public boolean isCellEditable(int row, int column) {
- return columnEditables[column];
- }
- });
- bookTypeTable.getColumnModel().getColumn(0).setPreferredWidth(49);
- bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(103);
- bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(142);
- scrollPane.setViewportView(bookTypeTable);
- getContentPane().setLayout(groupLayout);
-
- //初始化表格
- this.fillTable(new BookType());
- bookTypeDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
- bookTypeDesctxt.setLineWrap(true); //激活自动换行功能
- bookTypeDesctxt.setWrapStyleWord(true); // 激活断行不断字功能
- }
-
- /**
- * 删除事件处理
- * @param e
- */
- protected void bookTypeDeleteActionEvent(ActionEvent e) {
- // TODO 自动生成的方法存根
- String idString=idtxt.getText();
- if(Stringutil.isEmpty(idString)) {
- JOptionPane.showConfirmDialog(null, "请选择要删除的记录");
- return;
- }
- int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗");
- if(n==0) {
- Connection con=null;
- try {
- con=dbUtil.getCon();
- boolean fl=bookDao.existBook(con, idString);
- if(fl==true) {
- JOptionPane.showConfirmDialog(null, "当前图书类型下有图书无法删除");
- return ;
- }
- int deteleNum=bookTypeDao.delete(con, idString);
- if(deteleNum==1) {
- JOptionPane.showConfirmDialog(null, "删除成功");
- this.fillTable(new BookType());
- this.resetValue();
- }
- else {
- JOptionPane.showConfirmDialog(null, "删除失败");
- }
- } catch (Exception e2) {
- // TODO: handle exception
- JOptionPane.showConfirmDialog(null, "删除失败");
- e2.printStackTrace();
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }
- }
- }
- }
-
-
- /**
- * 图书类别修改
- * @param e
- */
- protected void bookTypeUpdataActionEvent(ActionEvent e) {
- // TODO 自动生成的方法存根
- String id=idtxt.getText();
- String bookTypeName=bookTypeNametxt.getText();
- String bookTypeDesc=bookTypeDesctxt.getText();
- if(Stringutil.isEmpty(id)) {
- JOptionPane.showConfirmDialog(null, "请选择要修改的记录");
- return;
- }
- else if(Stringutil.isEmpty(bookTypeName)) {
- JOptionPane.showConfirmDialog(null, "类别不能为空");
- return;
- }
- else {
- BookType bookType=new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);
- Connection con=null;
- try {
- con=dbUtil.getCon();
- int modifNum=bookTypeDao.updata(con, bookType);
- if(modifNum==1) {
- JOptionPane.showConfirmDialog(null, "修改成功");
- this.resetValue();
- //这里是为了直接刷新结果
- this.fillTable(new BookType());
- }
- else {
- JOptionPane.showConfirmDialog(null, "修改失败");
- }
- } catch (Exception e2) {
- // TODO: handle exception
- e2.printStackTrace();
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e3) {
- // TODO: handle exception
- e3.printStackTrace();
- }
- }
- }
- }
-
- /**
- * 表格行点击事件处理
- * @param e
- */
- protected void bookTypeMousePressed(MouseEvent e) {
- // TODO 自动生成的方法存根
- int row=bookTypeTable.getSelectedRow();
- idtxt.setText((String)bookTypeTable.getValueAt(row, 0));
- bookTypeNametxt.setText((String)bookTypeTable.getValueAt(row, 1));
- bookTypeDesctxt.setText((String)bookTypeTable.getValueAt(row, 2));
- }
-
- //图书类别查询事件
- protected void bookTypeSearchActionPerformed(ActionEvent evt) {
- // TODO 自动生成的方法存根
- String s_bookTypeName=this.s_bookTypeNametxt.getText();
- BookType bookType=new BookType();
- bookType.setBookTypeName(s_bookTypeName);
- this.fillTable(bookType);
- }
-
- private void fillTable(BookType bookType) {
- DefaultTableModel dtm=(DefaultTableModel)bookTypeTable.getModel();
- dtm.setRowCount(0);//设置成0行
- Connection con=null;
- try {
- con = dbUtil.getCon();
-
- } catch (Exception e3) {
- // TODO 自动生成的 catch 块
- e3.printStackTrace();
- }
- try {
- ResultSet rs=bookTypeDao.list(con, bookType);
- while(rs.next()) {
- Vector v=new Vector();
- v.add(rs.getString("id"));
- v.add(rs.getString("bookTypeName"));
- v.add(rs.getString("bookTpeDesc"));
- dtm.addRow(v);
- }
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }finally {
- try {
- dbUtil.closeCon(con);
- } catch (Exception e) {
- // TODO 自动生成的 catch 块
- e.printStackTrace();
- }
- }
- }
-
-
- /**
- * 重置表单
- */
- private void resetValue() {
- this.idtxt.setText("");
- this.bookTypeDesctxt.setText("");
- this.bookTypeNametxt.setText("");
- }
- }
- package com.java1234.view;
-
- import java.awt.EventQueue;
-
- import javax.swing.JInternalFrame;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JLabel;
- import javax.swing.ImageIcon;
-
- public class Java666interframe extends JInternalFrame {
-
- private static final long serialVersionUID = 1L;
-
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Java666interframe frame = new Java666interframe();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public Java666interframe() {
- getContentPane().setBackground(new Color(255, 255, 255));
-
- JLabel lblNewLabel = new JLabel("");
- lblNewLabel.setIcon(new ImageIcon(Java666interframe.class.getResource("/images/原神 启动!!!!.png")));
- GroupLayout groupLayout = new GroupLayout(getContentPane());
- groupLayout.setHorizontalGroup(
- groupLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
- .addContainerGap(95, Short.MAX_VALUE)
- .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 265, GroupLayout.PREFERRED_SIZE)
- .addGap(74))
- );
- groupLayout.setVerticalGroup(
- groupLayout.createParallelGroup(Alignment.LEADING)
- .addGroup(groupLayout.createSequentialGroup()
- .addGap(75)
- .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
- .addContainerGap(80, Short.MAX_VALUE))
- );
- getContentPane().setLayout(groupLayout);
- setIconifiable(true);
- setClosable(true);
- setTitle("关于我们");
- setBounds(100, 100, 450, 300);
-
- }
- }
- package com.java1234.view;
-
- import java.awt.EventQueue;
-
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
-
- import com.java1234.dao.UserDao;
- import com.java1234.model.User;
- import com.java1234.util.DbUtil;
- import com.java1234.util.Stringutil;
-
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.ImageIcon;
- import java.awt.Font;
- import javax.swing.LayoutStyle.ComponentPlacement;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.awt.event.ActionEvent;
- import javax.swing.JComboBox;
-
- public class Login extends JFrame {
-
- private static final long serialVersionUID = 1L;
- private JPanel contentPane;
- private JTextField userNameTxt;
- private JTextField passwordtxt;
-
- private DbUtil dbUtil=new DbUtil();
- private UserDao userDao=new UserDao();
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- String lookAndFeel ="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
-
- try {
- UIManager.setLookAndFeel(lookAndFeel);
- } catch (ClassNotFoundException e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- } catch (InstantiationException e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- } catch (IllegalAccessException e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- } catch (UnsupportedLookAndFeelException e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Login frame = new Login();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public Login() {
- setTitle("管理员登录");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 601, 355);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
-
- setContentPane(contentPane);
-
- JLabel lblNewLabel = new JLabel("图书管理系统");
- lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 19));
- lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/images/图书 (1).png")));
-
- JLabel lblNewLabel_1 = new JLabel("用户名:");
- lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- lblNewLabel_1.setIcon(new ImageIcon(Login.class.getResource("/images/用户名-登录页.png")));
-
- JLabel lblNewLabel_2 = new JLabel("密 码:");
- lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- lblNewLabel_2.setIcon(new ImageIcon(Login.class.getResource("/images/密码.png")));
-
- userNameTxt = new JTextField();
- userNameTxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- userNameTxt.setColumns(10);
-
- passwordtxt = new JTextField();
- passwordtxt.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- passwordtxt.setColumns(10);
-
- JButton btnNewButton = new JButton("登录");
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- loginActionPerformed(e);
- }
- });
- btnNewButton.setFocusable(false);
- btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- btnNewButton.setIcon(new ImageIcon(Login.class.getResource("/images/登录.png")));
-
- JButton btnNewButton_1 = new JButton("重置");
- btnNewButton_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- resetValueActionPerformed(e);
- }
- });
- btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 13));
- btnNewButton_1.setFocusable(false);
- btnNewButton_1.setIcon(new ImageIcon(Login.class.getResource("/images/重置.png")));
- GroupLayout gl_contentPane = new GroupLayout(contentPane);
- gl_contentPane.setHorizontalGroup(
- gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGap(96)
- .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addComponent(lblNewLabel_2)
- .addComponent(lblNewLabel_1))
- .addGap(38)
- .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
- .addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(userNameTxt)
- .addComponent(passwordtxt)))
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGap(47)
- .addComponent(btnNewButton)
- .addGap(135)
- .addComponent(btnNewButton_1)))
- .addContainerGap(164, Short.MAX_VALUE))
- );
- gl_contentPane.setVerticalGroup(
- gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGap(25)
- .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel_1)
- .addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(49)
- .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
- .addComponent(lblNewLabel_2)
- .addComponent(passwordtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
- .addGap(40)
- .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
- .addComponent(btnNewButton)
- .addComponent(btnNewButton_1))
- .addContainerGap(49, Short.MAX_VALUE))
- );
- contentPane.setLayout(gl_contentPane);
-
-
- //窗口居中
- this.setLocationRelativeTo(null);
- }
-
- /**
- * 登录事件处理
- * @param e
- */
- protected void loginActionPerformed(ActionEvent e) {
- // TODO 自动生成的方法存根
- String userName=this.userNameTxt.getText();
- String password=this.passwordtxt.getText();
- if(Stringutil.isEmpty(userName)) {
- JOptionPane.showConfirmDialog(null, "用户名不能为空");
- return ;
- }
- if(Stringutil.isEmpty(password)) {
- JOptionPane.showConfirmDialog(null, "密码不能为空");
- return ;
- }
- User user=new User(userName,password);
- Connection con=null;
- try {
- con=dbUtil.getCon();
- User currentUser=userDao.login(con, user);
- if(currentUser!=null) {
- dispose();
- new Mainframe().setVisible(true);
- }
- else {
- JOptionPane.showConfirmDialog(null, "用户名或者密码错误");
- }
- } catch (Exception e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }finally{
- try {
- dbUtil.closeCon(con);
- } catch (Exception e1) {
- // TODO 自动生成的 catch 块
- e1.printStackTrace();
- }
- }
-
- }
-
- /**
- * 重置事件
- * @param e
- */
- protected void resetValueActionPerformed(ActionEvent e) {
- // TODO 自动生成的方法存根
- this.userNameTxt.setText("");
- this.passwordtxt.setText("");
- }
- }
- package com.java1234.view;
-
- import java.awt.EventQueue;
-
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
-
- import com.mysql.cj.xdevapi.Table;
-
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JMenuBar;
- import javax.swing.JMenu;
- import javax.swing.JMenuItem;
- import javax.swing.JOptionPane;
- import javax.swing.ImageIcon;
- import javax.swing.JDesktopPane;
- import java.awt.BorderLayout;
- import javax.swing.JLayeredPane;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.Color;
-
- public class Mainframe extends JFrame {
-
- private static final long serialVersionUID = 1L;
- private JPanel contentPane;
- private JDesktopPane table = null;
-
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Mainframe frame = new Mainframe();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
-
- /**
- * Create the frame.
- */
- public Mainframe() {
- setTitle("图书管理系统");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
-
- JMenuBar menuBar = new JMenuBar();
- setJMenuBar(menuBar);
-
- JMenu mnNewMenu = new JMenu("基本数据维护");
- mnNewMenu.setIcon(new ImageIcon(Mainframe.class.getResource("/images/数据维护-基础数据.png")));
- menuBar.add(mnNewMenu);
-
- JMenu mnNewMenu_2 = new JMenu("图书类别管理");
- mnNewMenu_2.setIcon(new ImageIcon(Mainframe.class.getResource("/images/图书类别管理.png")));
- mnNewMenu.add(mnNewMenu_2);
-
- JMenuItem mntmNewMenuItem_1 = new JMenuItem("图书类别添加");
- mntmNewMenuItem_1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- BookTypeAddInterFrm bookTypeAddInterFrm=new BookTypeAddInterFrm();
- bookTypeAddInterFrm.setVisible(true);
- table.add(bookTypeAddInterFrm);
- }
- });
- mntmNewMenuItem_1.setIcon(new ImageIcon(Mainframe.class.getResource("/images/添加.png")));
- mnNewMenu_2.add(mntmNewMenuItem_1);
-
- JMenuItem mntmNewMenuItem_5 = new JMenuItem("图书类别维护");
- mntmNewMenuItem_5.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- BookTypeManagerInterFrm bookTypeManageInterFrm=new BookTypeManagerInterFrm();
- bookTypeManageInterFrm.setVisible(true);
- table.add(bookTypeManageInterFrm);
- }
- });
- mntmNewMenuItem_5.setIcon(new ImageIcon(Mainframe.class.getResource("/images/维护.png")));
- mnNewMenu_2.add(mntmNewMenuItem_5);
-
- JMenu mnNewMenu_3 = new JMenu("图书管理");
- mnNewMenu_3.setIcon(new ImageIcon(Mainframe.class.getResource("/images/图书管理.png")));
- mnNewMenu.add(mnNewMenu_3);
-
- JMenuItem mntmNewMenuItem_2 = new JMenuItem("添加图书");
- mntmNewMenuItem_2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- BookAddInterFrm bookAddInterFrm=new BookAddInterFrm();
- bookAddInterFrm.setVisible(true);
- table.add(bookAddInterFrm);
- }
- });
- mntmNewMenuItem_2.setIcon(new ImageIcon(Mainframe.class.getResource("/images/添加.png")));
- mnNewMenu_3.add(mntmNewMenuItem_2);
-
- JMenuItem mntmNewMenuItem_3 = new JMenuItem("图书维护");
- mntmNewMenuItem_3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- BookManageInterFrm bookAddInterFrm=new BookManageInterFrm();
- bookAddInterFrm.setVisible(true);
- table.add(bookAddInterFrm);
- }
- });
- mntmNewMenuItem_3.setIcon(new ImageIcon(Mainframe.class.getResource("/images/维护.png")));
- mnNewMenu_3.add(mntmNewMenuItem_3);
-
- JMenuItem mntmNewMenuItem_4 = new JMenuItem("退出");
- mntmNewMenuItem_4.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- int result=JOptionPane.showConfirmDialog(null,"是否退出");
- if(result==0) {
- dispose();
- }
- }
- });
- mntmNewMenuItem_4.setIcon(new ImageIcon(Mainframe.class.getResource("/images/退出.png")));
- mnNewMenu.add(mntmNewMenuItem_4);
-
- JMenu mnNewMenu_1 = new JMenu("关于我们");
- mnNewMenu_1.setIcon(new ImageIcon(Mainframe.class.getResource("/images/关于我们.png")));
- menuBar.add(mnNewMenu_1);
-
- JMenuItem mntmNewMenuItem = new JMenuItem("关于Java");
- mntmNewMenuItem.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- Java666interframe java666interframe=new Java666interframe();
- java666interframe.setVisible(true);
- table.add(java666interframe);
- }
- });
- mntmNewMenuItem.setIcon(new ImageIcon(Mainframe.class.getResource("/images/关于我们.png")));
- mnNewMenu_1.add(mntmNewMenuItem);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
-
- setContentPane(contentPane);
- contentPane.setLayout(new BorderLayout(0, 0));
- table = new JDesktopPane();
- table.setBackground(new Color(255, 255, 255));
- contentPane.add(table, BorderLayout.CENTER);
-
- //设置最大化
- this.setExtendedState(JFrame.MAXIMIZED_BOTH);
- }
- }
好的代码到这里就结束了,结尾我会给出整个包的连接,里面有图片等等。
这边启动是在Login界面开始启动的喔。。。。
没有做过多的可视化啊,有兴趣的小伙伴可以自己修改(期末考试太多了要复习没空做了)
这边想看我啰嗦的小伙伴可以看看,不想看的就跳。
1.这个实战项目实现了一个小小的前后端分离的操作,虽然很基础也很简单,但是主要是让我学会了怎么进行这种类似项目的开发,以及为日后的毕设做准备,如果说大家想看详细的教程,我是仿照b站上一个视频写的,这边主要是放源码并进行了一些修改。
java +swing(gui) +mysql 实现的图书管理系统_哔哩哔哩_bilibili
up主讲的有些快,希望大家尽力跟上就好了。
2.这边实际上我是不建议大家直接cv的,当然要是时间紧迫嘛也没什么。大家做这个课程设计虽然很大的目的是为了合格,但是我还是希望大家能够手动写一遍,以提升技术为目的,每次做完一个小项目都会让你受益良多。
3.这个开发的过程中呢,就算你是跟着视频写的,也很可能会写漏或者是写错什么变量。我这个代码里面就写错了一个变量但是不影响使用,有哪位细心的小伙伴可以指出来并在评论区留言。主要是因为我的这个eclipse自动填充变量名导致的,因此大家在开发项目之前对自己的编译器进行一定程度上的调节也是十分重要的,一切以自己用的舒服为先。
有什么问题也可以私聊或者直接@我都可以我会尽力帮大家解决。
代码文件包:
链接: https://pan.baidu.com/s/1lulyGlbA50O5e82Kfdhkzw?pwd=ersp 提取码: ersp 复制这段内容后打开百度网盘手机App,操作更方便哦
驱动文件包(代码里面应该有了,保险起见还是再发一份):
链接: https://pan.baidu.com/s/1KQMqJrSeLRDXryO72agOWw?pwd=dhte 提取码: dhte 复制这段内容后打开百度网盘手机App,操作更方便哦
最后祝大家都能够度过一个完美的暑假!!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。