6、图书类别修改删除功能
生活随笔
收集整理的這篇文章主要介紹了
6、图书类别修改删除功能
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
實(shí)現(xiàn)圖書(shū)類(lèi)別修改功能
1、添加修改的Dao方法
/*** 圖書(shū)類(lèi)別Dao類(lèi)* @author Administrator**/ public class BookTypeDao {/*** 圖書(shū)類(lèi)別添加* @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();//執(zhí)行}/*** 查詢(xún)圖書(shū)類(lèi)別集合* @param con* @param bookType* @return* @throws Exception*/public ResultSet list(Connection con,BookType bookType)throws Exception{StringBuffer sb=new StringBuffer("select * from t_bookType");//條件是動(dòng)態(tài)的,用拼接的方式//如果不為空,動(dòng)態(tài)添加if(StringUtil.isNotEmpty(bookType.getBookTypeName())){sb.append(" and bookTypeName like'%"+bookType.getBookTypeName()+"%'");}PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));//先轉(zhuǎn)換成字符串,調(diào)用字符串方法把a(bǔ)nd換位wherereturn pstmt.executeQuery();} -----------------------------------------新添加---------------------------------- /*** 刪除圖書(shū)類(lèi)別* @param con* @param id* @return* @throws Exception*/public int delete(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();}/*** 修改圖書(shū)類(lèi)別* @param con* @param bookType* @return* @throws Exception*/public int update(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();}--------------------------------------------------------------------------------- }2、在BookTypeManageInterFrm添加修改界面
添加JPanel組件:修改border的屬性,改為T(mén)itledBorder。修改布局為GroupLayout
在JPanel組件里添加JLabel,JTextField,JButton,JTextArea組件
把顯示id的JTextField設(shè)置為不可修改,重命名為idTxt
把顯示圖書(shū)類(lèi)別名稱(chēng)的JTextField重命名為bookTypeNameTxt
把顯示描述的JTextField重命名為bookTypeDescTxt
給描述的JTextField添加邊框
3、實(shí)現(xiàn)點(diǎn)擊圖書(shū)類(lèi)別,表單顯示類(lèi)別的詳細(xì)信息的功能
給bookTypeTable添加鼠標(biāo)點(diǎn)擊事件
4、實(shí)現(xiàn)修改按鈕功能
修改按鈕添加事件
public void actionPerformed(ActionEvent e) {bookTypeUpdateActionEvent(e);}封裝一個(gè)方法,用來(lái)重置數(shù)據(jù)
/*** 重置表單*/private void resetValue(){this.idTxt.setText("");this.bookTypeNameTxt.setText("");this.bookTypeDescTxt.setText("");} /*** 圖書(shū)類(lèi)別修改事件處理* @param evt*/private void bookTypeUpdateActionEvent(ActionEvent evt) {//獲取值String id=idTxt.getText();String bookTypeName=bookTypeNameTxt.getText();String bookTypeDesc=bookTypeDescTxt.getText();//判斷id,用戶(hù)什么都沒(méi)點(diǎn),不可修改if(StringUtil.isEmpty(id)){JOptionPane.showMessageDialog(null, "請(qǐng)選擇一個(gè)圖書(shū)類(lèi)!");}BookType bookType=new BookType(Integer.parseInt(id),bookTypeName,bookTypeDesc);Connection con=null;try{con=dbUtil.getCon();int modifyNum=bookTypeDao.update(con, bookType);if(modifyNum==1){JOptionPane.showMessageDialog(null, "修改成功");this.resetValue();//修改成功后,重置數(shù)據(jù)this.fillTable(new BookType());//刷新表格table的數(shù)據(jù)}else{JOptionPane.showMessageDialog(null, "修改失敗");}}catch(Exception e){e.printStackTrace();}finally{try {dbUtil.close(con);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
5、實(shí)現(xiàn)圖書(shū)類(lèi)別刪除功能
新建一個(gè)刪除按鈕事件
public void actionPerformed(ActionEvent e) {bookTypeDeleteActionEvent(e);} /*** 圖書(shū)類(lèi)別刪除事件處理* @param e*/private void bookTypeDeleteActionEvent(ActionEvent evt) {String id=idTxt.getText();if(StringUtil.isEmpty(id)){JOptionPane.showMessageDialog(null, "請(qǐng)選擇要?jiǎng)h除的記錄");return;}int n=JOptionPane.showConfirmDialog(null, "是否刪除該記錄");if(n==0){Connection con=null;try{con=dbUtil.getCon();int deleteNum=bookTypeDao.delete(con, id);if(deleteNum==1){JOptionPane.showMessageDialog(null, "刪除成功");this.resetValue();this.fillTable(new BookType());}else{JOptionPane.showMessageDialog(null, "刪除失敗");}}catch(Exception e){e.printStackTrace();}finally{try {dbUtil.close(con);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
總結(jié)
以上是生活随笔為你收集整理的6、图书类别修改删除功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 5、图书类别查询功能
- 下一篇: 7、图书添加功能