1、准备工作:
先在Eclipse中安装windowbuiler插件(窗体设计插件,比Swing强大)
点击Help —> Install New Software —> Add —>加入windowbuiler的下载地址
http://download.eclipse.org/windowbuilder/latest/ —>安装完重启即可
添加jar包的另一种方式,在项目中新建一个文件夹存放插件 -->Build Path -->Add to Build Path
移除jar包:点击Referenced Libraries中的插件–>Build Path -->Remove from Build Path
然后在数据库中创建几张表
t_user表
t_booktype表
t_book表
2、实现:
实现步骤:
1 管理员登入功能的实现
2 系统主界面的实现
3 图书类别添加功能
4 图书类别查询功能
5 图书类别修改和删除功能
6 图书添加功能
7 图书查询功能
8 图书修改和删除功能
实现代码:
代码结构如上图所示,dao包表示表示对数据库操作的类,model表示实体类,util表示工具类,view表示视图类(框架类),images表示要用到的一些小图标,jdbc中的是数据库驱动包。
BookDao.java类中包括了对图书增、删、改、查的方法
package com.chb.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.chb.model.Book;
import com.chb.util.StringUtil;
//图书Dao类
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.setInt(5, book.getBookTypeId());
pstmt.setString(6, book.getBookDesc());
return pstmt.executeUpdate();
}
//图书信息查询
public ResultSet list(Connection con,Book book)throws Exception{
StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
if(StringUtil.isNotEmpty(book.getBookName())){
sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
}
if(StringUtil.isNotEmpty(book.getAuthor())){
sb.append(" and b.author like '%"+book.getAuthor()+"%'");
}
if(StringUtil.isNotEmpty(book.getSex())){
sb.append(" and b.sex = '"+book.getSex()+"'");
}
if(book.getBookTypeId()!=null){
sb.append(" and b.bookTypeId = "+book.getBookTypeId());
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.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 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();
}
}
BookTypeDao类中包含了对图书类型增、删、改、查的方法
package com.chb.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.chb.model.BookType;
import com.chb.util.StringUtil;
//图书类别Dao类
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(StringUtil.isNotEmpty(bookType.getBookTypeName())){
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 update(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();
}
}
UserDao类中包含了用户登入验证的方法
package com.chb.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.chb.model.User;
//用户Dao类
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.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
}
Book类表示图书实体类
package com.chb.model;
//图书实体
public class Book {
private int id; // 编号
private String bookName; // 图书名称
private String author; // 作者
private String sex; // 性别
private Float price; // 图书价格
private String bookDesc; // 备注
private Integer bookTypeId; // 图书类型Id
private String BookTypeName; // 图书类型名称
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String bookName, String author, String sex, Float price, String bookDesc, Integer bookTypeId) {
super();
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookDesc = bookDesc;
this.bookTypeId = bookTypeId;
}
public Book(Integer id,String bookName, String author, String sex, Float price, String bookDesc, Integer bookTypeId) {
super();
this.id=id;
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookDesc = bookDesc;
this.bookTypeId = bookTypeId;
}
public Book(String bookName, String author, Integer bookTypeId) {
super();
this.bookName = bookName;
this.author = author;
this.bookTypeId = bookTypeId;
}
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 String getBookDesc() {
return bookDesc;
}
public void setBookDesc(String bookDesc) {
this.bookDesc = bookDesc;
}
public Integer getBookTypeId() {
return bookTypeId;
}
public void setBookTypeId(Integer bookTypeId) {
this.bookTypeId = bookTypeId;
}
public String getBookTypeName() {
return BookTypeName;
}
public void setBookTypeName(String bookTypeName) {
BookTypeName = bookTypeName;
}
}
BookType类表示图书类别实体类
package com.chb.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 this.getBookTypeName();
}
}
User类表示用户实体类
package com.chb.model;
//用户实体
public class User {
private int id; // 编号
private String userName; // 用户名
private String password; // 密码
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String userName, String password) {
super();
this.userName = userName;
this.password = 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;
}
}
DbUtil类表示数据库工具类
package com.chb.util;
import java.sql.Connection;
import java.sql.DriverManager;
//数据库工具类
public class DbUtil {
private String dbUrl="jdbc:mysql://localhost:3306/db_book"; // 数据库连接地址
private String dbUserName="root"; // 用户名
private String dbPassword="123456"; // 密码
private String jdbcName="com.mysql.jdbc.Driver"; // 驱动名称
//获取数据库连接
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
return con;
}
//关闭数据库连接
public void closeCon(Connection con) throws Exception{
if(con!=null){
con.close();
}
}
}
StringUtil类表示字符串工具类
package com.chb.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;
}
}
}
LogOnFrm类表示登入界面
package com.chb.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.chb.dao.UserDao;
import com.chb.model.User;
import com.chb.util.DbUtil;
import com.chb.util.StringUtil;
public class LogOnFrm extends JFrame {
private DbUtil dbUtil = new DbUtil();
private UserDao userDao = new UserDao();
private JPanel contentPane;
private JTextField userNameTxt;
private JPasswordField passwordTxt;
/**
* Launch the application.
*/
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();
}
}
});
}
/**
* Create the frame.
*/
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);
}
}
setResizable(false);
setTitle("\u7BA1\u7406\u5458\u767B\u5F55");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 482, 397);
contentPane = new JPanel();
contentPane.setToolTipText("");
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.se