JAVA專案(二)——圖書管理系統
阿新 • • 發佈:2019-02-10
1.簡介:使用java和jdbc相關知識製作的軟體,實現使用者登入,圖書管理(增加、修改、刪除),圖書類別管理(增加、修改、刪除)等功能。專案參考自:http://www.java1234.com/a/yuanchuang/swing2/ 。瞭解應實現的功能後基本獨立完成,實現過程與參考專案大同小異。不足之處歡迎討論。
2.流程:建立properties檔案--->連線工程類--->DTO(idEntity類-->實體類)--->DAO(介面-->介面的實現類)--->介面。
JDBC的DTO和DAO可參考:https://blog.csdn.net/Tsai_Hui/article/details/79750299。
3.包結構:
4.資料庫結構:
5.介面:(1)管理員登入介面
(2)主介面
(3)圖書新增介面
(4)圖書修改介面
(5)圖書類別增加
(6)圖書類別修改
(7)關於我介面
4.程式碼展示——以書籍管理為例:
(1)書籍的id類和實體類
package com.caihui.entity; public class IdEntity { private long id; public long getId() { return id; } public void setId(long id) { this.id = id; } }
package com.caihui.entity; public class Book extends IdEntity { private String bookName; private String author; private String sex; private String price; private int bookTypeId; private String bookDesc; public Book(String bookName, String author, String sex, String price, int bookTypeId, String bookDesc) { super(); this.bookName = bookName; this.author = author; this.sex = sex; this.price = price; this.bookTypeId = bookTypeId; this.bookDesc = bookDesc; } public Book(String bookName, String author, int bookTypeId) { super(); this.bookName = bookName; this.author = author; this.bookTypeId = bookTypeId; } public Book() { } 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 String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public int getBookTypeId() { return bookTypeId; } public void setBookTypeId(int bookTypeId) { this.bookTypeId = bookTypeId; } public String getBookDesc() { return bookDesc; } public void setBookDesc(String bookDesc) { this.bookDesc = bookDesc; } @Override public String toString() { return "Book [bookName=" + bookName + ", author=" + author + ", sex=" + sex + ", price=" + price + ", bookTypeId=" + bookTypeId + ", bookDesc=" + bookDesc + ", getId()=" + getId() + "]"; } }
(2)書籍的Dao類和Dao實現類
package com.caihui.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.caihui.entity.Book;
public interface BookDao {
public int save(Connection conn, Book book) throws SQLException;
public int delete(Connection conn, String id) throws SQLException;
public int update(Connection conn, String id, Book book) throws SQLException;
public ResultSet list(Connection conn, Book book) throws SQLException;
}
package com.caihui.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.caihui.dao.BookDao;
import com.caihui.entity.Book;
import com.caihui.until.StringUntil;
public class BookDaoImpl implements BookDao {
@Override
public int save(Connection conn, Book book) throws SQLException {
// TODO Auto-generated method stub
String sql = "INSERT INTO b_book(bookName,author,sex,price,bookTypeId,bookDesc) VALUES(?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, book.getBookName());
ps.setString(2, book.getAuthor());
ps.setString(3, book.getSex());
ps.setString(4, book.getPrice());
ps.setInt(5, book.getBookTypeId());
ps.setString(6, book.getBookDesc());
return ps.executeUpdate();
}
@Override
public int delete(Connection conn, String id) throws SQLException {
// TODO Auto-generated method stub
String sql = "DELETE FROM b_book WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, id);
return ps.executeUpdate();
}
@Override
public int update(Connection conn, String id, Book book) throws SQLException {
// TODO Auto-generated method stub
String sql = "UPDATE b_book SET bookName = ?,author = ?,sex = ?,price = ?,"
+ "bookTypeId = ?,bookDesc = ? WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, book.getBookName());
ps.setString(2, book.getAuthor());
ps.setString(3, book.getSex());
ps.setString(4, book.getPrice());
ps.setInt(5, book.getBookTypeId());
ps.setString(6, book.getBookDesc());
ps.setString(7, id);
return ps.executeUpdate();
}
@Override
public ResultSet list(Connection conn, Book book) throws SQLException {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer("SELECT* FROM b_book ");
if (StringUntil.isNotEmpty(book.getBookName())) {
sb.append(" and bookName like '%" + book.getBookName() + "%'");
}
if (StringUntil.isNotEmpty(book.getAuthor())) {
sb.append(" and author like '%" + book.getAuthor() + "%'");
}
sb.append(" and bookTypeId like '%" + book.getBookTypeId() + "%'");
String sql = sb.toString().replaceFirst("and", "where");
PreparedStatement ps = conn.prepareStatement(sql);
return ps.executeQuery();
}
}
(3)書籍新增的介面程式碼package com.caihui.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.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import com.caihui.dao.BookDao;
import com.caihui.dao.BookTypeDao;
import com.caihui.entity.Book;
import com.caihui.entity.BookType;
import com.caihui.impl.BookDaoImpl;
import com.caihui.impl.BookTypeDaoImpl;
import com.caihui.until.ConnectionFactory;
import com.caihui.until.StringUntil;
public class BookAddInterFrame extends JInternalFrame {
private JTextField tAddBook;
private JTextField tAddAuthor;
private JTextField tAddPrice;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JButton buttonResetBookAdd;
private JButton buttonBookAdd;
private JTextArea tAddBookDesc;
private JComboBox cbAddBookType;
private JRadioButton rbMan;
private JRadioButton rbWoman;
private Connection conn = null;
private BookTypeDao bookTypeDao = null;
private Book book = null;
private BookType bookType = null;
private BookDao bookDao = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookAddInterFrame frame = new BookAddInterFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BookAddInterFrame() {
setTitle("圖書新增");
setIconifiable(true);
setClosable(true);
setBounds(100, 100, 431, 375);
JLabel label = new JLabel("書名:");
tAddBook = new JTextField();
tAddBook.setColumns(10);
JLabel label_1 = new JLabel("作者:");
tAddAuthor = new JTextField();
tAddAuthor.setColumns(10);
JLabel label_2 = new JLabel("性別:");
rbMan = new JRadioButton("男");
buttonGroup.add(rbMan);
rbMan.setSelected(true);
rbWoman = new JRadioButton("女");
buttonGroup.add(rbWoman);
JLabel label_3 = new JLabel("價格:");
tAddPrice = new JTextField();
tAddPrice.setColumns(10);
JLabel label_4 = new JLabel("圖書類別:");
JLabel label_5 = new JLabel("圖書簡介:");
tAddBookDesc = new JTextArea();
cbAddBookType = new JComboBox();
buttonBookAdd = new JButton("新增");
buttonBookAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBookAction();
}
});
buttonResetBookAdd = new JButton("重置");
buttonResetBookAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookAddReset();
}
});
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
.createSequentialGroup().addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING).addGroup(groupLayout
.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addGroup(groupLayout.createSequentialGroup().addComponent(label_4)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(cbAddBookType,
0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup().addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(tAddBook,
GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup().addComponent(label_2)
.addPreferredGap(ComponentPlacement.UNRELATED).addComponent(rbMan)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(rbWoman)))
.addGap(31)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING).addComponent(label_3)
.addComponent(label_1))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addComponent(tAddAuthor)
.addComponent(tAddPrice, GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)))
.addGroup(groupLayout.createSequentialGroup().addComponent(label_5)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(tAddBookDesc, GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)))
.addGap(80))
.addGroup(groupLayout.createSequentialGroup().addComponent(buttonBookAdd).addGap(18)
.addComponent(buttonResetBookAdd).addGap(91)))));
groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup().addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label)
.addComponent(tAddBook, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(tAddAuthor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addGap(25)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label_2)
.addComponent(rbMan).addComponent(rbWoman).addComponent(label_3)
.addComponent(tAddPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE))
.addGap(32)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label_4)
.addComponent(cbAddBookType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE))
.addGap(29)
.addGroup(
groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label_5).addComponent(
tAddBookDesc, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
.addGap(18).addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(buttonBookAdd).addComponent(buttonResetBookAdd))
.addContainerGap(26, Short.MAX_VALUE)));
getContentPane().setLayout(groupLayout);
this.fillBookType(new BookType());
}
/**
* 新增書籍
*/
protected void addBookAction() {
// TODO Auto-generated method stub
String bookName = tAddBook.getText();
String author = tAddAuthor.getText();
String sex = "";
String price = tAddPrice.getText();
String bookDesc = tAddBookDesc.getText();
if (StringUntil.isEmpty(bookName)) {
JOptionPane.showMessageDialog(null, "圖書名不能為空");
return;
}
if (StringUntil.isEmpty(author)) {
JOptionPane.showMessageDialog(null, "作者不能為空");
return;
}
if (StringUntil.isEmpty(price)) {
JOptionPane.showMessageDialog(null, "價格不能為空");
return;
}
if (rbMan.isSelected()) {
sex = "男";
} else {
sex = "女";
}
try {
String bookTypeName = (String) cbAddBookType.getSelectedItem();
bookTypeDao = new BookTypeDaoImpl();
conn = ConnectionFactory.getInstance().getConnection();
String bookTypeIdString = bookTypeDao.searchId(conn, bookTypeName);
int bookTypeId = Integer.valueOf(bookTypeIdString);
book = new Book(bookName, author, sex, price, bookTypeId, bookDesc);
bookDao = new BookDaoImpl();
int n = bookDao.save(conn, book);
if (n == 1) {
JOptionPane.showMessageDialog(null, "圖書新增成功");
} else {
JOptionPane.showMessageDialog(null, "圖書新增失敗");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 重置書籍新增資訊
*/
protected void BookAddReset() {
// TODO Auto-generated method stub
tAddAuthor.setText(null);
tAddBook.setText(null);
tAddBookDesc.setText(null);
tAddPrice.setText(null);
rbMan.setSelected(true);
cbAddBookType.setSelectedIndex(-1);
}
/**
* 初始化圖書類別下拉框
*/
private void fillBookType(BookType bookType) {
try {
conn = ConnectionFactory.getInstance().getConnection();
bookTypeDao = new BookTypeDaoImpl();
ResultSet rs = bookTypeDao.list(conn, bookType);
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getLong("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
this.cbAddBookType.addItem(bookType.getBookTypeName());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
(4)書籍修改和刪除的介面程式碼package com.caihui.view;
import java.awt.Color;
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.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
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.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.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import com.caihui.dao.BookDao;
import com.caihui.dao.BookTypeDao;
import com.caihui.entity.Book;
import com.caihui.entity.BookType;
import com.caihui.impl.BookDaoImpl;
import com.caihui.impl.BookTypeDaoImpl;
import com.caihui.until.ConnectionFactory;
public class BookMangerInterFrame extends JInternalFrame {
private JTextField tsBookName;
private JTextField tsAuthor;
private JTable tBookList;
private JComboBox csBookTypeId;
private JButton bSearch;
private JScrollPane scrollPane;
private JButton bBookUpdate;
private JButton bBookDelete;
private Connection conn = null;
private Book book = null;
private BookDao bookDao = null;
private BookType bookType = null;
private BookTypeDao bookTypeDao = null;
private JLabel label_3;
private JLabel label_4;
private JLabel label_5;
private JTextField tlBookId;
private JTextField tlBookName;
private JTextField tlBookPrice;
private JLabel label_6;
private JTextField tlAuthor;
private JLabel label_7;
private JLabel label_8;
private JLabel label_9;
private JTextArea tlBookDesc;
private JComboBox cUpdateBookType;
private final ButtonGroup buttonGroup = new ButtonGroup();
private JRadioButton rbAddMan;
private JRadioButton rbAddWoman;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookMangerInterFrame frame = new BookMangerInterFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BookMangerInterFrame() {
setClosable(true);
setIconifiable(true);
setBounds(100, 100, 688, 509);
JLabel label = new JLabel("書名:");
JLabel label_1 = new JLabel("作者:");
JLabel label_2 = new JLabel("類別:");
tsBookName = new JTextField();
tsBookName.setColumns(10);
tsAuthor = new JTextField();
tsAuthor.setColumns(10);
csBookTypeId = new JComboBox();
bSearch = new JButton("搜 索");
bSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchBookAction();
}
});
scrollPane = new JScrollPane();
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4E66\u7C4D\u8BE6\u60C5",
TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
bBookUpdate = new JButton("修改");
bBookUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookUpdateAction();
}
});
bBookDelete = new JButton("刪除");
bBookDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bookDeleteAction();
}
});
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
.createSequentialGroup().addGap(23)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup().addComponent(bBookUpdate).addGap(18)
.addComponent(bBookDelete).addGap(14))
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addGroup(groupLayout.createSequentialGroup().addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(tsBookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addGap(27).addComponent(label_1).addPreferredGap(ComponentPlacement.RELATED)
.addComponent(tsAuthor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addGap(16).addComponent(label_2).addPreferredGap(ComponentPlacement.RELATED)
.addComponent(csBookTypeId, GroupLayout.PREFERRED_SIZE, 107,
GroupLayout.PREFERRED_SIZE)
.addGap(18).addComponent(bSearch, GroupLayout.PREFERRED_SIZE, 76,
GroupLayout.PREFERRED_SIZE))
.addComponent(scrollPane).addComponent(panel, Alignment.TRAILING,
GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap(153, Short.MAX_VALUE)));
groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup().addGap(20)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label)
.addComponent(label_1).addComponent(label_2)
.addComponent(tsBookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(tsAuthor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(csBookTypeId, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(bSearch))
.addGap(18)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE).addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(bBookDelete)
.addComponent(bBookUpdate))
.addContainerGap(30, Short.MAX_VALUE)));
label_3 = new JLabel("編號:");
label_4 = new JLabel("書名:");
label_5 = new JLabel("價格:");
tlBookId = new JTextField();
tlBookId.setEditable(false);
tlBookId.setColumns(10);
tlBookName = new JTextField();
tlBookName.setColumns(10);
tlBookPrice = new JTextField();
tlBookPrice.setColumns(10);
label_6 = new JLabel("作者:");
tlAuthor = new JTextField();
tlAuthor.setColumns(10);
label_7 = new JLabel("性別:");
label_8 = new JLabel("書籍類別:");
label_9 = new JLabel("書籍描述:");
tlBookDesc = new JTextArea();
cUpdateBookType = new JComboBox();
rbAddMan = new JRadioButton("男");
rbAddMan.setSelected(true);
buttonGroup.add(rbAddMan);
rbAddWoman = new JRadioButton("女");
buttonGroup.add(rbAddWoman);
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
.createSequentialGroup().addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false).addGroup(gl_panel
.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_panel.createSequentialGroup().addComponent(label_3)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(tlBookId))
.addGroup(gl_panel.createSequentialGroup().addComponent(label_6)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(tlAuthor,
GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
GroupLayout.PREFERRED_SIZE)))
.addGap(27)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup().addComponent(label_7)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(rbAddMan)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(rbAddWoman))
.addGroup(gl_panel.createSequentialGroup().addComponent(label_4)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(tlBookName,
GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup().addComponent(label_5)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(tlBookPrice))
.addGroup(gl_panel.createSequentialGroup().addComponent(label_8)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(cUpdateBookType,
GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE))))
.addGroup(gl_panel.createSequentialGroup().addComponent(label_9)
.addPreferredGap(ComponentPlacement.RELATED).addComponent(tlBookDesc)))
.addContainerGap(38, Short.MAX_VALUE)));
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(
gl_panel.createSequentialGroup().addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(label_3)
.addComponent(tlBookId, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_4)
.addComponent(tlBookName, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_5)
.addComponent(tlBookPrice, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(label_6)
.addComponent(tlAuthor, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(rbAddMan).addComponent(label_7).addComponent(rbAddWoman)
.addComponent(label_8)
.addComponent(cUpdateBookType, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(label_9)
.addComponent(tlBookDesc, GroupLayout.PREFERRED_SIZE, 67,
GroupLayout.PREFERRED_SIZE))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
panel.setLayout(gl_panel);
tBookList = new JTable();
tBookList.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
mousePressedAction();
}
});
tBookList.setModel(
new DefaultTableModel(new Object[][] {}, new String[] { "\u7F16\u53F7", "\u4E66\u540D", "\u4F5C\u8005",
"\u6027\u522B", "\u4EF7\u683C", "\u4E66\u7C4D\u7C7B\u522B", "\u4E66\u7C4D\u63CF\u8FF0" }) {
boolean[] columnEditables = new boolean[] { false, false, false, false, false, false, false };
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
scrollPane.setViewportView(tBookList);
getContentPane().setLayout(groupLayout);
this.fillBookType(csBookTypeId, new BookType());
this.fillBookType(cUpdateBookType, new BookType());
}
/**
* 在列表中滑鼠選擇書籍
*/
protected void mousePressedAction() {
// TODO Auto-generated method stub
int row = tBookList.getSelectedRow();
tlBookId.setText(tBookList.getValueAt(row, 0).toString());
tlBookName.setText(tBookList.getValueAt(row, 1).toString());
tlAuthor.setText(tBookList.getValueAt(row, 2).toString());
String sex = tBookList.getValueAt(row, 3).toString();
if (sex.equals("女")) {
rbAddWoman.setSelected(true);
}
tlBookPrice.setText(tBookList.getValueAt(row, 4).toString());
String bookType = tBookList.getValueAt(row, 5).toString();
int n = cUpdateBookType.getItemCount();
for (int i = 0; i < n; i++) {
cUpdateBookType.setSelectedIndex(i);
String s = cUpdateBookType.getSelectedItem().toString();
if (bookType.equals(s)) {
break;
}
}
tlBookDesc.setText(tBookList.getValueAt(row, 6).toString());
}
/**
* 刪除書籍
*/
protected void bookDeleteAction() {
// TODO Auto-generated method stub
try {
conn = ConnectionFactory.getInstance().getConnection();
String id = tlBookId.getText();
bookDao = new BookDaoImpl();
int n = bookDao.delete(conn, id);
if (n == 1) {
JOptionPane.showMessageDialog(null, "書籍刪除成功");
} else {
JOptionPane.showMessageDialog(null, "書籍刪除失敗");
}
this.BookAddReset();
this.searchBookAction();
} catch (SQLException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "書籍刪除失敗");
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 書籍資訊更新
*/
protected void bookUpdateAction() {
// TODO Auto-generated method stub
String id = tlBookId.getText();
String bookName = tlBookName.getText();
String author = tlAuthor.getText();
String sex = "男";
if (rbAddWoman.isSelected()) {
sex = "女";
}
String price = tlBookPrice.getText();
String bookTypeName = cUpdateBookType.getSelectedItem().toString();
String bookDesc = tlBookDesc.getText();
try {
bookTypeDao = new BookTypeDaoImpl();
bookDao = new BookDaoImpl();
conn = ConnectionFactory.getInstance().getConnection();
String bookTypeIdString = bookTypeDao.searchId(conn, bookTypeName);
int bookTypeId = Integer.valueOf(bookTypeIdString);
book = new Book(bookName, author, sex, price, bookTypeId, bookDesc);
int n = bookDao.update(conn, id, book);
if (n == 1) {
JOptionPane.showMessageDialog(null, "書籍更新成功");
} else {
JOptionPane.showMessageDialog(null, "書籍更新失敗");
}
this.BookAddReset();
this.searchBookAction();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 查詢書籍
*/
private void searchBookAction() {
DefaultTableModel dtm = (DefaultTableModel) tBookList.getModel();
dtm.setRowCount(0);
String bookName = tsBookName.getText();
String author = tsAuthor.getText();
try {
conn = ConnectionFactory.getInstance().getConnection();
bookDao = new BookDaoImpl();
bookTypeDao = new BookTypeDaoImpl();
String bookTypeName = (String) csBookTypeId.getSelectedItem();
String bookTypeIdString = bookTypeDao.searchId(conn, bookTypeName);
int bookTypeId = Integer.valueOf(bookTypeIdString);
book = new Book(bookName, author, bookTypeId);
ResultSet rs = bookDao.list(conn, book);
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.getString("price"));
v.add(bookTypeDao.searchTypeName(conn, rs.getString("bookTypeId")));
v.add(rs.getString("bookDesc"));
dtm.addRow(v);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 初始化下拉表
*
* @param bookType
*/
private void fillBookType(JComboBox cJComboBox, BookType bookType) {
try {
conn = ConnectionFactory.getInstance().getConnection();
bookTypeDao = new BookTypeDaoImpl();
ResultSet rs = bookTypeDao.list(conn, bookType);
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getLong("id"));
bookType.setBookTypeName(rs.getString("bookTypeName"));
cJComboBox.addItem(bookType.getBookTypeName());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 重置詳情框
*/
protected void BookAddReset() {
// TODO Auto-generated method stub
tlAuthor.setText(null);
tlBookDesc.setText(null);
tlBookId.setText(null);
tlBookName.setText(null);
tlBookPrice.setText(null);
cUpdateBookType.setSelectedIndex(-1);
rbAddMan.setSelected(true);
}
}
(5)工具類(空字元判斷類,連線工廠類)
package com.caihui.until;
public class StringUntil {
public static boolean isEmpty(String s) {
if (s == null || "".equals(s.trim())) {
return true;
} else {
return false;
}
}
public static boolean isNotEmpty(String s) {
if (s != null && !"".equals(s.trim())) {
return true;
} else {
return false;
}
}
}
package com.caihui.until;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionFactory {
private static String driver;
private static String dburl;
private static String user;
private static String password;
private static final ConnectionFactory factory = new ConnectionFactory();
Connection conn = null;
static {
Properties prop = new Properties();
try {
InputStream in = ConnectionFactory.class.getClassLoader().getResourceAsStream("dbconfig.properties");// 啟動類的載入器
prop.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("===屬性配製失敗===");
}
driver = prop.getProperty("driver");
dburl = prop.getProperty("dburl");
user = prop.getProperty("user");
password = prop.getProperty("password");
}
private ConnectionFactory() {
}
public static ConnectionFactory getInstance() {
return factory;
}
/**
* 開啟資料庫連線
*/
public Connection getConnection() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(dburl, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 關閉資料庫連線 因為connection佔用資源較多
*/
public void closeConnection() throws Exception {
if (conn != null) {
conn.close();
}
}
}