我的第一个项目----Java图书管理系统
项目参考自:http://www.java1234.com/a/yuanchuang/swing2/
项目视频及代码下载地址:链接:http://pan.baidu.com/s/1pLpQw2J 密码:cncv
项目开发环境的搭建:http://pan.baidu.com/s/1ntzhAmH#list/path=%2F
一,功能
(1),用户登录
(2),图书类别管理
(3),图书管理
(4),退出
二,工具
(1),JAVA编程:eclipes(1.8 soon版本)
(2),SQL:mysql
(3),Jdbc: jar(mysql-connector-java-5.1.40-bin.jar)
三,效果展示
(1),登录
(2),主界面
(3),图书类别添加
(4),图书类别管理
(5),图书添加
(6),图书管理
(7),关于作者
四,数据库设计
(1),t_user表
(2),t_bookType表
(3),t_book表
(四),Java层次分析:
(1),逻辑图
(2),包结构
(五),数据库层级分析:
1, ER分析
2, 数据
用户: 用户编号,用户名,密码
图书类别:图书类别编号,图书类别名称
图书:图书编号,图书名称,图书作者,图书价格,图书描述,图书类别(外键)
图书类别与图书之间根据图书类别相互关联
3,数据库表的建立
(1),t_use 用户信息表
(2),t_bookType 图书类别管理表
(3),t_book 图书信息管理表
4,数据库表的关联(外键的关联)
(六),主要Java代码分析:
(1),Dao 类(以BookDao为例)
package com.java1234.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.java1234.model.Book;
import com.java1234.model.BookType;
import com.java1234.util.StringUtil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
/**
* 图书Dao类
* @author H_Pioneer
*
*/
public class BookDao {
/**
* 图书添加
* @param con
* @param book
* @return
* @throws Exception
*/
public int add(Connection con,Book book)throws Exception{
String sql="insert into t_book values(null,?,?,?,?,?,?)";
PreparedStatement pstmt=(PreparedStatement) 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();
}
/**
* 图书信息查询
* @param con
* @param book
* @return
* @throws Exception
*/
public ResultSet list(Connection con,Book book)throws Exception{
StringBuffer sb=new StringBuffer("select * from t_book b,t_bookType bt where b.bookTypeId=bt.id");
if(StringUtil.isNotEmpty(book.getBookName())){
sb.append(" and b.bookName like '%"+book.getBookName()+"%'");
}
if(StringUtil.isNotEmpty(book.getAuthor())){
sb.append(" and b.author like '%"+book.getAuthor()+"%'");
}
if(book.getBookTypeId()!=null && book.getBookTypeId()!=-1){
sb.append(" and b.bookTypeId="+book.getBookTypeId());
}
PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
/**
* 图书信息删除
* @param con
* @param id
* @return
* @throws SQLException
*/
public int delete(Connection con,String id)throws Exception{
String sql="delete from t_book where id=?";
PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
/**
* 图书信息修改
* @param con
* @param book
* @return
* @throws Exception
*/
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=(PreparedStatement) 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();
}
/**
*
* @param con
* @param bookTypeId
* @return
* @throws Exception
*/
public boolean existBookByBookTypeId(Connection con,String bookTypeId)throws Exception{
String sql="select * from t_book where bookTypeId=?";
PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
pstmt.setString(1, bookTypeId);
ResultSet rs = pstmt.executeQuery();
String string = new String();
return rs.next();
}
}
*重点内容::
JDBC进行简单的数据库增删改查
详细参考:http://www.cnblogs.com/wuyuegb2312/p/3872607.html
(2),Model类(以BookModel为例)
package com.java1234.model;
/**
* 图书实体类
* @author H_Pioneer
*
*/
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(int id2, String bookName, String author, String sex, float price, Integer bookTypeId, String bookDesc) {
super();
this.id = id2;
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookTypeId = bookTypeId;
this.bookDesc = bookDesc;
}
public Book(String bookName, String author, Integer bookTypeId) {
super();
this.bookName = bookName;
this.author = author;
this.bookTypeId = bookTypeId;
}
public Book(String bookName, String author, String sex, float price, Integer bookTypeId, String bookDesc) {
super();
this.bookName = bookName;
this.author = author;
this.sex = sex;
this.price = price;
this.bookTypeId = bookTypeId;
this.bookDesc = bookDesc;
}
public Book() {
super();
// TODO Auto-generated constructor stub
}
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;
}
}
**重点内容::
(1),接口类的构造方法
(2),get,set方法
快捷键:
Shift+Alt+S –> Generate Getters and Setters –>选择你需要的get,set参数
(3),构造函数的使用
快捷键:
(1),Shift+Alt+S –>generate constructor using fields–>使用字段生成
(2),Shift+Alt+S –>generate constructors from…..–>不使用字段从父类获取
(三),Util类
package com.java1234.util;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
/**
* 数据库工具类
* @author H_Pioneer
*
*/
public class DbUtil {
private String dbUrl = "jdbc:mysql://localhost:3306/db_book";
//也可以写成private String dbUrl = "jdbc:mysql:///db_book";
private String dbUserName = "root";
private String dbPassword = "123456";
private String jdbcName = "com.mysql.jdbc.Driver";
/**
* 获取数据库连接
* @return
* @throws Exception
*/
public Connection getCon()throws Exception{
Class.forName(jdbcName);
Connection con = (Connection) DriverManager.getConnection(dbUrl,dbUserName,dbPassword);//链接数据库
return con;
}
/**
* 关闭数据库连接
* @param con
* @throws Exception
*/
public void closeCon (java.sql.Connection con)throws Exception {
if(con!=null){
con.close();
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
DbUtil dbUtil = new DbUtil();
try {
dbUtil.getCon();
System.out.println("数据库连接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); //在命令行打印异常信息在程序中出错的位置及原因。
System.out.println("数据库连接");
}
}
}
数据库工具类
package com.java1234.util;
import org.junit.Test;
import com.mysql.jdbc.StringUtils;
/**
* 字符串工具类
* @author H_Pioneer
*
*/
public class StringUtil {
/**
* 判断是否为空
* @param str
* @return
*/
public static boolean isEmpty(String str){
if(str==null||"".equals(str.trim())){
return true;
}else{
return false;
}
}
/**
* 判断不为空
* @param str
* @return
*/
public static boolean isNotEmpty(String str){
if(str!=null&&!"".equals(str.trim())){
return true;
}else{
return false;
}
}
}
**重点::
工具类的使用
(1)
字符串工具类的总结:
http://www.cnblogs.com/DreamDrive/p/5760588.html
(2)
数据库工具类的总结:
http://kettas.iteye.com/blog/1222519
(四),Frm类(以登录和图书类别添加为例)
package com.java1234.view;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.border.EmptyBorder;
import com.java1234.dao.UserDao;
import com.java1234.model.User;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;
import com.mysql.jdbc.Connection;
public class LogOnFrm extends JFrame {
private JPanel contentPane;
private final JTextField textField = new JTextField();
private JPasswordField passwordTxt;
private DbUtil dbUtil = new DbUtil();
private UserDao userDao = new UserDao();
private JTextField userNameTxt;
/**
* 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() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setTitle("管理员登录");
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("图书管理系统");
label.setFont(new Font("黑体", Font.BOLD, 25));
label.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/logo.png")));
JLabel lblNewLabel = new JLabel("用户名:");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 13));
lblNewLabel.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/userName.png")));
JLabel lblNewLabel_1 = new JLabel("密 码:");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 13));
lblNewLabel_1.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/password.png")));
textField.setColumns(10);
passwordTxt = new JPasswordField();
JButton btnNewButton = new JButton("登录");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loginActionPerformed(e);
}
});
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 13));
btnNewButton.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/login.png")));
JButton btnNewButton_1 = new JButton("重置");
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 13));
btnNewButton_1.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/reset.png")));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetValueActionPerformed(e);
}
});
userNameTxt = new JTextField();
userNameTxt.setColumns(10);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
.addGap(223))
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(lblNewLabel_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnNewButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(50))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
.addGap(36)))
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(btnNewButton_1)
.addGroup(Alignment.LEADING, gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
.addComponent(passwordTxt, Alignment.LEADING)
.addComponent(userNameTxt, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)))
.addGap(63))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label, GroupLayout.PREFERRED_SIZE, 320, GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(18)
.addComponent(label)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
.addGap(14)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(45)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
.addGap(27)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
.addComponent(btnNewButton))
.addContainerGap(13, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
//设置居中显示
this.setLocationRelativeTo(null);
}
/**
* 登录事件处理
* @param e
*/
protected void loginActionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String userName = this.userNameTxt.getText();
String password = new String(this.passwordTxt.getPassword());
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null,"用户名不能为空");
return;
}
if(StringUtil.isEmpty(password)){
JOptionPane.showMessageDialog(null,"密码不能为空");
return;
}
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.showMessageDialog(null,"用户名密码错误");
}
}catch(Exception e1){
e1.printStackTrace();
}
}
/**
* 重置事件处理
* @param evt
*/
private void resetValueActionPerformed(ActionEvent evt) {
// TODO Auto-generated method stub
this.userNameTxt.setText("");
this.passwordTxt.setText("");
}
}
**重点::
(1),Java可视化编程
- windowbuilder插件的安装
- Window Builder→SWT Designer→SWT→Application Window→Next→窗口名→默认→Finish→s自动生成代码→Design
(2),对于按钮等添加事件如何与数据接口联系
对于JFrame,JLable,JTable等,右击可以选择重命名或者添加事件即可返回代码之中,一般我们会把操作进行封装,对事件进行相应的处理
(七),整个项目的分析与不足
1.MVC3层架构有问题(这个只有dao层)
正常的应该是dao层就接口不是实现类,现在的dao是正常的daoImpl,dao的实现类
2.实体类(com.java1234.model包下的)可以是entity,domain
应该尽量用entity或model,少用domain
3 DbUtil和StringUtil的实现方法不好而且很多并没有实际用处