日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

6、图书类别修改删除功能

發布時間:2025/3/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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添加邊框

//設置文本域邊框bookTypeDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));

3、實現點擊圖書類別,表單顯示類別的詳細信息的功能

給bookTypeTable添加鼠標點擊事件

@Override public void mousePressed(MouseEvent e) {bookTypeTableMousePressed(e); } /*** 表格行點擊事件處理* @param e*/private void bookTypeTableMousePressed(MouseEvent evt) {int row=bookTypeTable.getSelectedRow();//獲取選中行,返回行號idTxt.setText((String)bookTypeTable.getValueAt(row, 0));//獲取第幾行,第幾列的值bookTypeNameTxt.setText((String)bookTypeTable.getValueAt(row, 1));bookTypeDescTxt.setText((String)bookTypeTable.getValueAt(row, 2));}

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、图书类别修改删除功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。