Swing学习总结 花了差不多四天时间学习了Swing做的图书管理系统,系统虽然不大,但对我来说做起来还是有一定困难的。
小锋老师讲的还是很不错的,重点、难点都讲得还是很清楚,条理很清晰。 通过视频学习我也受益匪浅。
对于一个系统的开发过程我有了更清楚的认识,不管多大的系统都要思路清晰,都要一步一步来,不管是从DAO层开始,还是 从View层开始,从每一个功能开始,一步一步地实现。 对于JDBC我有更深的理解,增、删、改、查操作更熟练
DAO层的方法根据什么判断返回类型?应该怎么去写?
DAO层主要是操作数据的。。 加入是添加,更新,删除 返回的是Int 实际操作数据条数 如果是查询 返回的是List集合,或者ResultSet
1.添加数据
String sql = "insert into t_book values(null,?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getBookName());
........
return pstmt.executeUpdate();
2.删除数据
public int bookDelete(Connection conn,String id) throws SQLException{ String sql = "delete from t_bookType where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id); return pstmt.executeUpdate(); }
3.修改数据 public int modifyBookType(Connection conn,BookType bookType) throws SQLException{
String sql = "update t_bookType set bookType = ? , bookTypeDesc = ? where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, bookType.getBookType());
pstmt.setString(2, bookType.getBookTypeDesc());
pstmt.setInt(3, bookType.getId());
return pstmt.executeUpdate(); }
4.查询数据 public ResultSet bookReseach(Connection conn,Book book) throws SQLException{
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.autor like '%"+book.getAuthor()+"'"); }
if(StringUtil.isNotEmpty(book.getSex())){
sb.append(" and b.sex = '"+book.getSex()+"'"); }
if(book.getBookTypeId()!=-1){
sb.append(" and b.bookTypeId ="+book.getBookTypeId()); }
PreparedStatement pstmt = conn.prepareStatement(sb.toString());
return pstmt.executeQuery(); }
///条件查询
StringBuffer sb = new StringBuffer("select * from t_bookType");
if(StringUtil.isNotEmpty(bookType.getBookType())){
sb.append(" and bookType like '%"+bookType.getBookType()+"%'"); }
PreparedStatement pstmt = conn.prepareStatement(sb.toString().replace("and", "where"));
return pstmt.executeQuery();
//这个算法不错,非常感谢
5.向JTable中填数据
private void fillBookTable(Book book) {
DefaultTableModel dtm = (DefaultTableModel) tabBook.getModel();
dtm.setRowCount(0); Connection conn = null;
try { conn = dbUtil.getCon();
ResultSet rs = bookDao.bookReseach(conn, book);
while (rs.next()) { Vector vector = new Vector();
vector.add(rs.getInt("id"));
vector.add(rs.getString("bookName"));
vector.add(rs.getString("autor"));
vector.add(rs.getString("sex"));
vector.add(rs.getString("bookDesc"));
vector.add(rs.getString("bookType"));
vector.add(rs.getFloat("price"));
dtm.addRow(vector); } } catch (SQLException e) { e.printStackTrace(); } finally { try { dbUtil.close(conn); } catch (SQLException e) { e.printStackTrace(); } } }
6.控件Combo box中item项中填数据
private void fillBookType() { Connection conn = null;
BookType bookType = null;
try { conn = dbUtil.getCon();
ResultSet rs = bookTypeDao.bookTypeResearch(conn, new BookType());
bookType = new BookType();
bookType.setBookType("请选择...");
bookType.setId(-1);
this.jcbBookType.addItem(bookType);
while (rs.next()) {
bookType = new BookType();
bookType.setId(rs.getInt("id"));
bookType.setBookType(rs.getString("bookType"));
this.jcbBookType.addItem(bookType); } } catch (SQLException e) { e.printStackTrace(); } finally { try { dbUtil.close(conn); } catch (SQLException e) { e.printStackTrace(); } }
}
7.JTable中Mouse的mousePressed事件
private void tabBookTypeMousePressed(java.awt.event.MouseEvent evt) {
int row = tabBookType.getSelectedRow();
this.txtId.setText((String) tabBookType.getValueAt(row, 0));
this.txtBookT.setText((String) tabBookType.getValueAt(row, 1));
this.txtBookTypeDesc.setText((String) tabBookType.getValueAt(row, 2)); }
8.Combo box读数据并显示bookType
String bookType = (String) tabBook.getValueAt(row, 5);
int n = this.jcbBookType.getItemCount();
for (int i = 0; i < n; i++) {
BookType item = (BookType) this.jcbBookType.getItemAt(i);
if (item.getBookType().equals(bookType)) {
this.jcbBookT.setSelectedIndex(i); } }
9.JRadioButton读数据据并显示
String sex = (String) tabBook.getValueAt(row, 3);
if ("男".equals(sex)) {
this.jrbMan1.setSelected(true); }
else if ("女".equals(sex)) {
this.jrbWoman1.setSelected(true); }
在跟着练习的过程中也遇到很多问题,比如:空指针异常、越界等问题。 我也学会了解决问题方法,对于这些问题,要进入Debug模式一步一步去调试,看变量的值,从而找到问题关键。
还有就是学会了“偷懒的方法”,对于一些经常要用的方法进行封装重复利用
1.对于数据库注册、连接、关闭封装成DbUtil类
2.对于重置方法:
private void resetValue() {
this.txtId.setText("");
this.txtAutor.setText("");
this.txtBookDesc.setText("");
this.txtBook.setText("");
this.txtPrice.setText("");
this.jrbMan1.setSelected(true);
if(this.jcbBookT.getItemCount()>0){ this.jcbBookT.setSelectedIndex(0); } }
第一次写blog,写得不好,新人莫怪!
最后感谢小锋老师,希望在老师带领下学的更多的知识!