9、图书修改,删除功能
生活随笔
收集整理的這篇文章主要介紹了
9、图书修改,删除功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、添加圖書修改,刪除的Dao代碼
/*** 圖書Dao類* @author Administrator**/ public class BookDao {/*** 圖書添加* @param con* @param book* @return* @throws Exception */public int add(Connection con,Book book) throws Exception{String sql="insert into t_book values(null,?,?,?,?,?,?)";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, book.getBookName());pstmt.setString(2, book.getAuthor());pstmt.setString(3, book.getSex());pstmt.setFloat(4, book.getPrice());pstmt.setInt(5, book.getBookTypeId());pstmt.setString(6, book.getBookDesc());pstmt.setInt(7, book.getId());return pstmt.executeUpdate();}/*** 圖書信息查詢* @param con* @param book* @return* @throws Exception*/public ResultSet list(Connection con,Book book)throws Exception{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.author like '%"+book.getAuthor()+"%'");}if(book.getBookTypeId()!=null && book.getBookTypeId()!=-1){sb.append(" and b.bookTypeId="+book.getBookTypeId());}PreparedStatement pstmt=con.prepareStatement(sb.toString());return pstmt.executeQuery();} ----------------------------------------------------------------------------------- /*** 圖書信息刪除* @param con* @param id* @return* @throws Exception*/public int delete(Connection con,String id)throws Exception{String sql="delete from t_book where id=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, id);return pstmt.executeUpdate();}/*** 產品信息修改* @param con* @param book* @return* @throws Exception*/public int update(Connection con,Book book)throws Exception{String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=? ";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, book.getBookName());pstmt.setString(2, book.getAuthor());pstmt.setString(3, book.getSex());pstmt.setFloat(4, book.getPrice());pstmt.setString(5, book.getBookDesc());pstmt.setInt(6, book.getBookTypeId());return pstmt.executeUpdate();}---------------------------------------------------------------------------- }2、在“圖書管理-修改”窗體新建圖書修改刪除組件
在BookManageInterFrm窗體上添加JLabel,JTextField,JComboBox,JTextArea,JButtonJRadioButton組件
重命名組件:
實現bookTable的行點擊事件
調用方法:
this.fillBookType("modify");修改fillBookType方法,為modify時:
/*** 初始化下拉框*/private void fillBookType(String type){Connection con=null;BookType bookType=null;try{con=dbUtil.getCon();ResultSet rs=bookTypeDao.list(con, new BookType());if("search".equals(type)){bookType=new BookType();bookType.setBookTypeName("請選擇");bookType.setId(-1);this.s_bookTypeJcb.addItem(bookType);}while(rs.next()){bookType=new BookType();bookType.setBookTypeName(rs.getString("bookTypeName"));bookType.setId(rs.getInt("id"));if("search".equals(type)){this.s_bookTypeJcb.addItem(bookType);}else if("modify".equals(type)){ -----------------------------------------------------------------------------------this.bookTypeJcb.addItem(bookType); ------------------------------------------------------------------------------------------}}}catch(Exception e){e.printStackTrace();}finally{try {dbUtil.close(con);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} /*** 表格行點擊處理* @param e*/private void bookTableMousePressed(MouseEvent e) {int row=this.bookTable.getSelectedRow();//獲取選中的行this.idTxt.setText(bookTable.getValueAt(row, 0)+"");this.bookNameTxt.setText((String)bookTable.getValueAt( row, 1));this.authorTxt.setText((String)bookTable.getValueAt(row, 2));String sex=(String)bookTable.getValueAt(row, 3);if("男".equals(sex)){this.manJrb.setSelected(true);}else if("女".equals(sex)){this.femaleJrb.setSelected(true);}this.priceTxt.setText((Float)bookTable.getValueAt(row, 4)+"");this.bookDescTxt.setText((String)bookTable.getValueAt(row, 5));String bookTypeName=(String)this.bookTable.getValueAt(row, 6);//遍歷下拉框int n=this.bookTypeJcb.getItemCount();for(int i=0;i<n;i++){BookType item=(BookType)this.bookTypeJcb.getItemAt(i);if(item.getBookTypeName().equals(bookTypeName)){this.bookTypeJcb.setSelectedIndex(i);}}}3、實現修改功能:
修改按鈕新建事件
public void actionPerformed(ActionEvent e) {bookUpdateActionPerformed(e); }封裝一個重置表單的方法
/*** 重置表單*/private void resetValue() {this.idTxt.setText("");this.bookNameTxt.setText("");this.authorTxt.setText("");this.priceTxt.setText("");this.manJrb.setSelected(true);this.bookDescTxt.setText("");if(this.bookTypeJcb.getItemCount()>0){this.bookTypeJcb.setSelectedIndex(0);}} /*** 圖書修改事件處理*/ private void bookUpdateActionPerformed(ActionEvent evt) {String id=this.idTxt.getText();if(StringUtil.isEmpty(id)){JOptionPane.showMessageDialog(null, "請選擇圖書!");return;}String bookName=this.bookNameTxt.getText();String author=this.authorTxt.getText();String price=this.priceTxt.getText();String bookDesc=this.bookDescTxt.getText();if(StringUtil.isEmpty(bookName)){JOptionPane.showMessageDialog(null, "圖書名為空!");return;}if(StringUtil.isEmpty(author)){JOptionPane.showMessageDialog(null, "作者為空!");return;}if(StringUtil.isEmpty(price)){JOptionPane.showMessageDialog(null, "價格為空!");return;}String sex="";if(manJrb.isSelected()){sex="男";}else if(femaleJrb.isSelected()){sex="女";}//獲取下拉框BookType bookType=(BookType)bookTypeJcb.getSelectedItem();int bookTypeId=bookType.getId();Book book=new Book(Integer.parseInt(id),bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc);Connection con=null;try{con=dbUtil.getCon();int addNum=bookDao.update(con, book);if(addNum==1){JOptionPane.showMessageDialog(null, "修改成功!");resetValue();this.fillTable(new Book());//刷新表單}else{JOptionPane.showMessageDialog(null, "修改失敗!");}}catch(Exception e){e.printStackTrace();JOptionPane.showMessageDialog(null, "修改失敗!");}finally{try {dbUtil.close(con);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }4、實現刪除功能
刪除按鈕新建事件
public void actionPerformed(ActionEvent e) {bookDeleteActionPerformed(e);} /*** 圖書刪除事件處理* @param e*/private void bookDeleteActionPerformed(ActionEvent evt) {String id=this.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=bookDao.delete(con, id);if(deleteNum==1){JOptionPane.showMessageDialog(null, "刪除成功!");resetValue();this.fillTable(new Book());}else{JOptionPane.showMessageDialog(null, "刪除失敗!");}}catch(Exception e){e.printStackTrace();JOptionPane.showMessageDialog(null, "刪除失敗!");}finally{try {dbUtil.close(con);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}5、刪除圖書類別時做判斷,圖書類別下是否存在圖書
/*** 圖書Dao類* @author Administrator**/ public class BookDao {/*** 圖書添加* @param con* @param book* @return* @throws Exception */public int add(Connection con,Book book) throws Exception{String sql="insert into t_book values(null,?,?,?,?,?,?)";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, book.getBookName());pstmt.setString(2, book.getAuthor());pstmt.setString(3, book.getSex());pstmt.setFloat(4, book.getPrice());pstmt.setInt(5, book.getBookTypeId());pstmt.setString(6, book.getBookDesc());return pstmt.executeUpdate();}/*** 圖書信息查詢* @param con* @param book* @return* @throws Exception*/public ResultSet list(Connection con,Book book)throws Exception{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.author like '%"+book.getAuthor()+"%'");}if(book.getBookTypeId()!=null && book.getBookTypeId()!=-1){sb.append(" and b.bookTypeId="+book.getBookTypeId());}PreparedStatement pstmt=con.prepareStatement(sb.toString());return pstmt.executeQuery();}/*** 圖書信息刪除* @param con* @param id* @return* @throws Exception*/public int delete(Connection con,String id)throws Exception{String sql="delete from t_book where id=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, id);return pstmt.executeUpdate();}/*** 產品信息修改* @param con* @param book* @return* @throws Exception*/public int update(Connection con,Book book)throws Exception{String sql="update t_book set bookName=?,author=?,sex=?,price=?,bookDesc=?,bookTypeId=? where id=? ";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, book.getBookName());pstmt.setString(2, book.getAuthor());pstmt.setString(3, book.getSex());pstmt.setFloat(4, book.getPrice());pstmt.setString(5, book.getBookDesc());pstmt.setInt(6, book.getBookTypeId());pstmt.setInt(7, book.getId());return pstmt.executeUpdate();} -------------------------------------------------------------------------------- /*** 指定圖書類別下是否存在圖書* @param con* @param bookTypeId* @return* @throws Exception*/public boolean existBookByBookTypeId(Connection con,String bookTypeId)throws Exception{String sql="select * from t_book where bookTypeId=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, bookTypeId);ResultSet rs=pstmt.executeQuery();return rs.next();} ----------------------------------------------------------------------------- }在BookTypeManageInterFrm 圖書類別刪除事件處理里加判斷
/*** 圖書類別刪除事件處理* @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(); ----------------------------------------------------------------------------------------boolean flag=bookDao.existBookByBookTypeId(con, id);if(flag){JOptionPane.showMessageDialog(null, "當前圖書類存在圖書,不可刪除");return;} ---------------------------------------------------------------------------------------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();}}}}總結
以上是生活随笔為你收集整理的9、图书修改,删除功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 8、图书查询功能
- 下一篇: 1、CSS样式及其基本语法