6、图书类别修改删除功能
生活随笔
收集整理的這篇文章主要介紹了
6、图书类别修改删除功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現圖書類別修改功能
1、添加修改的Dao方法
/*** 圖書類別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();} -----------------------------------------新添加---------------------------------- /*** 刪除圖書類別* @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();}/*** 修改圖書類別* @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的屬性,改為TitledBorder。修改布局為GroupLayout
在JPanel組件里添加JLabel,JTextField,JButton,JTextArea組件
把顯示id的JTextField設置為不可修改,重命名為idTxt
把顯示圖書類別名稱的JTextField重命名為bookTypeNameTxt
把顯示描述的JTextField重命名為bookTypeDescTxt
給描述的JTextField添加邊框
3、實現點擊圖書類別,表單顯示類別的詳細信息的功能
給bookTypeTable添加鼠標點擊事件
4、實現修改按鈕功能
修改按鈕添加事件
public void actionPerformed(ActionEvent e) {bookTypeUpdateActionEvent(e);}封裝一個方法,用來重置數據
/*** 重置表單*/private void resetValue(){this.idTxt.setText("");this.bookTypeNameTxt.setText("");this.bookTypeDescTxt.setText("");} /*** 圖書類別修改事件處理* @param evt*/private void bookTypeUpdateActionEvent(ActionEvent evt) {//獲取值String id=idTxt.getText();String bookTypeName=bookTypeNameTxt.getText();String bookTypeDesc=bookTypeDescTxt.getText();//判斷id,用戶什么都沒點,不可修改if(StringUtil.isEmpty(id)){JOptionPane.showMessageDialog(null, "請選擇一個圖書類!");}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();//修改成功后,重置數據this.fillTable(new BookType());//刷新表格table的數據}else{JOptionPane.showMessageDialog(null, "修改失敗");}}catch(Exception e){e.printStackTrace();}finally{try {dbUtil.close(con);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
5、實現圖書類別刪除功能
新建一個刪除按鈕事件
public void actionPerformed(ActionEvent e) {bookTypeDeleteActionEvent(e);} /*** 圖書類別刪除事件處理* @param e*/private void bookTypeDeleteActionEvent(ActionEvent evt) {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 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();}}}}
總結
以上是生活随笔為你收集整理的6、图书类别修改删除功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5、图书类别查询功能
- 下一篇: 7、图书添加功能