我的第一個專案—-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視覺化程式設計

  1. windowbuilder外掛的安裝
  2. 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的實現方法不好而且很多並沒有實際用處