代码加文字 10万 字左右 !!! 此图书馆里系统我花了大概一周的时间写出 本博客教程熬夜肝了大概三天三夜!!! 为了我的所有粉丝们都能从零基础上手,因此教程十分详细! 非常适合零基础新手小白。 而且我还配备了全部源码! 感谢一路上支持和不帮助我的人! 共勉~~~
零、项目效果预览及设计准备
项目效果预览
登录页面  图书管理系统主界面 
关于我们  基本数据维护  图书类别添加  图书类别维护 
图书添加  图书维护  安全退出 
数据库架构设计
SQL源码
;
;
;
;
;
;
CREATE DATABASE `db_book` ;
USE `db_book`;
DROP TABLE IF EXISTS `t_book`;
CREATE TABLE `t_book` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`bookName` VARCHAR(20) DEFAULT NULL,
`author` VARCHAR(20) DEFAULT NULL,
`sex` VARCHAR(10) DEFAULT NULL,
`price` float DEFAULT NULL,
`bookDesc` varchar(1000) DEFAULT NULL,
`bookTypeId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_t_book` (`bookTypeId`),
CONSTRAINT `FK_t_book` FOREIGN KEY (`bookTypeId`) REFERENCES `t_booktype` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
insert into `t_book`(`id`,`bookName`,`author`,`sex`,`price`,`bookDesc`,`bookTypeId`) values (1,'数据结构','王凯','男',80,'算法书籍',2),(2,'Java编程思想1','JJ1','女',111,'这个一本好书籍1',1),(3,'美女的生活1','呵呵2','女',121,'11112',2),(4,'java代码','风格','女',120,'是',1),(5,'Java','胡超','男',10,'好书',1),(6,'java编程思想','美女','女',100,'搜索',2),(7,'数据库','胡超','男',100,'好书',1),(8,'算法设计','普通人','男',60,'算法书',1),(9,'算法','许昌人','男',45,'废人的书',1),(10,'操作系统','超哥','男',60,'超级牛逼书',1),(11,'计算机网络','超超','男',80,'牛逼牛逼',1),(12,'计算机网络','毛毛','男',90,' 超哥厉害',2),(13,'python','凤凤','女',70,'2好书',2),(14,'算法之道','圆圆','女',9999,'ACM铜牌大佬编写!!!',17);
DROP TABLE IF EXISTS `t_booktype`;
CREATE TABLE `t_booktype` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bookTypeName` varchar(20) DEFAULT NULL,
`bookTypeDesc` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
insert into `t_booktype`(`id`,`bookTypeName`,`bookTypeDesc`) values (1,'计算机类2','计算机类书籍3'),(2,'计算机','操作系统'),(15,'计算机 ','数据结构'),(17,'算法类','算法很重要!!');
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
insert into `t_user`(`id`,`userName`,`password`) values (1,'huchao','10428376');
;
;
;
;
表 架构设计
 
项目目录

项目工具类代码配置
DbUtil配置
package com.java.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DbUtil {
private String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=utf8&&useSSL=false&serverTimezone = Asia/Shanghai ";
private String duUsername="root";
private String dbPassword="10428376";
private String jdbcName="com.mysql.jdbc.Driver";
public Connection getCon() throws Exception {
Class.forName(jdbcName);
Connection con= DriverManager.getConnection(dbUrl, duUsername, 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) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
}
}
StringUtil配置
package com.java.util;
public class StringUtil {
public static boolean isEmpty (String str) {
if (str==null || "".equals(str.trim())) {
return true;
}else {
return false;
}
}
public static boolean isnotEmpty(String str) {
if (str!=null||!"".equals(str.trim())) {
return true;
} else {
return false;
}
}
}
所需工具
swing插件之可视化开发工具windowbuilder swing插件之可视化开发工具windowbuilder
Eclipse GUI插件WindowBuilder安装 Eclipse GUI插件WindowBuilder安装
Eclipse  JDK jdk1.8.0_221
SQLyog  jar包
mysql-connector-java-5.1.7-bin.jar
ER图

一、登录功能实现
管理员登录功能实现
小图标下载网站推荐:http://www.easyicon.net/ Java 开发统一环境搭建:http://www.java1234.com/a/kaiyuan/java/2013/0419/228.html
类的备注快捷方式

      
Font font = new Font("Dialog", Font.PLAIN, 12);
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, font);
}
}
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
resetValueActionPerformed(arg0);
}
});
private void resetValueActionPerformed(ActionEvent evt) {
this.userNameTxt.setText("");
this.passwordTxt.setText("");
}


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loginActionPerformed(arg0);
}
}
private void loginActionPerformed(ActionEvent ev) {
String username = this.userNameTxt.getText();
String password =new String(this.passwordTxt.getPassword());
if (StringUtil.isEmpty(username)) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
}
if (StringUtil.isnotEmpty(password)) {
JOptionPane.showMessageDialog(null,"密码不能为空!");
}
}
package com.java1234.util;
public class StringUtil {
public static boolean isEmpty (String str) {
if (str==null || "".equals(str.trim())) {
return true;
}else {
return false;
}
}
public static boolean isnotEmpty(String str) {
if (str!=null||!"".equals(str.trim())) {
return true;
} else {
return false;
}
}
}
数据库连接

public User() {
super();
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
LogOnFrm.java
private void loginActionPerformed(ActionEvent ev) {
String username = this.userNameTxt.getText();
String password =new String(this.passwordTxt.getPassword());
if (StringUtil.isEmpty(username)) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
}
if (StringUtil.isEmpty(password)) {
JOptionPane.showMessageDialog(null,"密码不能为空!");
}
User user = new User(username,password);
Connection con =null;
try {
con=dbUtil.getCon();
User currentuser =userDao.login(con, user);
if (currentuser!=null) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showConfirmDialog(null, "用户名或密码错误!");
}
} catch (Exception e) {
}
}
效果预览
 成功登录
User(用户实体类)源码
package com.java.model;
public class User {
public User() {
super();
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserDao(用户Dao类)源码
package com.java.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import com.java.model.User;
import java.sql.PreparedStatement;
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.getUsername());
pstmt.setString(2, user.getPassword());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
resultUser=new User();
resultUser.setId(rs.getInt("id"));
resultUser.setUsername(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
}
LogOnFrm源码
package com.java.view;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import com.java.dao.UserDao;
import com.java.model.User;
import com.java.util.DbUtil;
import com.java.util.StringUtil;
public class LogOnFrm extends JFrame {
private JPanel contentPane;
private JTextField userNameTxt;
private JPasswordField passwordTxt;
private DbUtil dbUtil=new DbUtil();
private UserDao userDao=new UserDao();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LogOnFrm frame = new LogOnFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LogOnFrm() {
Font font = new Font("Dialog", Font.PLAIN, 12);
java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, font);
}
}
setTitle("\u7BA1\u7406\u5458\u767B\u5F55");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF");
lblNewLabel.setFont(new Font("宋体", Font.BOLD, 23));
lblNewLabel.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/logo.png")));
JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D\uFF1A");
lblNewLabel_1.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/userName.png")));
JLabel lblNewLabel_2 = new JLabel("\u5BC6 \u7801\uFF1A");
lblNewLabel_2.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/password.png")));
userNameTxt = new JTextField();
userNameTxt.setColumns(10);
passwordTxt = new JPasswordField();
JButton btnNewButton = new JButton("\u767B\u5F55");
btnNewButton.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/login.png")));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loginActionPerformed(arg0);
}
});
JButton btnNewButton_1 = new JButton("\u91CD\u7F6E");
btnNewButton_1.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/reset.png")));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
resetValueActionPerformed(arg0);
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(54)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(17)
.addComponent(lblNewLabel_1)
.addGap(28)
.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(18)
.addComponent(lblNewLabel_2)
.addGap(33)
.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(100, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(74)
.addComponent(btnNewButton)
.addPreferredGap(ComponentPlacement.RELATED, 117, Short.MAX_VALUE)
.addComponent(btnNewButton_1)
.addGap(83))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(103)
.addComponent(lblNewLabel)
.addContainerGap(109, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(20)
.addComponent(lblNewLabel)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(21)
.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(22)
.addComponent(lblNewLabel_1)))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(30)
.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(32)
.addComponent(lblNewLabel_2)))
.addPreferredGap(ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addComponent(btnNewButton)
.addGap(18))
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addComponent(btnNewButton_1)
.addGap(17))))
);
contentPane.setLayout(gl_contentPane);
this.setLocationRelativeTo(null);
}
private void loginActionPerformed(ActionEvent ev) {
String username = this.userNameTxt.getText();
String password =new String(this.passwordTxt.getPassword());
if (StringUtil.isEmpty(username)) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
}
if (StringUtil.isEmpty(password)) {
JOptionPane.showMessageDialog(null,"密码不能为空!");
}
User user = new User(username,password);
Connection con =null;
try {
con=dbUtil.getCon();
User currentuser =userDao.login(con, user);
if (currentuser!=null) {
JOptionPane.showMessageDialog(null, "登录成功!");
dispose();
new MainFrm().setVisible(true);
} else {
JOptionPane.showConfirmDialog(null, "用户名或密码错误!");
}
} catch (Exception e) {
}finally {
try {
dbUtil.closeCon(con);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
private void resetValueActionPerformed(ActionEvent evt) {
this.userNameTxt.setText("");
this.passwordTxt.setText("");
}
}
二、系统主界面实现
JFrame 居中实现
系统主界面实现
内部窗体实现
this.setLocationRelativeTo(null);
F6
继续下一步
F8
执行到下一个断点
假如没有的话,执行完;

使用Menu下的JMenuBar、JMenu、JmenuItem
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setLocationRelativeTo(null);
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result= JOptionPane.showConfirmDialog(null, "是否退出系统");
if (result==0) {
dispose();
}
}
}

private JDesktopPane table = null;
mntmAuthorhuchao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JavaInterFrm javaInterFrm = new JavaInterFrm();
javaInterFrm.setVisible(true);
table.add(javaInterFrm);
}
}
MainFrm源码
package com.java.view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class MainFrm extends JFrame {
private JPanel contentPane;
private JDesktopPane table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrm frame = new MainFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrm() {
setTitle("\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF\u4E3B\u754C\u9762");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("\u57FA\u672C\u6570\u636E\u7EF4\u62A4");
menu.setIcon(new ImageIcon(MainFrm.class.getResource("/images/base.png")));
menuBar.add(menu);
JMenu menu_1 = new JMenu("\u56FE\u4E66\u7C7B\u522B\u7BA1\u7406");
menu_1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/bookTypeManager.png")));
menu.add(menu_1);
JMenuItem menuItem = new JMenuItem("\u56FE\u4E66\u7C7B\u522B\u6DFB\u52A0");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookTypeAddInterFrm bookTypeAddInterFrm = new BookTypeAddInterFrm();
bookTypeAddInterFrm.setVisible(true);
}
});
menuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/images/add.png")));
menu_1.add(menuItem);
JMenuItem mntmNewMenuItem = new JMenuItem("\u56FE\u4E66\u7C7B\u522B\u7EF4\u62A4");
mntmNewMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
BookTypeManageInterFrm bookTypeManageInterFrm = new BookTypeManageInterFrm();
bookTypeManageInterFrm.setVisible(true);
}
});
mntmNewMenuItem.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
menu_1.add(mntmNewMenuItem);
JMenu menu_2 = new JMenu("\u56FE\u4E66\u7BA1\u7406");
menu_2.setIcon(new ImageIcon(MainFrm.class.getResource("/images/bookManager.png")));
menu.add(menu_2);
JMenuItem menuItem_3 = new JMenuItem("\u56FE\u4E66\u6DFB\u52A0");
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
BookAddInterFrm bookAddInterFrm = new BookAddInterFrm();
bookAddInterFrm.setVisible(true);
}
});
menuItem_3.setIcon(new ImageIcon(MainFrm.class.getResource("/images/add.png")));
menu_2.add(menuItem_3);
JMenuItem menuItem_4 = new JMenuItem("\u56FE\u4E66\u7EF4\u62A4");
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
BookManageInterfrm bookManageInterFrm = new BookManageInterfrm();
bookManageInterFrm.setVisible(true);
}
});
menuItem_4.setIcon(new ImageIcon(MainFrm.class.getResource("/images/edit.png")));
menu_2.add(menuItem_4);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u5206\u9875\u67E5\u8BE2");
mntmNewMenuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookPageSearchFrm bookPageSearchFrm = new BookPageSearchFrm();
bookPageSearchFrm.setVisible(true);
}
});
mntmNewMenuItem_2.setIcon(new ImageIcon(MainFrm.class.getResource("/images/search.png")));
menu_2.add(mntmNewMenuItem_2);
JMenuItem menuItem_1 = new JMenuItem("\u5B89\u5168\u9000\u51FA");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int result= JOptionPane.showConfirmDialog(null, "是否退出系统");
if (result==0) {
dispose();
JOptionPane.showMessageDialog(null, "退出成功!");
}
}
});
menuItem_1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/exit.png")));
menu.add(menuItem_1);
JMenu menu_3 = new JMenu("\u5173\u4E8E\u6211\u4EEC");
menu_3.setIcon(new ImageIcon(MainFrm.class.getResource("/images/about.png")));
menuBar.add(menu_3);
JMenuItem mntmAuthorhuchao = new JMenuItem("Author\uFF1A@HuChao");
mntmAuthorhuchao.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JavaInterFrm javaInterFrm = new JavaInterFrm();
javaInterFrm.setVisible(true);
}
});
mntmAuthorhuchao.setIcon(new ImageIcon(MainFrm.class.getResource("/images/\u80E1\u8D85.png")));
menu_3.add(mntmAuthorhuchao);
JMenu mnNewMenu = new JMenu("\u70B9\u51FB\u5173\u6CE8");
menu_3.add(mnNewMenu);
JMenu mnNewMenu_1 = new JMenu("CSDN");
mnNewMenu.add(mnNewMenu_1);
JMenuItem mntmNewMenuItem_1 = new JMenuItem(" ");
mntmNewMenuItem_1.setIcon(new ImageIcon(MainFrm.class.getResource("/images/csdn.png")));
mnNewMenu_1.add(mntmNewMenuItem_1);
JMenu mnNewMenu_2 = new JMenu("\u7B80\u4E66");
mnNewMenu.add(mnNewMenu_2);
JMenuItem menuItem_2 = new JMenuItem("");
menuItem_2.setIcon(new ImageIcon(MainFrm.class.getResource("/images/\u7B80\u4E66.png")));
mnNewMenu_2.add(menuItem_2);
JMenu mnNewMenu_3 = new JMenu("bilibili");
mnNewMenu.add(mnNewMenu_3);
JMenuItem menuItem_5 = new JMenuItem("");
menuItem_5.setIcon(new ImageIcon(MainFrm.class.getResource("/images/bili.png")));
mnNewMenu_3.add(menuItem_5);
JMenu menu_4 = new JMenu("\u6296\u97F3");
mnNewMenu.add(menu_4);
JMenuItem menuItem_6 = new JMenuItem(" ");
menuItem_6.setIcon(new ImageIcon(MainFrm.class.getResource("/images/douyin.png")));
menu_4.add(menuItem_6);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JDesktopPane table = new JDesktopPane();
table.setBackground(Color.GRAY);
contentPane.add(table, BorderLayout.CENTER);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
JavaInterFrm源码
package com.java.view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class JavaInterFrm extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JavaInterFrm frame = new JavaInterFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JavaInterFrm() {
setTitle("Author\uFF1A@\u80E1\u8D85_\u4E09\u6708");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 557, 705);
contentPane = new JPanel();
contentPane.setForeground(Color.RED);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setBackground(Color.RED);
contentPane.add(desktopPane);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon(JavaInterFrm.class.getResource("/images/P(D$LPR)%%EWHGCMW8UE70B.png")));
GroupLayout gl_desktopPane = new GroupLayout(desktopPane);
gl_desktopPane.setHorizontalGroup(
gl_desktopPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_desktopPane.createSequentialGroup()
.addContainerGap(123, Short.MAX_VALUE)
.addComponent(lblNewLabel)
.addGap(90))
);
gl_desktopPane.setVerticalGroup(
gl_desktopPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_desktopPane.createSequentialGroup()
.addContainerGap(71, Short.MAX_VALUE)
.addComponent(lblNewLabel)
.addGap(65))
);
desktopPane.setLayout(gl_desktopPane);
}
}
三、图书类别添加功能
图书类别添加功能实现
bookTypeDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
设置文本域边框
Debug 详解
F5 进入方法内部
Debug 过程中 ,能够动态修改变量的值
Ctrl+shift+i 查看表达式结果
package com.java1234.model;
public class BookType {
private int id;
private String bookTypeName;
private String 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;
}
}
package com.java1234.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import com.java1234.model.BookType;
public class BookTypeDao {
public int add(Connection con,BookType bookType)throws Exception {
String sql ="insert into t_booktype values(null,?,?)";
PreparedStatement pstmt= con.prepareStatement(sql);
pstmt.setString(1, bookType.getBookTypeName());
pstmt.setString(2, bookType.getBookTypeDesc());
return pstmt.executeUpdate();
}
}
bookTypeDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
private JTextField bookTypeNameTxt;
private JTextArea bookTypeDescTxt;
private void resetValueActionPerformed(ActionEvent evt) {
this.resetValue();
}
private void resetValue() {
this.bookTypeNameTxt.setText("");
this.bookTypeDescTxt.setText("");
}
bookTypeDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
图书类别添加
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookTypeAddActionPerformed(e);
}
}
private void bookTypeAddActionPerformed(ActionEvent e) {
String bookTypeName = this.bookTypeNameTxt.getText();
String bookTypeDesc = this.bookTypeDescTxt.getText();
if (StringUtil.isEmpty(bookTypeName)) {
JOptionPane.showMessageDialog(null, "图书类别不能为空!");
return;
}
BookType bookType = new BookType(bookTypeName,bookTypeDesc);
Connection con=null;
try {
con = dbutil.getCon();
int n = bookTypeDao.add(con, bookType);
if (n==1) {
JOptionPane.showMessageDialog(null, "图书类别添加成功!");
resetValue();
}else {
JOptionPane.showMessageDialog(null, "图书类别添加失败!");
}
} catch (Exception e2) {
e2.printStackTrace();
JOptionPane.showMessageDialog(null, "图书类别添加失败!");
}finally {
try {
dbutil.closeCon(con);
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
效果图 
BookTypeAddInterFrm(图书类别添加窗体)源码
package com.java.view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import com.java.dao.BookTypeDao;
import com.java.model.BookType;
import com.java.util.DbUtil;
import com.java.util.StringUtil;
public class BookTypeAddInterFrm extends JFrame {
private JPanel contentPane;
private JTextField bookTypeNameTxt;
private JTextField bookTypeDescTxt;
private DbUtil dbutil = new DbUtil();
private BookTypeDao bookTypeDao = new BookTypeDao();
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();
}
}
});
}
public BookTypeAddInterFrm() {
setTitle("\u56FE\u4E66\u7C7B\u522B\u6DFB\u52A0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u7C7B\u522B\u540D\u79F0");
JLabel lblNewLabel_1 = new JLabel("\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0");
bookTypeNameTxt = new JTextField();
bookTypeNameTxt.setColumns(10);
JButton btnNewButton = new JButton("\u6DFB\u52A0");
btnNewButton.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/add.png")));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookTypeAddActionPerformed(e);
}
});
JButton btnNewButton_1 = new JButton("\u91CD\u7F6E");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetValueActionPerformed(e);
}
});
btnNewButton_1.setIcon(new ImageIcon(BookTypeAddInterFrm.class.getResource("/images/reset.png")));
bookTypeDescTxt = new JTextField();
bookTypeDescTxt.setColumns(10);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(56)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(btnNewButton)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel)
.addComponent(lblNewLabel_1)))
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnNewButton_1))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(27)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(bookTypeDescTxt)
.addComponent(bookTypeNameTxt, GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE))))
.addGap(68))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(40)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(bookTypeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(35)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_1)
.addComponent(bookTypeDescTxt, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton)
.addComponent(btnNewButton_1))
.addGap(18))
);
contentPane.setLayout(gl_contentPane);
this.setLocationRelativeTo(null);
bookTypeDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
}
private void bookTypeAddActionPerformed(ActionEvent e) {
String bookTypeName = this.bookTypeNameTxt.getText();
String bookTypeDesc = this.bookTypeDescTxt.getText();
if (StringUtil.isEmpty(bookTypeName)) {
JOptionPane.showMessageDialog(null, "图书类别不能为空!");
return;
}
BookType bookType = new BookType(bookTypeName,bookTypeDesc);
Connection con=null;
try {
con = dbutil.getCon();
int n = bookTypeDao.add(con, bookType);
if (n==1) {
JOptionPane.showMessageDialog(null, "图书类别添加成功!");
resetValue();
}else {
JOptionPane.showMessageDialog(null, "图书类别添加失败!");
}
} catch (Exception e2) {
e2.printStackTrace();
JOptionPane.showMessageDialog(null, "图书类别添加失败!");
}finally {
try {
dbutil.closeCon(con);
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
private void resetValueActionPerformed(ActionEvent evt) {
this.resetValue();
}
private void resetValue() {
this.bookTypeNameTxt.setText("");
this.bookTypeDescTxt.setText("");
}
}
四、图书类别查询功能

class BookTypeDao
public ResultSet list(Connection con,BookType bookType)throws Exception {
StringBuffer sb = new StringBuffer("select * from t_bookType");
if (bookType.getBookTypeName()!=null) {
sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
return pstmt.executeQuery();
}
private JTable bookTypetable;
private DbUtil dbutil = new DbUtil();
private BookTypeDao bookTypeDao = new BookTypeDao();
private JTextField s_bookTypeNameTxt;
private void bookTypeSearchActionPerformed(ActionEvent evt) {
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);
Connection con=null;
try {
con=dbutil.getCon();
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("bookTypeDesc"));
dtm.addRow(v);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
dbutil.closeCon(con);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
五、图书类别修改删除功能
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();
}
public int updata(Connection con,BookType bookType) throws Exception {
String sql = "update t_bookType where bookTypeName=?,bookTypeDesc=? 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();
}
Class BookTypeManageInterFrm
private JTextField idTxt;
private JTextField bookTypeNameTxt;
private JTextArea bookTypeDescTxt;
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookTypeSearchActionPerformed(e);
}
}
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bookTypeDeleteActionEvent(arg0);
}
}
private void bookTypeDeleteActionEvent(ActionEvent arg0) {
String id = idTxt.getText();
if (StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择要修改的纪录");
return;
}
int n = JOptionPane.showConfirmDialog(null,"确定要删除的记录");
if (n==0) {
Connection con = null;
try {
con = dbutil.getCon();
int deleteNum = bookTypeDao.delete(con, id);
if (deleteNum==1) {
JOptionPane.showMessageDialog(null, "删除成功");
this.resetValue();
this.fillTable(new BookType());
}else {
JOptionPane.showMessageDialog(null, "删除失败");
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败");
}finally {
try {
dbutil.closeCon(con);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
private void bookTypeUpdateActionEvent(ActionEvent arg0) {
String id = idTxt.getText();
String bookTypeName = bookTypeNameTxt.getText();
String bookTypeDesc = bookTypeDescTxt.getText();
if (StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "请选择要修改的纪录");
return;
}
if (StringUtil.isEmpty(bookTypeName)) {
JOptionPane.showMessageDialog(null, "图书类别不能为空");
}
BookType bookType = new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);
Connection con = null;
try {
con=dbutil.getCon();
int modifyNum = bookTypeDao.updata(con, bookType);
if (modifyNum==1) {
JOptionPane.showMessageDialog(null, "修改成功");
this.resetValue();
this.fillTable(new BookType());
}else {
JOptionPane.showMessageDialog(null, "修改失败");
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "修改失败");
}finally {
try {
dbutil.closeCon(con);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
private void bookTypeTableMousePressed(MouseEvent evt) {
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));
}
public void resetValue() {
this.idTxt.setText("");
this.bookTypeNameTxt.setText("");
this.bookTypeDescTxt.setText("");
}
效果图 
BookType(图书类别实体)源码
package com.java.model;
public class BookType {
private int id;
private String bookTypeName;
private String bookTypeDesc;
public BookType() {
super();
}
public BookType(String bookTypeName, String bookTypeDesc) {
super();
this.bookTypeName = bookTypeName;
this.bookTypeDesc = bookTypeDesc;
}
public BookType(int id, String bookTypeName, String bookTypeDesc) {
super();
this.id = id;
this.bookTypeName = bookTypeName;
this.bookTypeDesc = bookTypeDesc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookTypeName() {
return bookTypeName;
}
public void setBookTypeName(String bookTypeName) {
this.bookTypeName = bookTypeName;
}
public String getBookTypeDesc() {
return bookTypeDesc;
}
public void setBookTypeDesc(String bookTypeDesc) {
this.bookTypeDesc = bookTypeDesc;
}
@Override
public String toString() {
return bookTypeName;
}
}
BookTypeDao(图书类别Dao)源码
package com.java.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.java.model.Book_B;
import com.java.model.BookType;
import com.java.util.StringUtil;
public class BookTypeDao {
public int add(Connection con,BookType bookType)throws Exception {
String sql="insert into t_bookType values(null,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, bookType.getBookTypeName());
pstmt.setString(2, bookType.getBookTypeDesc());
return pstmt.executeUpdate();
}
public ResultSet list(Connection con,BookType bookType)throws Exception {
StringBuffer sb = new StringBuffer("select * from t_booktype");
if (bookType.getBookTypeName()!=null) {
sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
}
PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
return pstmt.executeQuery();
}
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();
}
public int updata(Connection con,BookType bookType) throws Exception {
String sql = "update t_bookType set bookTypeName=?,bookTypeDesc=? 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();
}
}
BookTypeManageInterFrm(图书类别管理窗口)源码
package com.java.view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.Book;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import com.java.dao.BookDao;
import com.java.dao.BookTypeDao;
import com.java.model.BookType;
import com.java.model.Book_B;
import com.java.util.DbUtil;
import com.java.util.StringUtil;
public class BookManageInterfrm extends JFrame {
private JPanel contentPane;
private JTable bookTable;
private JTextField s_bookNameTxt;
private JTextField s_authorTxt;
private BookDao bookDao = new BookDao();
private JComboBox s_bookTypeJcb;
private DbUtil dbutil = new DbUtil();
private BookTypeDao bookTypeDao = new BookTypeDao();
private JTextField idTxt;
private JTextField bookNameTxt;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField priceTxt;
private JTextField authorTxt;
private JRadioButton manJrb;
private JRadioButton femaleJrb;
private JTextArea bookDescTxt;
private JComboBox bookTypeJcb;
private static int page=1;
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();
}
}
});
}
public BookManageInterfrm() {
setTitle("\u56FE\u4E66\u7BA1\u7406");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 876, 737);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
JButton btnNewButton_1 = new JButton("\u4E0A\u4E00\u9875");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
prevActionPerfromed(arg0);
}
});
JButton btnNewButton_2 = new JButton("\u4E0B\u4E00\u9875");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nextActionPerfromed(e);
}
});
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
JButton btnNewButton_3 = new JButton("\u4FEE\u6539");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bookUpdateActionPerformed(arg0);
}
});
btnNewButton_3.setIcon(new ImageIcon(BookManageInterfrm.class.getResource("/images/modify.png")));
JButton btnNewButton_4 = new JButton("\u5220\u9664");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bookDeleteActionPerformed(arg0);
}
});
btnNewButton_4.setIcon(new ImageIcon(BookManageInterfrm.class.getResource("/images/delete.png")));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 755, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(56)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE))))
.addGap(41))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(120)
.addComponent(btnNewButton_1)
.addPreferredGap(ComponentPlacement.RELATED, 380, Short.MAX_VALUE)
.addComponent(btnNewButton_2)
.addGap(212))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(100)
.addComponent(btnNewButton_3)
.addPreferredGap(ComponentPlacement.RELATED, 402, Short.MAX_VALUE)
.addComponent(btnNewButton_4)
.addGap(194))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
.addGap(10)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_1)
.addComponent(btnNewButton_2))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_3)
.addComponent(btnNewButton_4))
.addContainerGap())
);
JLabel lblNewLabel_3 = new JLabel("\u7F16\u53F7\uFF1A");
idTxt = new JTextField();
idTxt.setColumns(10);
bookNameTxt = new JTextField();
bookNameTxt.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
JLabel lblNewLabel_5 = new JLabel("\u4F5C\u8005\u6027\u522B\uFF1A");
manJrb = new JRadioButton("\u7537");
buttonGroup.add(manJrb);
manJrb.setSelected(true);
femaleJrb = new JRadioButton("\u5973");
buttonGroup.add(femaleJrb);
JLabel lblNewLabel_6 = new JLabel("\u4EF7\u683C\uFF1A");
priceTxt = new JTextField();
priceTxt.setColumns(10);
JLabel lblNewLabel_7 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
authorTxt = new JTextField();
authorTxt.setColumns(10);
JLabel lblNewLabel_8 = new JLabel("\u56FE\u4E66\u7C7B\u522B;");
JLabel lblNewLabel_9 = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
bookDescTxt = new JTextArea();
bookTypeJcb = new JComboBox();
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(17)
.addComponent(lblNewLabel_3)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
.addGap(33)
.addComponent(lblNewLabel_4)
.addGap(5)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
.addGap(43)
.addComponent(lblNewLabel_5)
.addGap(6)
.addComponent(manJrb)
.addGap(12)
.addComponent(femaleJrb))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(19)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(lblNewLabel_9)
.addGap(2)
.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 619, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(lblNewLabel_6)
.addGap(7)
.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
.addGap(32)
.addComponent(lblNewLabel_7)
.addGap(5)
.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, 173, GroupLayout.PREFERRED_SIZE)
.addGap(45)
.addComponent(lblNewLabel_8)
.addGap(11)
.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(53, Short.MAX_VALUE))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(21)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_3)))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(25)
.addComponent(lblNewLabel_4))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(22)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(23)
.addComponent(manJrb))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(25)
.addComponent(lblNewLabel_5))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(23)
.addComponent(femaleJrb)))
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(29)
.addComponent(lblNewLabel_6))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(30)
.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(34)
.addComponent(lblNewLabel_7))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(30)
.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(33)
.addComponent(lblNewLabel_8)))
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(37)
.addComponent(lblNewLabel_9))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(34)
.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE))))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(78)
.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(20, Short.MAX_VALUE))
);
panel_1.setLayout(gl_panel_1);
JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
s_bookNameTxt = new JTextField();
s_bookNameTxt.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
s_authorTxt = new JTextField();
s_authorTxt.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bookSearchActionPerformed(arg0);
}
});
btnNewButton.setIcon(new ImageIcon(BookManageInterfrm.class.getResource("/images/search.png")));
s_bookTypeJcb = new JComboBox();
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, 125, GroupLayout.PREFERRED_SIZE)
.addGap(22)
.addComponent(lblNewLabel_1)
.addGap(14)
.addComponent(s_authorTxt, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
.addGap(25)
.addComponent(lblNewLabel_2)
.addGap(14)
.addComponent(s_bookTypeJcb, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
.addComponent(btnNewButton)
.addGap(26))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(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)))
.addGroup(gl_panel.createSequentialGroup()
.addGap(14)
.addComponent(lblNewLabel_1))
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(s_authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel.createSequentialGroup()
.addGap(11)
.addComponent(lblNewLabel_2))
.addGroup(gl_panel.createSequentialGroup()
.addGap(9)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(s_bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btnNewButton))))
.addContainerGap(18, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
bookTable = new JTable();
bookTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
bookTableMousePressed(arg0);
}
});
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", "\u56FE\u4E66\u7C7B\u522BID"
}
) {
boolean[] columnEditables = new boolean[] {
false, false, false, false, false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
scrollPane.setViewportView(bookTable);
contentPane.setLayout(gl_contentPane);
this.setLocationRelativeTo(null);
bookDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
this.fillBookType("search");
this.fillBookType("modify");
this.fillTable(new Book_B());
this.fillTable1(new Book_B());
}
private void prevActionPerfromed(ActionEvent e) {
if(page==1) {
return;
}
page--;
this.fillTable1(new Book_B());
}
private void nextActionPerfromed(ActionEvent ent) {
page++;
this.fillTable1(new Book_B());
}
private void bookDeleteActionPerformed(ActionEvent evt) {
String id=idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null, "请选择要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗?");
if(n==0){
Connection con=null;
try{
con=dbutil.getCon();
boolean flag = bookDao.existBookByBookTypeId(con, id);
if (flag) {
JOptionPane.showMessageDialog(null, "当前图书类别下有图书,不能删除此类别");
return;
}
int deleteNum=bookDao.delete(con, id);
if(deleteNum==1){
JOptionPane.showMessageDialog(null, "删除成功");
this.resetValue();
this.fillTable(new Book_B());
}else{
JOptionPane.showMessageDialog(null, "删除失败");
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败");
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void bookUpdateActionPerformed(ActionEvent evt) {
String id=this.idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(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.showMessageDialog(null, "图书名称不能为空!");
return;
}
if(StringUtil.isEmpty(author)){
JOptionPane.showMessageDialog(null, "图书作者不能为空!");
return;
}
if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null, "图书价格不能为空!");
return;
}
String sex="";
if(manJrb.isSelected()){
sex="男";
}else if(femaleJrb.isSelected()){
sex="女";
}
BookType bookType=(BookType) bookTypeJcb.getSelectedItem();
int bookTypeId=bookType.getId();
Book_B book=new Book_B(Integer.parseInt(id), bookName, author, sex, Float.parseFloat(price), bookTypeId, bookDesc);
Connection con=null;
try{
con=dbutil.getCon();
int addNum=bookDao.update(con, book);
if(addNum==1){
JOptionPane.showMessageDialog(null, "图书修改成功!");
resetValue();
this.fillTable(new Book_B());
}else{
JOptionPane.showMessageDialog(null, "图书修改失败!");
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "图书修改失败!");
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void resetValue(){
this.idTxt.setText("");
this.bookNameTxt.setText("");
this.authorTxt.setText("");
this.priceTxt.setText("");
this.manJrb.setSelected(true);
this.bookDescTxt.setText("");
if(this.bookTypeJcb.getItemCount()>0){
this.bookTypeJcb.setSelectedIndex(0);
}
}
private void bookTableMousePressed(MouseEvent met) {
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("男".equals(sex)){
this.manJrb.setSelected(true);
}else if("女".equals(sex)){
this.femaleJrb.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);
}
}
}
private void bookSearchActionPerformed(ActionEvent arg0) {
String bookName = this.s_bookNameTxt.getText();
String author = this.s_authorTxt.getText();
BookType bookType =(BookType)this.s_bookTypeJcb.getSelectedItem();
int bookTypeId=bookType.getId();
Book_B book = new Book_B(bookName,author,bookTypeId);
this.fillTable(book);
}
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) {
e.printStackTrace();
}finally {
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void fillTable(Book_B book1){
DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=dbutil.getCon();
ResultSet rs=bookDao.list(con,book1);
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"));
v.add(rs.getString("bookTypeId"));
dtm.addRow(v);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void fillTable1(Book_B book){
DefaultTableModel dtm= (DefaultTableModel) bookTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try {
con=dbutil.getCon();
ResultSet rs = bookDao.query(con, book, page);
while(rs.next()) {
Vector v2=new Vector();
v2.add(rs.getString("id"));
v2.add(rs.getString("bookName"));
v2.add(rs.getString("author"));
v2.add(rs.getString("sex"));
v2.add(rs.getFloat("price"));
v2.add(rs.getString("bookDesc"));
v2.add(rs.getString("bookTypeName"));
v2.add(rs.getString("bookTypeId"));
dtm.addRow(v2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
六、图书添加功能
外键关联 
图书类实体
package com.java.model;
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 bookDesc;
public Book() {
super();
}
public Book(String bookName, String author, String sex, Float price, Integer bookTypeId, String bookDesc) {
super();
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookTypeId = bookTypeId;
this.bookDesc = bookDesc;
}
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 getBookDesc() {
return bookDesc;
}
public void setBookDesc(String bookDesc) {
this.bookDesc = bookDesc;
}
}
图书类Dao层
package com.java.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import com.java.model.Book;
public class BookDao {
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.setString(5, book.getBookDesc());
pstmt.setInt(6, book.getBookTypeId());
return pstmt.executeUpdate();
}
}

public class BookAddInterFrm extends JFrame {
private JPanel contentPane;
private JTextField bookNameTxt;
private JTextField authorTxt;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField priceTxt;
private final ButtonGroup buttonGroup_1 = new ButtonGroup();
private JComboBox bookTypeJcb;
private JTextArea bookDescTxt;
private JRadioButton manJrb;
private JRadioButton femaleJrb;
private DbUtil dbUtil = new DbUtil();
private BookTypeDao bookTypeDao = new BookTypeDao();
private BookDao bookDao = new BookDao();
private void resetValueActionPerformed(ActionEvent arg0) {
this.resetValue();
}
private void bookAddActionPerformed(ActionEvent arg0) {
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.showMessageDialog(null, "图书类别名不能为空!");
return;
}
if (StringUtil.isEmpty(author)) {
JOptionPane.showMessageDialog(null, "图书作者不能为空!");
return;
}
if (StringUtil.isEmpty(price)) {
JOptionPane.showMessageDialog(null, "图书价格不能为空!");
return;
}
String sex="";
if (manJrb.isSelected()) {
sex="男";
}else if (femaleJrb.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.showMessageDialog(null, "图书添加成功!");
resetValue();
}else {
JOptionPane.showMessageDialog(null, "图书添加失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
dbUtil.closeCon(con);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
private void resetValue() {
this.bookNameTxt.setText("");
this.authorTxt.setText("");
this.priceTxt.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) {
e.printStackTrace();
}finally {
}
}
}
效果图 
BookAddInterFrm(图书添加窗口)源码
package com.java.view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import com.java.dao.BookDao;
import com.java.dao.BookTypeDao;
import com.java.model.Book_B;
import com.java.model.BookType;
import com.java.util.DbUtil;
import com.java.util.StringUtil;
public class BookAddInterFrm extends JFrame {
private JPanel contentPane;
private JTextField bookNameTxt;
private JTextField authorTxt;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField priceTxt;
private final ButtonGroup buttonGroup_1 = new ButtonGroup();
private JComboBox bookTypeJcb;
private JTextArea bookDescTxt;
private JRadioButton manJrb;
private JRadioButton femaleJrb;
private DbUtil dbUtil = new DbUtil();
private BookTypeDao bookTypeDao = new BookTypeDao();
private BookDao bookDao = new BookDao();
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();
}
}
});
}
public BookAddInterFrm() {
setTitle("\u56FE\u4E66\u6DFB\u52A0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 532, 625);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
bookNameTxt = new JTextField();
bookNameTxt.setColumns(10);
JLabel label_1 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
authorTxt = new JTextField();
authorTxt.setColumns(10);
JLabel label_2 = new JLabel("\u4F5C\u8005\u6027\u522B\uFF1A");
manJrb = new JRadioButton("\u7537");
buttonGroup_1.add(manJrb);
manJrb.setSelected(true);
femaleJrb = new JRadioButton("\u5973");
buttonGroup_1.add(femaleJrb);
JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u4EF7\u683C\uFF1A");
priceTxt = new JTextField();
priceTxt.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
bookTypeJcb = new JComboBox();
JLabel lblNewLabel_2 = new JLabel("\u56FE\u4E66\u63CF\u8FF0\uFF1A");
bookDescTxt = new JTextArea();
JButton button = new JButton("\u6DFB\u52A0");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bookAddActionPerformed(arg0);
}
});
button.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/add.png")));
JButton btnNewButton = new JButton("\u91CD\u7F6E");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
resetValueActionPerformed(arg0);
}
});
btnNewButton.setIcon(new ImageIcon(BookAddInterFrm.class.getResource("/images/reset.png")));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(29)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel_1)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(bookTypeJcb, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label_2)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(manJrb)
.addGap(18)
.addComponent(femaleJrb)))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label_1)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(priceTxt))))
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(btnNewButton)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel_2)
.addGap(8)
.addComponent(bookDescTxt, GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))))
.addGap(75))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(99)
.addComponent(button)
.addContainerGap(330, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(25)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addComponent(label))
.addGap(37)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(manJrb)
.addComponent(femaleJrb)
.addComponent(lblNewLabel)
.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(38)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_1)
.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(39)
.addComponent(lblNewLabel_2))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(45)
.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE)))
.addGap(80)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button)
.addComponent(btnNewButton))
.addContainerGap(82, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
this.setLocationRelativeTo(null);
bookDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
this.fillBookType();
}
private void resetValueActionPerformed(ActionEvent arg0) {
this.resetValue();
}
private void bookAddActionPerformed(ActionEvent arg0) {
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.showMessageDialog(null, "图书类别名不能为空!");
return;
}
if (StringUtil.isEmpty(author)) {
JOptionPane.showMessageDialog(null, "图书作者不能为空!");
return;
}
if (StringUtil.isEmpty(price)) {
JOptionPane.showMessageDialog(null, "图书价格不能为空!");
return;
}
String sex="";
if (manJrb.isSelected()) {
sex="男";
}else if (femaleJrb.isSelected()) {
sex="女";
}
BookType bookType = (BookType) bookTypeJcb.getSelectedItem();
int bookTypeId = bookType.getId();
Book_B book = new Book_B(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.showMessageDialog(null, "图书添加成功!");
resetValue();
}else {
JOptionPane.showMessageDialog(null, "图书添加失败!");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
dbUtil.closeCon(con);
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
private void resetValue() {
this.bookNameTxt.setText("");
this.authorTxt.setText("");
this.priceTxt.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) {
e.printStackTrace();
}finally {
}
}
}
七、图书查询功能

信息查询
BookDao类
class BookDao
public ResultSet list(Connection con,Book_B book1)throws Exception{
StringBuffer sb = new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
if (book1.getBookName()!=null) {
sb.append(" and b.bookName like '%"+book1.getBookName()+"%'");
}
if (book1.getAuthor()!=null) {
sb.append(" and b.author like '%"+book1.getAuthor()+"%'");
}
if (book1.getBookTypeId()!=null&&book1.getBookTypeId()!=-1) {
sb.append(" and b.bookTypeId="+book1.getBookTypeId());
}
PreparedStatement pstmt = con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
private void bookSearchActionPerformed(ActionEvent arg0) {
String bookName = this.s_bookNameTxt.getText();
String author = this.s_authorTxt.getText();
BookType bookType =(BookType)this.s_bookTypeJcb.getSelectedItem();
int bookTypeId=bookType.getId();
Book_B book = new Book_B(bookName,author,bookTypeId);
this.fillTable(book);
}
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)) {
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void fillTable(Book_B book1){
DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=dbutil.getCon();
ResultSet rs=bookDao.list(con,book1);
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){
e.printStackTrace();
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
分页查询
BookDao类
public ResultSet query(Connection con,Book_B book,int page) throws Exception {
StringBuffer sb1=new StringBuffer("SELECT s.id,`bookName`,`author`,`sex`,`price`,`bookDesc`,`bookTypeName`,`bookTypeId`\r\n" +
"FROM t_booktype s\r\n" +
"INNER JOIN t_book r\r\n" +
"ON s.id = r.bookTypeId");
sb1.append(" limit "+(page-1)*10+",10");
System.out.println(sb1);
PreparedStatement ps=con.prepareStatement(sb1.toString());
return ps.executeQuery();
}
BookManageInterFrm类
this.fillTable1(new Book_B());
private void prevActionPerfromed(ActionEvent e) {
if(page==1) {
return;
}
page--;
this.fillTable1(new Book_B());
}
private void nextActionPerfromed(ActionEvent ent) {
page++;
this.fillTable1(new Book_B());
}
初始化表格(分页查询)
private void fillTable1(Book_B book){
DefaultTableModel dtm= (DefaultTableModel) bookTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try {
con=dbutil.getCon();
ResultSet rs = bookDao.query(con, book, page);
while(rs.next()) {
Vector v2=new Vector();
v2.add(rs.getString("id"));
v2.add(rs.getString("bookName"));
v2.add(rs.getString("author"));
v2.add(rs.getString("sex"));
v2.add(rs.getFloat("price"));
v2.add(rs.getString("bookDesc"));
v2.add(rs.getString("bookTypeName"));
v2.add(rs.getString("bookTypeId"));
dtm.addRow(v2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
八、图书修改,删除功能
图书修改功能实现
图书删除功能实现
图书类别删除加判断
BookDao类
class BookDao
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 update(Connection con,Book_B book)throws Exception{
String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? 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.setString(5, book.getBookDesc());
pstmt.setInt(6, book.getBookTypeId());
pstmt.setInt(7, book.getId());
return pstmt.executeUpdate();
}
public boolean existBookByBookTypeId(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();
}
BookManagerInterfrm类
class BookManageInterfrm
private void bookDeleteActionPerformed(ActionEvent evt) {
String id=idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null, "请选择要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗?");
if(n==0){
Connection con=null;
try{
con=dbutil.getCon();
boolean flag = bookDao.existBookByBookTypeId(con, id);
if (flag) {
JOptionPane.showMessageDialog(null, "当前图书类别下有图书,不能删除此类别");
return;
}
int deleteNum=bookDao.delete(con, id);
if(deleteNum==1){
JOptionPane.showMessageDialog(null, "删除成功");
this.resetValue();
this.fillTable(new Book_B());
}else{
JOptionPane.showMessageDialog(null, "删除失败");
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败");
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void bookUpdateActionPerformed(ActionEvent evt) {
String id=this.idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(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.showMessageDialog(null, "图书名称不能为空!");
return;
}
if(StringUtil.isEmpty(author)){
JOptionPane.showMessageDialog(null, "图书作者不能为空!");
return;
}
if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null, "图书价格不能为空!");
return;
}
String sex="";
if(manJrb.isSelected()){
sex="男";
}else if(femaleJrb.isSelected()){
sex="女";
}
BookType bookType=(BookType) bookTypeJcb.getSelectedItem();
int bookTypeId=bookType.getId();
Book_B book=new Book_B(Integer.parseInt(id), bookName, author, sex, Float.parseFloat(price), bookTypeId, bookDesc);
Connection con=null;
try{
con=dbutil.getCon();
int addNum=bookDao.update(con, book);
if(addNum==1){
JOptionPane.showMessageDialog(null, "图书修改成功!");
resetValue();
this.fillTable(new Book_B());
}else{
JOptionPane.showMessageDialog(null, "图书修改失败!");
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "图书修改失败!");
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void resetValue(){
this.idTxt.setText("");
this.bookNameTxt.setText("");
this.authorTxt.setText("");
this.priceTxt.setText("");
this.manJrb.setSelected(true);
this.bookDescTxt.setText("");
if(this.bookTypeJcb.getItemCount()>0){
this.bookTypeJcb.setSelectedIndex(0);
}
}
private void bookTableMousePressed(MouseEvent met) {
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("男".equals(sex)){
this.manJrb.setSelected(true);
}else if("女".equals(sex)){
this.femaleJrb.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);
}
}
}
package com.java.model;
Book_B(图书实体类)源码
public class Book_B {
private int id;
private String bookName;
private String author;
private String sex;
private Float price;
private Integer bookTypeId;
private String bookTypeName;
private String bookDesc;
public Book_B() {
super();
}
public Book_B(String bookName, String author, String sex, Float price, Integer bookTypeId, String bookDesc) {
super();
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookTypeId = bookTypeId;
this.bookDesc = bookDesc;
}
public Book_B(String bookName, String author, Integer bookTypeId) {
super();
this.bookName = bookName;
this.author = author;
this.bookTypeId = bookTypeId;
}
public Book_B(int id, String bookName, String author, String sex, Float price, Integer bookTypeId,
String bookDesc) {
super();
this.id = id;
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookTypeId = bookTypeId;
this.bookDesc = bookDesc;
}
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 getBookDesc() {
return bookDesc;
}
public void setBookDesc(String bookDesc) {
this.bookDesc = bookDesc;
}
}
图书管理界面效果图

BookDao(图书Dao类)源码
package com.java.dao;
import java.awt.print.Book;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.java.model.Book_B;
import com.java.util.DbUtil;
import com.java.util.StringUtil;
public class BookDao {
public int add(Connection con,Book_B book1) throws Exception {
String sql = "insert into t_book values(null,?,?,?,?,?,?)";
PreparedStatement pstmt =con.prepareStatement(sql);
pstmt.setString(1, book1.getBookName());
pstmt.setString(2, book1.getAuthor());
pstmt.setString(3, book1.getSex());
pstmt.setFloat(4, book1.getPrice());
pstmt.setString(5, book1.getBookDesc());
pstmt.setInt(6, book1.getBookTypeId());
return pstmt.executeUpdate();
}
public ResultSet list(Connection con,Book_B book1)throws Exception{
StringBuffer sb = new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
if (book1.getBookName()!=null) {
sb.append(" and b.bookName like '%"+book1.getBookName()+"%'");
}
if (book1.getAuthor()!=null) {
sb.append(" and b.author like '%"+book1.getAuthor()+"%'");
}
if (book1.getBookTypeId()!=null&&book1.getBookTypeId()!=-1) {
sb.append(" and b.bookTypeId="+book1.getBookTypeId());
}
PreparedStatement pstmt = con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
public ResultSet query(Connection con,Book_B book,int page) throws Exception {
StringBuffer sb1=new StringBuffer("SELECT s.id,`bookName`,`author`,`sex`,`price`,`bookDesc`,`bookTypeName`,`bookTypeId`\r\n" +
"FROM t_booktype s\r\n" +
"INNER JOIN t_book r\r\n" +
"ON s.id = r.bookTypeId");
sb1.append(" limit "+(page-1)*10+",10");
System.out.println(sb1);
PreparedStatement ps=con.prepareStatement(sb1.toString());
return ps.executeQuery();
}
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 update(Connection con,Book_B book)throws Exception{
String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? 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.setString(5, book.getBookDesc());
pstmt.setInt(6, book.getBookTypeId());
pstmt.setInt(7, book.getId());
return pstmt.executeUpdate();
}
public boolean existBookByBookTypeId(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();
}
}
Book_B(图书实体类)源码
package com.java.model;
public class Book_B {
private int id;
private String bookName;
private String author;
private String sex;
private Float price;
private Integer bookTypeId;
private String bookTypeName;
private String bookDesc;
public Book_B() {
super();
}
public Book_B(String bookName, String author, String sex, Float price, Integer bookTypeId, String bookDesc) {
super();
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookTypeId = bookTypeId;
this.bookDesc = bookDesc;
}
public Book_B(String bookName, String author, Integer bookTypeId) {
super();
this.bookName = bookName;
this.author = author;
this.bookTypeId = bookTypeId;
}
public Book_B(int id, String bookName, String author, String sex, Float price, Integer bookTypeId,
String bookDesc) {
super();
this.id = id;
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookTypeId = bookTypeId;
this.bookDesc = bookDesc;
}
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 getBookDesc() {
return bookDesc;
}
public void setBookDesc(String bookDesc) {
this.bookDesc = bookDesc;
}
}
BookManageInterfrm(图书管理界面)源码
package com.java.view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.Book;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import com.java.dao.BookDao;
import com.java.dao.BookTypeDao;
import com.java.model.BookType;
import com.java.model.Book_B;
import com.java.util.DbUtil;
import com.java.util.StringUtil;
public class BookManageInterfrm extends JFrame {
private JPanel contentPane;
private JTable bookTable;
private JTextField s_bookNameTxt;
private JTextField s_authorTxt;
private BookDao bookDao = new BookDao();
private JComboBox s_bookTypeJcb;
private DbUtil dbutil = new DbUtil();
private BookTypeDao bookTypeDao = new BookTypeDao();
private JTextField idTxt;
private JTextField bookNameTxt;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JTextField priceTxt;
private JTextField authorTxt;
private JRadioButton manJrb;
private JRadioButton femaleJrb;
private JTextArea bookDescTxt;
private JComboBox bookTypeJcb;
private static int page=1;
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();
}
}
});
}
public BookManageInterfrm() {
setTitle("\u56FE\u4E66\u7BA1\u7406");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 876, 737);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
JButton btnNewButton_1 = new JButton("\u4E0A\u4E00\u9875");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
prevActionPerfromed(arg0);
}
});
JButton btnNewButton_2 = new JButton("\u4E0B\u4E00\u9875");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nextActionPerfromed(e);
}
});
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
JButton btnNewButton_3 = new JButton("\u4FEE\u6539");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bookUpdateActionPerformed(arg0);
}
});
btnNewButton_3.setIcon(new ImageIcon(BookManageInterfrm.class.getResource("/images/modify.png")));
JButton btnNewButton_4 = new JButton("\u5220\u9664");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bookDeleteActionPerformed(arg0);
}
});
btnNewButton_4.setIcon(new ImageIcon(BookManageInterfrm.class.getResource("/images/delete.png")));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 755, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(56)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)
.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE))))
.addGap(41))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(120)
.addComponent(btnNewButton_1)
.addPreferredGap(ComponentPlacement.RELATED, 380, Short.MAX_VALUE)
.addComponent(btnNewButton_2)
.addGap(212))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(100)
.addComponent(btnNewButton_3)
.addPreferredGap(ComponentPlacement.RELATED, 402, Short.MAX_VALUE)
.addComponent(btnNewButton_4)
.addGap(194))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
.addGap(10)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_1)
.addComponent(btnNewButton_2))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_3)
.addComponent(btnNewButton_4))
.addContainerGap())
);
JLabel lblNewLabel_3 = new JLabel("\u7F16\u53F7\uFF1A");
idTxt = new JTextField();
idTxt.setColumns(10);
bookNameTxt = new JTextField();
bookNameTxt.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
JLabel lblNewLabel_5 = new JLabel("\u4F5C\u8005\u6027\u522B\uFF1A");
manJrb = new JRadioButton("\u7537");
buttonGroup.add(manJrb);
manJrb.setSelected(true);
femaleJrb = new JRadioButton("\u5973");
buttonGroup.add(femaleJrb);
JLabel lblNewLabel_6 = new JLabel("\u4EF7\u683C\uFF1A");
priceTxt = new JTextField();
priceTxt.setColumns(10);
JLabel lblNewLabel_7 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
authorTxt = new JTextField();
authorTxt.setColumns(10);
JLabel lblNewLabel_8 = new JLabel("\u56FE\u4E66\u7C7B\u522B;");
JLabel lblNewLabel_9 = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
bookDescTxt = new JTextArea();
bookTypeJcb = new JComboBox();
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(17)
.addComponent(lblNewLabel_3)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
.addGap(33)
.addComponent(lblNewLabel_4)
.addGap(5)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
.addGap(43)
.addComponent(lblNewLabel_5)
.addGap(6)
.addComponent(manJrb)
.addGap(12)
.addComponent(femaleJrb))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(19)
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(lblNewLabel_9)
.addGap(2)
.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 619, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(lblNewLabel_6)
.addGap(7)
.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
.addGap(32)
.addComponent(lblNewLabel_7)
.addGap(5)
.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, 173, GroupLayout.PREFERRED_SIZE)
.addGap(45)
.addComponent(lblNewLabel_8)
.addGap(11)
.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(53, Short.MAX_VALUE))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(21)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_3)))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(25)
.addComponent(lblNewLabel_4))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(22)
.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(23)
.addComponent(manJrb))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(25)
.addComponent(lblNewLabel_5))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(23)
.addComponent(femaleJrb)))
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(29)
.addComponent(lblNewLabel_6))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(30)
.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(34)
.addComponent(lblNewLabel_7))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(30)
.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(33)
.addComponent(lblNewLabel_8)))
.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(37)
.addComponent(lblNewLabel_9))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(34)
.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE))))
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(78)
.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(20, Short.MAX_VALUE))
);
panel_1.setLayout(gl_panel_1);
JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
s_bookNameTxt = new JTextField();
s_bookNameTxt.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
s_authorTxt = new JTextField();
s_authorTxt.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
JButton btnNewButton = new JButton("\u67E5\u8BE2");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
bookSearchActionPerformed(arg0);
}
});
btnNewButton.setIcon(new ImageIcon(BookManageInterfrm.class.getResource("/images/search.png")));
s_bookTypeJcb = new JComboBox();
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, 125, GroupLayout.PREFERRED_SIZE)
.addGap(22)
.addComponent(lblNewLabel_1)
.addGap(14)
.addComponent(s_authorTxt, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
.addGap(25)
.addComponent(lblNewLabel_2)
.addGap(14)
.addComponent(s_bookTypeJcb, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
.addComponent(btnNewButton)
.addGap(26))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(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)))
.addGroup(gl_panel.createSequentialGroup()
.addGap(14)
.addComponent(lblNewLabel_1))
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(s_authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel.createSequentialGroup()
.addGap(11)
.addComponent(lblNewLabel_2))
.addGroup(gl_panel.createSequentialGroup()
.addGap(9)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(s_bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btnNewButton))))
.addContainerGap(18, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
bookTable = new JTable();
bookTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent arg0) {
bookTableMousePressed(arg0);
}
});
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", "\u56FE\u4E66\u7C7B\u522BID"
}
) {
boolean[] columnEditables = new boolean[] {
false, false, false, false, false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
scrollPane.setViewportView(bookTable);
contentPane.setLayout(gl_contentPane);
this.setLocationRelativeTo(null);
bookDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
this.fillBookType("search");
this.fillBookType("modify");
this.fillTable(new Book_B());
this.fillTable1(new Book_B());
}
private void prevActionPerfromed(ActionEvent e) {
if(page==1) {
return;
}
page--;
this.fillTable1(new Book_B());
}
private void nextActionPerfromed(ActionEvent ent) {
page++;
this.fillTable1(new Book_B());
}
private void bookDeleteActionPerformed(ActionEvent evt) {
String id=idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null, "请选择要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗?");
if(n==0){
Connection con=null;
try{
con=dbutil.getCon();
boolean flag = bookDao.existBookByBookTypeId(con, id);
if (flag) {
JOptionPane.showMessageDialog(null, "当前图书类别下有图书,不能删除此类别");
return;
}
int deleteNum=bookDao.delete(con, id);
if(deleteNum==1){
JOptionPane.showMessageDialog(null, "删除成功");
this.resetValue();
this.fillTable(new Book_B());
}else{
JOptionPane.showMessageDialog(null, "删除失败");
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败");
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void bookUpdateActionPerformed(ActionEvent evt) {
String id=this.idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(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.showMessageDialog(null, "图书名称不能为空!");
return;
}
if(StringUtil.isEmpty(author)){
JOptionPane.showMessageDialog(null, "图书作者不能为空!");
return;
}
if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null, "图书价格不能为空!");
return;
}
String sex="";
if(manJrb.isSelected()){
sex="男";
}else if(femaleJrb.isSelected()){
sex="女";
}
BookType bookType=(BookType) bookTypeJcb.getSelectedItem();
int bookTypeId=bookType.getId();
Book_B book=new Book_B(Integer.parseInt(id), bookName, author, sex, Float.parseFloat(price), bookTypeId, bookDesc);
Connection con=null;
try{
con=dbutil.getCon();
int addNum=bookDao.update(con, book);
if(addNum==1){
JOptionPane.showMessageDialog(null, "图书修改成功!");
resetValue();
this.fillTable(new Book_B());
}else{
JOptionPane.showMessageDialog(null, "图书修改失败!");
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "图书修改失败!");
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void resetValue(){
this.idTxt.setText("");
this.bookNameTxt.setText("");
this.authorTxt.setText("");
this.priceTxt.setText("");
this.manJrb.setSelected(true);
this.bookDescTxt.setText("");
if(this.bookTypeJcb.getItemCount()>0){
this.bookTypeJcb.setSelectedIndex(0);
}
}
private void bookTableMousePressed(MouseEvent met) {
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("男".equals(sex)){
this.manJrb.setSelected(true);
}else if("女".equals(sex)){
this.femaleJrb.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);
}
}
}
private void bookSearchActionPerformed(ActionEvent arg0) {
String bookName = this.s_bookNameTxt.getText();
String author = this.s_authorTxt.getText();
BookType bookType =(BookType)this.s_bookTypeJcb.getSelectedItem();
int bookTypeId=bookType.getId();
Book_B book = new Book_B(bookName,author,bookTypeId);
this.fillTable(book);
}
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) {
e.printStackTrace();
}finally {
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void fillTable(Book_B book1){
DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=dbutil.getCon();
ResultSet rs=bookDao.list(con,book1);
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"));
v.add(rs.getString("bookTypeId"));
dtm.addRow(v);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
dbutil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void fillTable1(Book_B book){
DefaultTableModel dtm= (DefaultTableModel) bookTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try {
con=dbutil.getCon();
ResultSet rs = bookDao.query(con, book, page);
while(rs.next()) {
Vector v2=new Vector();
v2.add(rs.getString("id"));
v2.add(rs.getString("bookName"));
v2.add(rs.getString("author"));
v2.add(rs.getString("sex"));
v2.add(rs.getFloat("price"));
v2.add(rs.getString("bookDesc"));
v2.add(rs.getString("bookTypeName"));
v2.add(rs.getString("bookTypeId"));
dtm.addRow(v2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
九、全部资料及源码
基于Swing+jdbc+mysql的Java图书管理系统
基于Swing+jdbc+mysql的Java图书管理系统 下载后,可直接用Eclipse或者IEDA打开 内含: 1、BookManager图书管理系统源码(含jdbcUtils工具类和驱动) 2、ER图 3、SQL数据库表

十、Github链接
https://github.com/mao888/BookManager

十、更多相关文章点这里哦
【Java全栈】Java全套学习路线及项目资料总结【JavaSE+Web基础+MySQL+JavaEE】
【MySQL学习】数据库问题及着重点汇总
|