1、系統架構
Java jdk+ eclipse + mysql資料庫
2、系統結構圖

3、各子產品詳細設計
由于這次我們的項目主要是用來熟悉軟體工程的流程,是以難度不大。主要子產品介紹如下:
1、帳号
package cn.edu.sau.model;
public class User {
private int id;
private String userName;
private String userPassword;
private int role = 0; //預設為0->讀者 、 1->圖書管理者
private String pname;
public User(){
this.id = -1;
}
public User(int id, String userName) {
super();
this.id = id;
this.userName = userName;
}
public User(String userName, String userPassword) {
super();
this.userName = userName;
this.userPassword = userPassword;
}
public User(int id,String userName, String userPassword, int role,String pname) {
super();
this.id = id;
this.userName = userName;
this.userPassword = userPassword;
this.role = role;
this.pname = pname;
}
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 getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
}
cn.edu.sau.model.User
User類實作了對帳号的抽象描述,主要包括使用者名,密碼,帳号角色以及賬号所有人名稱。
1 package cn.edu.sau.dao;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6
7 import cn.edu.sau.model.User;
8
9 public class UserDao {
10 public User login(Connection con,User user) throws Exception{
11 User returnUser = null;
12 String sql = "select * from t_user where userName=? and password=?";
13 PreparedStatement pstmt = con.prepareStatement(sql);
14 pstmt.setString(1, user.getUserName());
15 pstmt.setString(2, user.getUserPassword());
16 ResultSet rs = pstmt.executeQuery();
17 if(rs.next()){
18 returnUser = new User(rs.getString("userName"),rs.getString("password"),rs.getInt("role"));
19 }
20 return returnUser;
21 }
22 }
cn.edu.sau.dao.UserDao
UserDao提供帳号驗證的方法并傳回帳号對象,為空則帳号不存在。
package cn.edu.sau.view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
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.border.EmptyBorder;
import cn.edu.sau.dao.UserDao;
import cn.edu.sau.model.User;
import cn.edu.sau.util.DbUtil;
import cn.edu.sau.util.StringUtil;
import javax.swing.ImageIcon;
public class LogonFrm extends JFrame {
private JPanel contentPane;
private JTextField userNameTxt;
private JPasswordField userPasswordTxt;
/**
* 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() {
setTitle("\u56FE\u4E66\u7BA1\u7406\u7CFB\u7EDF");
UserDao userDao = new UserDao();
DbUtil dbUtil = new DbUtil();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
//設定frame居中
this.setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
init();
}
private void reset(){
userNameTxt.setText("");
userPasswordTxt.setText("");
}
private void init() {
DbUtil dbUtil = new DbUtil();
UserDao userDao = new UserDao();
JLabel lb_userName = new JLabel("\u7528\u6237\u540D\uFF1A");
lb_userName.setBounds(102, 75, 54, 15);
contentPane.add(lb_userName);
userNameTxt = new JTextField();
userNameTxt.setBounds(166, 72, 152, 21);
contentPane.add(userNameTxt);
userNameTxt.setColumns(10);
JLabel lb_userPassword = new JLabel("\u5BC6 \u7801\uFF1A");
lb_userPassword.setBounds(102, 129, 54, 15);
contentPane.add(lb_userPassword);
JButton jb_login = new JButton("\u767B\u5F55");
jb_login.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\login.png"));
jb_login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName = userNameTxt.getText();
String userPassword = new String(userPasswordTxt.getPassword());
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null, "使用者名不能為空");
return;
}
if(StringUtil.isEmpty(userPassword)){
JOptionPane.showMessageDialog(null, "密碼不能為空");
return;
}
User user = new User(userName,userPassword);
Connection con = null;
try {
con = dbUtil.getCon();
User reUser = userDao.login(con, user);
if(reUser != null){
dispose();//銷毀登入框
if(1 == reUser.getRole()){
new MainFrm().setVisible(true);//建立管理者主界面Firme
}
else if(0 == reUser.getRole()){
new UserMainFrm(reUser).setVisible(true);
}
}else{
JOptionPane.showMessageDialog(null, "使用者名或密碼不正确");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "登入失敗");
e1.printStackTrace();
} finally{
try {
con.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
});
jb_login.setBounds(102, 191, 93, 23);
contentPane.add(jb_login);
JButton jb_reset = new JButton("\u91CD\u7F6E");
jb_reset.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\reset.png"));
jb_reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
reset();
}
});
jb_reset.setBounds(225, 191, 93, 23);
contentPane.add(jb_reset);
userPasswordTxt = new JPasswordField();
userPasswordTxt.setBounds(166, 126, 152, 21);
contentPane.add(userPasswordTxt);
JLabel lblNewLabel = new JLabel(" ");
lblNewLabel.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\logo.png"));
lblNewLabel.setBounds(182, 10, 99, 52);
contentPane.add(lblNewLabel);
}
}
cn.edu.sau.view.LogonFrm
登入框界面通過驗證邏輯層傳回的帳号資料判斷帳号是否存在,以及判斷帳号權限,打開新的界面。
2、圖書類别維護
package cn.edu.sau.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 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();
}
}
cn.edu.sau.model.BookType
BookType類實作了對圖書類别的對象描述,主要有圖書類别名稱以及圖書類别的描述。
package cn.edu.sau.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import cn.edu.sau.model.BookType;
import cn.edu.sau.util.StringUtil;
public class BookTypeDao {
public int bookTypeAdd(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 bookTypeList(Connection con , BookType bookType) throws Exception{
StringBuffer sb = new StringBuffer("select * from t_booktype");
String bookTypeName = bookType.getBookTypeName();
if(!StringUtil.isEmpty(bookTypeName)){
sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
}
PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceAll("and", "where"));
return pstmt.executeQuery();
}
public int bookTypeModify(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();
}
public int bookTypeDelete(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();
}
}
cn.edu.sau.dao.BookTypeDao
BookTypeDao類提供了添加圖書類别,擷取圖書類别清單,更改圖書類别以及删除圖書類别方法。
String bookTypeName = bookTypeNameTxt.getText();
String bookTypeDesc = bookTypeDescTxt.getText();
if(StringUtil.isEmpty(bookTypeName)){
JOptionPane.showMessageDialog(null,"圖書類别名稱不能為空");
}else{
Connection con = null;
try {
con = dbUtil.getCon();
BookType bookType = new BookType(bookTypeName,bookTypeDesc);
int ret = bookTypeDao.bookTypeAdd(con, bookType);
if(1 == ret){
JOptionPane.showMessageDialog(null, "圖書類别添加成功");
reset();
}else{
JOptionPane.showMessageDialog(null, "圖書類别添加失敗");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "圖書類别添加失敗");
e1.printStackTrace();
}
}
}
cn.edu.sau.view.BookTypeAddInterFrm
圖書類别添加界面擷取使用者輸入,進行圖書類别的添加。
private void fillTable(BookType bookType){
DefaultTableModel dtm = (DefaultTableModel)bookTypeTable.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = dbUtil.getCon();
ResultSet res = bookTypeDao.bookTypeList(con, bookType);
while(res.next()){
Vector v = new Vector();
v.add(res.getString("id"));
v.add(res.getString("bookTypeName"));
v.add(res.getString("bookTypeDesc"));
dtm.addRow(v);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//滑鼠點選表格填充内容至相應位置
bookTypeTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
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));
}
});
//修改圖書類别函數體
String bookTypeName = bookTypeNameTxt.getText();
String bookTypeDesc = bookTypeDescTxt.getText();
if(StringUtil.isEmpty(bookTypeName)){
JOptionPane.showMessageDialog(null, "圖書類别名稱不能為空");
}
BookType bookType = new BookType(bookTypeName,bookTypeDesc);
bookType.setId(Integer.parseInt(idTxt.getText()));
Connection con = null;
try {
con = dbUtil.getCon();
int re = bookTypeDao.bookTypeModify(con, bookType);
if(1 == re){
JOptionPane.showMessageDialog(null, "修改成功");
fillTable(new BookType("",""));
reset();
}else{
JOptionPane.showMessageDialog(null, "修改失敗");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "修改失敗");
e1.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e1) {
e1.printStackTrace();
}
}
//删除圖書類别函數體
String id = idTxt.getText();
Connection con = null;
try {
con = dbUtil.getCon();
int re = bookTypeDao.bookTypeDelete(con, id);
if(1 == re){
JOptionPane.showMessageDialog(null, "删除成功");
fillTable(new BookType("",""));
reset();
}else{
JOptionPane.showMessageDialog(null, "删除失敗");
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "删除失敗");
e1.printStackTrace();
} finally{
try {
dbUtil.closeCon(con);
} catch (Exception e1) {
e1.printStackTrace();
}
}
cn.edu.sau.view.BookTypeManageInterFrm
fillTable為圖書類别管理界面函數,填充界面表格元素的内容。其餘函數在代碼中有注釋,分别為表格的點選自動填充、修改圖書類别、删除圖書類别代碼實作
3、圖書維護
package cn.edu.sau.model;
/**
* 圖書類
* */
public class Book {
private int id;
private String bookName;
private String bookAuthor;
private float bookPrice;
private String bookDesc;
private int bookTypeId;
private String bookTypeName;
public Book() {
super();
this.id = -1;
this.bookName = "";
this.bookAuthor = "";
this.bookPrice = 0;
this.bookDesc = "";
this.bookTypeId = -1;
}
public Book(int id, String bookName) {
super();
this.id = id;
this.bookName = bookName;
}
public Book(int id, String bookName, String bookAuthor, float bookPrice,
String bookDesc, int bookTypeId) {
super();
this.id = id;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookPrice = bookPrice;
this.bookDesc = bookDesc;
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 getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public float getBookPrice() {
return bookPrice;
}
public void setBookPrice(float bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookDesc() {
return bookDesc;
}
public void setBookDesc(String bookDesc) {
this.bookDesc = bookDesc;
}
public int getBookTypeId() {
return bookTypeId;
}
public void setBookTypeId(int bookTypeId) {
this.bookTypeId = bookTypeId;
}
public String getBookTypeName() {
return bookTypeName;
}
public void setBookTypeName(String bookTypeName) {
this.bookTypeName = bookTypeName;
}
}
cn.edu.sau.model.Book
該類為圖書的抽象類,完成了對圖書的抽象描述。
package cn.edu.sau.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import cn.edu.sau.model.Book;
import cn.edu.sau.model.BookType;
import cn.edu.sau.util.StringUtil;
public class BookDao {
public static int bookAdd(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.getBookAuthor());
pstmt.setFloat(3,book.getBookPrice());
pstmt.setString(4,book.getBookDesc());
pstmt.setInt(5,book.getBookTypeId());
return pstmt.executeUpdate();
}
public ResultSet bookList(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.getBookAuthor())){
sb.append(" and b.author like '%"+book.getBookAuthor()+"%'");
}
if(book.getBookTypeId()!=-1){
sb.append(" and b.bookTypeId = "+book.getBookTypeId());
}
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
public int bookDelete(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 bookModify(Connection con,Book book) throws Exception{
String sql="update t_book set bookName=?,author=?,price=?,bookDesc=?,bookTypeId=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, book.getBookName());
pstmt.setString(2, book.getBookAuthor());
pstmt.setFloat(3, book.getBookPrice());
pstmt.setString(4, book.getBookDesc());
pstmt.setInt(5, book.getBookTypeId());
pstmt.setInt(6, book.getId());
return pstmt.executeUpdate();
}
public boolean getBookByBookTypeId(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();
}
}
cn.edu.sau.dao.BookDao
該類和圖書類别維護的Dao層類相似,實作了圖書添加,圖書類别,圖書修改以及圖書删除的方法。
package cn.edu.sau.view;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import cn.edu.sau.dao.BookDao;
import cn.edu.sau.dao.BookTypeDao;
import cn.edu.sau.model.Book;
import cn.edu.sau.model.BookType;
import cn.edu.sau.util.DbUtil;
import cn.edu.sau.util.StringUtil;
import javax.swing.ImageIcon;
public class BookAddInterFrm extends JInternalFrame {
DbUtil dbUtil = new DbUtil();
BookTypeDao bookTypeDao = new BookTypeDao();
BookDao bookDao = new BookDao();
private JTextField bookNameTxt;
private JTextField bookAuthorTxt;
private JTextField bookPriceTxt;
private JComboBox jcb_bookType;
private JTextArea bookDescTxt;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookAddInterFrm frame = new BookAddInterFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BookAddInterFrm() {
setClosable(true);
setIconifiable(true);
setTitle("\u56FE\u4E66\u6DFB\u52A0");
setBounds(100, 100, 474, 443);
setLocation(500, 100);
getContentPane().setLayout(null);
init();
fillBookType();
}
private void init() {
JLabel lb_bookName = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
lb_bookName.setBounds(29, 39, 72, 15);
getContentPane().add(lb_bookName);
bookNameTxt = new JTextField();
bookNameTxt.setBounds(112, 36, 106, 21);
getContentPane().add(bookNameTxt);
bookNameTxt.setColumns(10);
JLabel lb_bookAuthor = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
lb_bookAuthor.setBounds(236, 39, 72, 15);
getContentPane().add(lb_bookAuthor);
bookAuthorTxt = new JTextField();
bookAuthorTxt.setBounds(304, 36, 101, 21);
getContentPane().add(bookAuthorTxt);
bookAuthorTxt.setColumns(10);
JLabel lb_bookPrice = new JLabel("\u56FE\u4E66\u4EF7\u683C\uFF1A");
lb_bookPrice.setBounds(29, 100, 72, 15);
getContentPane().add(lb_bookPrice);
bookPriceTxt = new JTextField();
bookPriceTxt.setBounds(112, 97, 106, 21);
getContentPane().add(bookPriceTxt);
bookPriceTxt.setColumns(10);
JLabel lb_bookType = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
lb_bookType.setBounds(236, 100, 74, 15);
getContentPane().add(lb_bookType);
jcb_bookType = new JComboBox();
jcb_bookType.setBounds(304, 97, 101, 21);
getContentPane().add(jcb_bookType);
JLabel lb_bookDesc = new JLabel("\u56FE\u4E66\u7B80\u4ECB\uFF1A");
lb_bookDesc.setBounds(28, 176, 73, 15);
getContentPane().add(lb_bookDesc);
bookDescTxt = new JTextArea();
bookDescTxt.setBounds(112, 172, 297, 127);
getContentPane().add(bookDescTxt);
JButton jb_bookAdd = new JButton("\u6DFB\u52A0");
jb_bookAdd.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\add.png"));
jb_bookAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bookName = bookNameTxt.getText();
String bookAuthor = bookAuthorTxt.getText();
String bookPrice = bookPriceTxt.getText();
String bookDesc = bookDescTxt.getText();
if(StringUtil.isEmpty(bookName)){
JOptionPane.showMessageDialog(null, "圖書名稱不能為空");
return;
}
if(StringUtil.isEmpty(bookAuthor)){
JOptionPane.showMessageDialog(null, "圖書作者不能為空");
return;
}
if(StringUtil.isEmpty(bookPrice)){
JOptionPane.showMessageDialog(null, "圖書價格不能為空");
return;
}
BookType bookType = (BookType)jcb_bookType.getSelectedItem();
int bookTypeId = bookType.getId();
Book book = new Book(-1,bookName,bookAuthor,Float.parseFloat(bookPrice),bookDesc,bookTypeId);
Connection con = null;
try{
con = dbUtil.getCon();
int addNum = BookDao.bookAdd(con,book);
if(1 == addNum){
JOptionPane.showMessageDialog(null, "添加成功");
resetValue();
}else{
JOptionPane.showMessageDialog(null, "添加失敗");
}
}catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失敗");
}finally{
try {
con.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
});
jb_bookAdd.setBounds(115, 339, 93, 23);
getContentPane().add(jb_bookAdd);
JButton jb_bookReset = new JButton("\u91CD\u7F6E");
jb_bookReset.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\reset.png"));
jb_bookReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetValue();
}
});
jb_bookReset.setBounds(264, 339, 93, 23);
getContentPane().add(jb_bookReset);
}
private void fillBookType(){
Connection con = null;
BookType bookType = null;
try{
con = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeList(con, new BookType("",""));
while(rs.next()){
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
jcb_bookType.addItem(bookType);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private void resetValue(){
bookNameTxt.setText("");
bookAuthorTxt.setText("");
bookPriceTxt.setText("");
jcb_bookType.setSelectedIndex(0);
bookDescTxt.setText("");
}
}
cn.edu.sau.view.BookAddInterFrm
界面中提供了添加圖書的具體實作,以及其中将圖書類别添加進下拉框的方法。
package cn.edu.sau.view;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import cn.edu.sau.dao.BookDao;
import cn.edu.sau.dao.BookTypeDao;
import cn.edu.sau.model.Book;
import cn.edu.sau.model.BookType;
import cn.edu.sau.util.DbUtil;
import cn.edu.sau.util.StringUtil;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
public class BookManageInterFrm extends JInternalFrame {
DbUtil dbUtil = new DbUtil();
BookTypeDao bookTypeDao = new BookTypeDao();
BookDao bookDao = new BookDao();
private JTextField s_bookNameTxt;
private JTextField s_bookAuthorTxt;
private JTable bookTable;
private JTextField idTxt;
private JTextField bookNameTxt;
private JTextField bookAuthorTxt;
private JTextField bookPriceTxt;
private JTextArea bookDescTxt;
private JScrollPane scrollPane;
private JComboBox<BookType> s_jcbBookType;
private JComboBox<BookType> jcb_bookType;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookManageInterFrm frame = new BookManageInterFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BookManageInterFrm() {
setClosable(true);
setIconifiable(true);
setTitle("\u56FE\u4E66\u7BA1\u7406");
setBounds(100, 100, 812, 618);
setLocation(300, 20);
getContentPane().setLayout(null);
init();
fillTable(new Book());
this.fillBookType("search");
this.fillBookType("modify");
}
private void fillBookType(String type) {
Connection con = null;
BookType bookType = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
if ("search".equals(type)) {
bookType = new BookType();
bookType.setBookTypeName("請選擇...");
bookType.setId(-1);
this.s_jcbBookType.addItem(bookType);
}
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
if ("search".equals(type)) {
this.s_jcbBookType.addItem(bookType);
} else if ("modify".equals(type)) {
this.jcb_bookType.addItem(bookType);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void fillTable(Book book) {
DefaultTableModel dtm = (DefaultTableModel)bookTable.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookDao.bookList(con, book);
while (rs.next()) {
Vector v = new Vector();
v.add(rs.getInt("id"));
v.add(rs.getString("bookName"));
v.add(rs.getString("author"));
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();
}
}
}
private void init() {
scrollPane = new JScrollPane();
scrollPane.setToolTipText("");
scrollPane.setBounds(30, 91, 732, 178);
getContentPane().add(scrollPane);
bookTable = new JTable();
bookTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row = bookTable.getSelectedRow();
idTxt.setText((Integer)bookTable.getValueAt(row, 0)+"");
bookNameTxt.setText((String) bookTable.getValueAt(row, 1));
bookAuthorTxt.setText((String) bookTable.getValueAt(row, 2));
bookPriceTxt.setText((Float) bookTable.getValueAt(row, 3) + "");
bookDescTxt.setText(bookTable.getValueAt(row, 4) + "");
String bookTypeName = (String) bookTable.getValueAt(row, 5);
int n = jcb_bookType.getItemCount();
for (int i = 0; i < n; i++) {
BookType item = (BookType) jcb_bookType.getItemAt(i);
if (item.getBookTypeName().equals(bookTypeName)) {
jcb_bookType.setSelectedIndex(i);
}
}
}
});
bookTable.setToolTipText("\u56FE\u4E66");
bookTable.setSurrendersFocusOnKeystroke(true);
bookTable.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null, null},
},
new String[] {
"\u56FE\u4E66\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u56FE\u4E66\u4F5C\u8005", "\u56FE\u4E66\u4EF7\u683C", "\u56FE\u4E66\u63CF\u8FF0", "\u56FE\u4E66\u7C7B\u522B"
}
) {
boolean[] columnEditables = new boolean[] {
true, false, false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
bookTable.getColumnModel().getColumn(1).setPreferredWidth(100);
bookTable.getColumnModel().getColumn(2).setPreferredWidth(92);
bookTable.getColumnModel().getColumn(3).setPreferredWidth(94);
bookTable.getColumnModel().getColumn(4).setPreferredWidth(164);
bookTable.getColumnModel().getColumn(5).setPreferredWidth(97);
scrollPane.setViewportView(bookTable);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setToolTipText("\u641C\u7D22\u6761\u4EF6");
panel.setBounds(31, 14, 732, 68);
getContentPane().add(panel);
panel.setLayout(null);
JLabel label = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A\r\n");
label.setBounds(410, 29, 72, 15);
panel.add(label);
JLabel lblNewLabel_1 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
lblNewLabel_1.setBounds(209, 29, 72, 15);
panel.add(lblNewLabel_1);
JLabel lblNewLabel = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
lblNewLabel.setBounds(10, 29, 67, 15);
panel.add(lblNewLabel);
s_bookNameTxt = new JTextField();
s_bookNameTxt.setBounds(84, 26, 114, 21);
panel.add(s_bookNameTxt);
s_bookNameTxt.setColumns(10);
s_bookAuthorTxt = new JTextField();
s_bookAuthorTxt.setBounds(285, 26, 114, 21);
panel.add(s_bookAuthorTxt);
s_bookAuthorTxt.setColumns(10);
JButton jb_search = new JButton("\u67E5\u8BE2");
jb_search.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\search.png"));
jb_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Book book = new Book();
String bookName = s_bookNameTxt.getText();
String bookAuthor = s_bookAuthorTxt.getText();
BookType bookType = (BookType)s_jcbBookType.getSelectedItem();
int bookTypeId = bookType.getId();
book.setBookName(bookName);
book.setBookAuthor(bookAuthor);
book.setBookTypeId(bookTypeId);
fillTable(book);
}
});
jb_search.setBounds(629, 25, 93, 23);
panel.add(jb_search);
s_jcbBookType = new JComboBox<BookType>();
s_jcbBookType.setBounds(472, 26, 131, 21);
panel.add(s_jcbBookType);
panel.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{label, lblNewLabel_1, lblNewLabel, s_bookNameTxt, s_bookAuthorTxt, jb_search, s_jcbBookType}));
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setToolTipText("\u8868\u5355\u64CD\u4F5C");
panel_1.setBounds(32, 283, 732, 274);
getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel label_1 = new JLabel("\u7F16\u53F7\uFF1A");
label_1.setBounds(10, 41, 57, 15);
panel_1.add(label_1);
idTxt = new JTextField();
idTxt.setEditable(false);
idTxt.setBounds(96, 38, 118, 21);
panel_1.add(idTxt);
idTxt.setColumns(10);
JLabel label_2 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
label_2.setBounds(10, 81, 76, 15);
panel_1.add(label_2);
bookNameTxt = new JTextField();
bookNameTxt.setBounds(96, 78, 118, 21);
panel_1.add(bookNameTxt);
bookNameTxt.setColumns(10);
JLabel label_3 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
label_3.setBounds(224, 81, 65, 15);
panel_1.add(label_3);
bookAuthorTxt = new JTextField();
bookAuthorTxt.setColumns(10);
bookAuthorTxt.setBounds(299, 78, 118, 21);
panel_1.add(bookAuthorTxt);
JLabel label_4 = new JLabel("\u56FE\u4E66\u4EF7\u683C\uFF1A");
label_4.setBounds(10, 160, 76, 15);
panel_1.add(label_4);
bookPriceTxt = new JTextField();
bookPriceTxt.setBounds(96, 157, 117, 21);
panel_1.add(bookPriceTxt);
bookPriceTxt.setColumns(10);
JLabel label_5 = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A");
label_5.setBounds(224, 160, 65, 15);
panel_1.add(label_5);
jcb_bookType = new JComboBox<BookType>();
jcb_bookType.setBounds(299, 157, 118, 21);
panel_1.add(jcb_bookType);
JLabel label_6 = new JLabel("\u56FE\u4E66\u63CF\u8FF0\uFF1A");
label_6.setBounds(439, 81, 65, 15);
panel_1.add(label_6);
bookDescTxt = new JTextArea();
bookDescTxt.setBounds(514, 77, 208, 98);
panel_1.add(bookDescTxt);
JButton jb_modify = new JButton("\u4FEE\u6539");
jb_modify.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\modify.png"));
jb_modify.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id = idTxt.getText();
if (StringUtil.isEmpty(id)) {
JOptionPane.showMessageDialog(null, "請選擇要修改的記錄!");
return;
}
String bookName=bookNameTxt.getText();
String author=bookAuthorTxt.getText();
String price=bookPriceTxt.getText();
String bookDesc=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;
}
BookType bookType=(BookType) jcb_bookType.getSelectedItem();
int bookTypeId=bookType.getId();
Book book=new Book(Integer.parseInt(id),bookName,author,Float.parseFloat(price), bookDesc, bookTypeId);
Connection con = null;
try {
con = dbUtil.getCon();
int modifyNum = bookDao.bookModify(con, book);
if (modifyNum == 1) {
JOptionPane.showMessageDialog(null, "修改成功");
resetValue();
fillTable(new Book());
} else {
JOptionPane.showMessageDialog(null, "修改失敗");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "修改失敗");
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception ec) {
ec.printStackTrace();
}
}
}
});
jb_modify.setBounds(66, 225, 93, 23);
panel_1.add(jb_modify);
JButton jb_delete = new JButton("\u5220\u9664");
jb_delete.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\delete.png"));
jb_delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
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 re = bookDao.bookDelete(con, id);
if(1 == re){
JOptionPane.showMessageDialog(null, "删除成功");
resetValue();
fillTable(new Book());
}else
JOptionPane.showMessageDialog(null, "删除失敗");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "删除失敗");
e1.printStackTrace();
}
}
}
});
jb_delete.setBounds(257, 225, 93, 23);
panel_1.add(jb_delete);
}
private void resetValue() {
this.idTxt.setText("");
this.bookNameTxt.setText("");
this.bookAuthorTxt.setText("");
this.bookPriceTxt.setText("");
this.bookDescTxt.setText("");
if (jcb_bookType.getItemCount() > 0) {
jcb_bookType.setSelectedIndex(0);
}
}
}
cn.edu.sau.view.BookManageInterFrm
提供圖書管理功能,主要功能和圖書類别管理類似,這裡不再贅述。
4、讀者借閱圖書
package cn.edu.sau.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import cn.edu.sau.model.Book;
import cn.edu.sau.model.User;
import cn.edu.sau.util.StringUtil;
public class UserDao {
public User login(Connection con, User user) throws Exception {
User returnUser = 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.getUserPassword());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
returnUser = new User(rs.getInt("id"), rs.getString("userName"),
rs.getString("password"), rs.getInt("role"),
rs.getString("pname"));
}
return returnUser;
}
// 借書
public int lendBook(Connection con, int userId, int bookId)
throws Exception {
String sql = "insert into t_lendbook values(null,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, userId);
pstmt.setInt(2, bookId);
return pstmt.executeUpdate();
}
// 還書
public int returnBook(Connection con, String id) throws Exception {
String sql = "delete from t_lendbook where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
return pstmt.executeUpdate();
}
public ResultSet lendBookList(Connection con, User user, Book book)
throws Exception {
StringBuffer sb = new StringBuffer("select * from t_lendbook ");
if(user!=null && user.getId()!= -1){
sb.append(" and userid = "+user.getId());
}
if(book!=null && book.getId()!=-1){
sb.append(" and bookid ="+book.getId());
}
PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
return pstmt.executeQuery();
}
public String getPnameById(Connection con, int userId) throws Exception {
String sql = "select * from t_user where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
return rs.getString("pname");
else
return null;
}
}
提供借書,還書,讀取借書類别清單,以及輔助函數(根據使用者id擷取使用者姓名)等方法。
package cn.edu.sau.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import cn.edu.sau.dao.BookDao;
import cn.edu.sau.dao.BookTypeDao;
import cn.edu.sau.dao.UserDao;
import cn.edu.sau.model.Book;
import cn.edu.sau.model.BookType;
import cn.edu.sau.model.User;
import cn.edu.sau.util.DbUtil;
import java.awt.Window.Type;
import javax.swing.ImageIcon;
public class UserMainFrm extends JFrame {
private User user;
DbUtil dbUtil = new DbUtil();
BookDao bookDao = new BookDao();
BookTypeDao bookTypeDao = new BookTypeDao();
UserDao userDao = new UserDao();
private JPanel contentPane;
private JPanel panel;
private JTextField bookNameTxt;
private JTextField bookAuthorTxt;
private JComboBox<BookType> jcb_bookType;
private JButton jb_search;
private JTable table;
private JTextField s_idTxt;
private JButton jb_lendBook;
private JTextField s_bookNameTxt;
/**
* Launch the application.
*/
/*public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UserMainFrm frame = new UserMainFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}*/
/**
* Create the frame.
*/
public UserMainFrm(User user) {
setTitle("\u501F\u9605\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 835, 445);
this.user = user;
JOptionPane.showMessageDialog(null, "歡迎"+user.getPname()+"使用我們的系統");
init();
fillTable(new Book());
this.fillBookType("search");
this.fillBookType("modify");
}
private void fillBookType(String type) {
Connection con = null;
BookType bookType = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeList(con, new BookType());
if ("search".equals(type)) {
bookType = new BookType();
bookType.setBookTypeName("請選擇...");
bookType.setId(-1);
this.jcb_bookType.addItem(bookType);
}
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
if ("search".equals(type)) {
this.jcb_bookType.addItem(bookType);
} else if ("modify".equals(type)) {
this.jcb_bookType.addItem(bookType);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void fillTable(Book book) {
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = dbUtil.getCon();
ResultSet rs = bookDao.bookList(con, book);
while (rs.next()) {
Vector v = new Vector();
v.add(rs.getInt("id"));
v.add(rs.getString("bookName"));
v.add(rs.getString("author"));
v.add(rs.getFloat("price"));
v.add(rs.getString("bookDesc"));
v.add(rs.getString("bookTypeName"));
String state = rs.getInt("state")==1?"借閱中":"正常";
v.add(state);
dtm.addRow(v);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void init() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel = new JPanel();
panel.setLayout(null);
panel.setToolTipText("\u641C\u7D22\u6761\u4EF6");
panel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setBounds(43, 28, 732, 68);
contentPane.add(panel);
JLabel label = new JLabel("\u56FE\u4E66\u7C7B\u522B\uFF1A\r\n");
label.setBounds(410, 29, 72, 15);
panel.add(label);
JLabel label_1 = new JLabel("\u56FE\u4E66\u4F5C\u8005\uFF1A");
label_1.setBounds(209, 29, 72, 15);
panel.add(label_1);
JLabel label_2 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
label_2.setBounds(10, 29, 67, 15);
panel.add(label_2);
bookNameTxt = new JTextField();
bookNameTxt.setColumns(10);
bookNameTxt.setBounds(84, 26, 114, 21);
panel.add(bookNameTxt);
bookAuthorTxt = new JTextField();
bookAuthorTxt.setColumns(10);
bookAuthorTxt.setBounds(285, 26, 114, 21);
panel.add(bookAuthorTxt);
jb_search = new JButton("\u67E5\u8BE2");
jb_search.setIcon(new ImageIcon("D:\\java\\MyBookManager\\image\\search.png"));
jb_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Book book = new Book();
String bookName = bookNameTxt.getText();
String bookAuthor = bookAuthorTxt.getText();
BookType bookType = (BookType)jcb_bookType.getSelectedItem();
int bookTypeId = bookType.getId();
book.setBookName(bookName);
book.setBookAuthor(bookAuthor);
book.setBookTypeId(bookTypeId);
fillTable(book);
}
});
jb_search.setBounds(629, 25, 93, 23);
panel.add(jb_search);
jcb_bookType = new JComboBox<BookType>();
jcb_bookType.setBounds(472, 26, 131, 21);
panel.add(jcb_bookType);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setToolTipText("");
scrollPane.setBounds(43, 120, 732, 178);
contentPane.add(scrollPane);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row = table.getSelectedRow();
s_idTxt.setText((Integer)table.getValueAt(row, 0)+"");
s_bookNameTxt.setText(table.getValueAt(row, 1)+"");
}
});
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null, null, null},
},
new String[] {
"\u56FE\u4E66\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u56FE\u4E66\u4F5C\u8005", "\u56FE\u4E66\u4EF7\u683C", "\u56FE\u4E66\u63CF\u8FF0", "\u56FE\u4E66\u7C7B\u522B", "\u56FE\u4E66\u72B6\u6001"
}
));
table.setToolTipText("\u56FE\u4E66");
table.setSurrendersFocusOnKeystroke(true);
scrollPane.setViewportView(table);
jb_lendBook = new JButton("\u501F\u4E66");
jb_lendBook.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
String state = table.getValueAt(row,6)+"";
if("借閱中".equals(state)){
JOptionPane.showMessageDialog(null, "該書已經被借走,請選擇其他書籍");
}else{
Connection con=null;
try {
con = dbUtil.getCon();
int bookId = (Integer)table.getValueAt(row,0);
int ret = userDao.lendBook(con, user.getId(), bookId);
if(1 == ret){
fillTable(new Book());
JOptionPane.showMessageDialog(null, "借閱成功");
bookDao.bookLend(con, bookId);
}
else
JOptionPane.showMessageDialog(null, "借閱失敗");
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "借閱失敗");
e1.printStackTrace();
} finally{
try {
dbUtil.closeCon(con);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
});
jb_lendBook.setBounds(615, 322, 160, 23);
contentPane.add(jb_lendBook);
JLabel label_3 = new JLabel("\u56FE\u4E66\u7F16\u53F7\uFF1A");
label_3.setBounds(43, 326, 66, 15);
contentPane.add(label_3);
s_idTxt = new JTextField();
s_idTxt.setEnabled(false);
s_idTxt.setBounds(119, 323, 135, 21);
contentPane.add(s_idTxt);
s_idTxt.setColumns(10);
JLabel label_4 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
label_4.setBounds(340, 326, 66, 15);
contentPane.add(label_4);
s_bookNameTxt = new JTextField();
s_bookNameTxt.setEnabled(false);
s_bookNameTxt.setColumns(10);
s_bookNameTxt.setBounds(427, 323, 135, 21);
contentPane.add(s_bookNameTxt);
}
}
cn.edu.sau.view.UserMain
這裡書的狀态主要由book表的state字段決定,為0正常,為1表示被借走。
5、管理者借閱情況管理
package cn.edu.sau.view;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import cn.edu.sau.dao.BookDao;
import cn.edu.sau.dao.UserDao;
import cn.edu.sau.model.Book;
import cn.edu.sau.model.User;
import cn.edu.sau.util.DbUtil;
import cn.edu.sau.util.StringUtil;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class LendBookManageInterFrm extends JInternalFrame {
DbUtil dbUtil = new DbUtil();
UserDao userDao = new UserDao();
BookDao bookDao = new BookDao();
private JTextField userIdTxt;
private JTextField bookIdTxt;
private JTable table;
private JTextField s_idTxt;
private JTextField s_userNameTxt;
private JTextField s_bookNameTxt;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LendBookManageInterFrm frame = new LendBookManageInterFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LendBookManageInterFrm() {
setIconifiable(true);
setClosable(true);
setTitle("\u56FE\u4E66\u501F\u9605\u7BA1\u7406");
setBounds(100, 100, 691, 448);
getContentPane().setLayout(null);
init();
fillTable(null,null);
}
//填充借書記錄表格
private void fillTable(User user,Book book){
DefaultTableModel dtm = (DefaultTableModel)table.getModel();
dtm.setRowCount(0);
Connection con = null;
try {
con = dbUtil.getCon();
ResultSet rs = userDao.lendBookList(con, user, book);
while (rs.next()) {
Vector v = new Vector();
v.add(rs.getInt("id"));
v.add(userDao.getPnameById(con, rs.getInt("userid"))+"("+rs.getInt("userid")+")");
v.add(bookDao.getBookNameById(con, rs.getInt("bookid"))+"("+rs.getInt("bookid")+")");
dtm.addRow(v);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void init() {
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
panel.setToolTipText("\u641C\u7D22\u6761\u4EF6");
panel.setBounds(10, 24, 655, 58);
getContentPane().add(panel);
panel.setLayout(null);
JLabel lblid = new JLabel("\u7528\u6237Id\uFF1A");
lblid.setBounds(10, 20, 70, 15);
panel.add(lblid);
userIdTxt = new JTextField();
userIdTxt.setBounds(78, 17, 150, 21);
panel.add(userIdTxt);
userIdTxt.setColumns(10);
JLabel lblid_1 = new JLabel("\u56FE\u4E66Id\r\n\uFF1A");
lblid_1.setBounds(238, 20, 70, 15);
panel.add(lblid_1);
bookIdTxt = new JTextField();
bookIdTxt.setBounds(318, 17, 155, 21);
panel.add(bookIdTxt);
bookIdTxt.setColumns(10);
//搜尋按鈕
JButton jb_search = new JButton("\u641C\u7D22");
jb_search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection con=null;
try {
con = dbUtil.getCon();
String userId = userIdTxt.getText();
String bookId = bookIdTxt.getText();
Book book = new Book();
User user = new User();
if(StringUtil.isNotEmpty(bookId)){
book.setId(Integer.parseInt(bookId));
}
if(StringUtil.isNotEmpty(userId))
user.setId(Integer.parseInt(userId));
fillTable(user,book);
} catch (Exception e1) {
e1.printStackTrace();
}finally{
try {
dbUtil.closeCon(con);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
jb_search.setBounds(510, 16, 93, 23);
panel.add(jb_search);
panel.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblid, userIdTxt, lblid_1, bookIdTxt, jb_search}));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 92, 655, 174);
getContentPane().add(scrollPane);
//借書記錄表格
table = new JTable();
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int row = table.getSelectedRow();
s_idTxt.setText(table.getValueAt(row, 0)+"");
String userName = table.getValueAt(row, 1)+"";
s_userNameTxt.setText(userName.replaceAll("\\(\\d*\\)", ""));
String bookName = table.getValueAt(row, 2)+"";
s_bookNameTxt.setText(bookName.replaceAll("\\(\\d*\\)", ""));
}
});
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null},
},
new String[] {
"\u7F16\u53F7", "\u501F\u9605\u4EBA", "\u501F\u9605\u56FE\u4E66"
}
));
table.getColumnModel().getColumn(0).setPreferredWidth(83);
table.getColumnModel().getColumn(1).setPreferredWidth(162);
table.getColumnModel().getColumn(2).setPreferredWidth(187);
scrollPane.setViewportView(table);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(10, 276, 655, 101);
getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel label_2 = new JLabel("\u7F16\u53F7\uFF1A");
label_2.setBounds(10, 33, 60, 15);
panel_1.add(label_2);
s_idTxt = new JTextField();
s_idTxt.setBounds(91, 30, 66, 21);
panel_1.add(s_idTxt);
s_idTxt.setColumns(10);
JLabel label_3 = new JLabel("\u501F\u9605\u4EBA\uFF1A");
label_3.setBounds(195, 33, 65, 15);
panel_1.add(label_3);
s_userNameTxt = new JTextField();
s_userNameTxt.setBounds(270, 30, 132, 21);
panel_1.add(s_userNameTxt);
s_userNameTxt.setColumns(10);
JLabel label_4 = new JLabel("\u56FE\u4E66\u540D\u79F0\uFF1A");
label_4.setBounds(432, 33, 71, 15);
panel_1.add(label_4);
s_bookNameTxt = new JTextField();
s_bookNameTxt.setBounds(513, 30, 132, 21);
panel_1.add(s_bookNameTxt);
s_bookNameTxt.setColumns(10);
//還書按鈕
JButton jb_return = new JButton("\u5F52\u8FD8");
jb_return.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Connection con = null;
try {
con = dbUtil.getCon();
String id = s_idTxt.getText();
String bookName = s_bookNameTxt.getText();
if(StringUtil.isEmpty(bookName)){
JOptionPane.showMessageDialog(null, "請選擇要歸還書籍記錄");
return ;
}
userDao.returnBook(con, id);
bookDao.bookReturn(con, bookName);
fillTable(null, null);
} catch (Exception e1) {
e1.printStackTrace();
} finally{
try {
dbUtil.closeCon(con);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
jb_return.setBounds(10, 68, 635, 23);
panel_1.add(jb_return);
}
}
cn.edu.sau.view.LendBookManageInterFrm
界面提供了多級連表查詢以及還書接口。
6、資料庫設計
1)user表
2)booktype表
3)book表
4)lendbook表
根據資料庫所學知識,我們将資料庫分為4張表,user表中記錄使用者資訊;booktype表中記錄圖書分類資訊;book表中記錄圖書資訊,同時将book表中的booktypeId關聯到booktype表的id上;lendbook為借書資訊,其中userid和bookid為兩個外鍵,分别對應user表的id和book表的id。這裡應該感謝這學期的資料庫課程,讓我們在設計表結構的時候免去了不少的麻煩。
注:代碼注釋不是很詳細,傳至coding時會添加完整注釋内容以及資料庫檔案等(雖然我現在不知道這種工程應該怎麼傳送到coding T T )。
Conding位址:https://coding.net/u/2013040101068/p/alarm-work/git