5、图书类别查询功能
生活随笔
收集整理的這篇文章主要介紹了
5、图书类别查询功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、添加查詢的Dao方法
在BookTypeDao添加代碼
/*** 圖書類別Dao類* @author Administrator**/ public class BookTypeDao {/*** 圖書類別添加* @param con* @param bookType* @return* @throws Exception*/public static int add(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();//執行}-----------------新添加--------------------/*** 查詢圖書類別集合* @param con* @param bookType* @return* @throws Exception*/public ResultSet list(Connection con,BookType bookType)throws Exception{StringBuffer sb=new StringBuffer("select * from t_bookType");//條件是動態的,用拼接的方式//如果不為空,動態添加if(StringUtil.isNotEmpty(bookType.getBookTypeName())){sb.append(" and bookTypeName like'%"+bookType.getBookTypeName()+"%'");}PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));//先轉換成字符串,調用字符串方法把and換位wherereturn pstmt.executeQuery();} --------------------------------------------------------------------------------- }2、新建圖書類別查詢界面BookTypeManageInterFrm.java
更改布局方式
添加JScrollPane組件
JScrollPane組件上添加JTable組件
修改model屬性,添加表格
3、初始化JTable,把數據寫入表格里
重命名table的Variable為bookTypeTable
在BookTypeManageInterFrm里添加fillTable方法
調用fillTable方法
this.fillTable(new BookType());4、在MainFrm.java的“圖書類別-修改”添加事件
public void actionPerformed(ActionEvent arg0) {BookTypeManageInterFrm bookTypeManageInterFrm=new BookTypeManageInterFrm();bookTypeManageInterFrm.setVisible(true);table.add(bookTypeManageInterFrm);}6、實現查詢功能
添加JLabel,JTextField,JButton組件
重命名JTextField組件為s_bookTypeNameTxt
添加“查詢”事件
public void actionPerformed(ActionEvent e) {bookTypeSearchActionPerformed(e);} //圖書類別搜索事件處理 private void bookTypeSearchActionPerformed(ActionEvent evt) {String s_bookTypeName=this.s_bookTypeNameTxt.getText();BookType bookType=new BookType();bookType.setBookTypeName(s_bookTypeName);this.fillTable(bookType);}總結
以上是生活随笔為你收集整理的5、图书类别查询功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4、图书类别添加功能
- 下一篇: 6、图书类别修改删除功能