Java Web應(yīng)用開發(fā)技術(shù)實(shí)驗(yàn)指導(dǎo)書 實(shí)驗(yàn)3:數(shù)據(jù)庫(kù)操作_第1頁(yè)
Java Web應(yīng)用開發(fā)技術(shù)實(shí)驗(yàn)指導(dǎo)書 實(shí)驗(yàn)3:數(shù)據(jù)庫(kù)操作_第2頁(yè)
Java Web應(yīng)用開發(fā)技術(shù)實(shí)驗(yàn)指導(dǎo)書 實(shí)驗(yàn)3:數(shù)據(jù)庫(kù)操作_第3頁(yè)
Java Web應(yīng)用開發(fā)技術(shù)實(shí)驗(yàn)指導(dǎo)書 實(shí)驗(yàn)3:數(shù)據(jù)庫(kù)操作_第4頁(yè)
Java Web應(yīng)用開發(fā)技術(shù)實(shí)驗(yàn)指導(dǎo)書 實(shí)驗(yàn)3:數(shù)據(jù)庫(kù)操作_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

任務(wù)四新聞發(fā)布系統(tǒng)-訪問數(shù)據(jù)庫(kù)實(shí)驗(yàn)3數(shù)據(jù)庫(kù)操作實(shí)驗(yàn)?zāi)康模?.掌握數(shù)據(jù)庫(kù)新聞數(shù)據(jù)表增刪改查的方法;掌握數(shù)據(jù)庫(kù)新聞?lì)悇e數(shù)據(jù)表增刪改查的方法(自行補(bǔ)充);掌握數(shù)據(jù)庫(kù)用戶數(shù)據(jù)庫(kù)增刪改查的方法(自行補(bǔ)充)實(shí)驗(yàn)步驟:新建NewsDao接口publicinterfaceNewsDao{ publicList<News>search();//查詢所有新聞 publicList<News>search(intt_id);//根據(jù)新聞?lì)悇eID查詢新聞 publicList<News>searchByNtitle(Stringn_title);//根據(jù)新聞標(biāo)題的關(guān)鍵字查詢新聞 publicList<News>searchByNcontent(Stringn_content);//根據(jù)新聞內(nèi)容的關(guān)鍵字查詢新聞 publicNewssearchByNid(intn_id);//根據(jù)新聞ID查詢新聞 publicbooleanadd(Newsnews);//添加新聞 publicbooleandelete(intid);//根據(jù)新聞ID刪除新聞 publicbooleanupdate(Newsnews);//修改新聞}新建NewsDao接口的實(shí)現(xiàn)類NewsDaoImplpublicclassNewsDaoImplextendsBaseDaoimplementsNewsDao{}接口實(shí)現(xiàn)類NewsDaoImpl實(shí)現(xiàn)NewsDao接口中所有的抽象方法publicList<News>search(){//查詢所有新聞 List<News>list=newArrayList(); Stringsql="select*fromnrc_news"; try{ super.getConnection(); pstm=con.prepareStatement(sql); rs=pstm.executeQuery(); while(rs.next()){ Newsnews=newNews(); news.setN_id(rs.getInt("n_id")); news.setN_title(rs.getString("n_title")); news.setN_content(rs.getString("n_content")); news.setT_id(rs.getInt("t_id")); news.setN_publishtime(rs.getString("n_publishtime"));news.setN_source(rs.getString("n_source")); list.add(news); } }catch(Exceptione){ e.printStackTrace(); }finally{ super.closeAll(); } returnlist; }//根據(jù)新聞?lì)悇eID查詢新聞 publicListsearch(intt_id){ Listlist=newArrayList(); Stringsql="select*fromnrc_newswheret_id=?"; try{ super.getConnection(); pstm=con.prepareStatement(sql); pstm.setInt(1,t_id); rs=pstm.executeQuery(); while(rs.next()){ Newsnews=newNews(); news.setN_id(rs.getInt(1)); news.setN_title(rs.getString(2)); news.setN_content(rs.getString(3)); news.setT_id(rs.getInt(4)); news.setN_publishtime(rs.getString(5));news.setN_source(rs.getString("n_source")); list.add(news); } }catch(Exceptione){ e.printStackTrace(); }finally{ super.closeAll(); } returnlist; } //根據(jù)新聞ID查詢新聞publicNewssearchByNid(intn_id){ Newsnews=null; Stringsql="select*fromnrc_newswheren_id=?"; try{ super.getConnection(); pstm=con.prepareStatement(sql); pstm.setInt(1,n_id); rs=pstm.executeQuery(); if(rs.next()){ Stringn_title=rs.getString("n_title"); Stringn_content=rs.getString("n_content"); intt_id=rs.getInt("t_id"); Stringn_publishtime=rs.getString("n_publishtime");Stringn_source=rs.getString("n_source"); news=newNews(n_id,n_title,n_content,t_id,n_publishtime,n_source); } }catch(Exceptione){ e.printStackTrace(); }finally{ closeAll(); } returnnews; }//根據(jù)新聞標(biāo)題模糊查詢新聞 publicListsearchByNtitle(Stringn_title){ Listlist=newArrayList(); Stringsql="SELECT*FROMnrc_newsWHEREN_TITLELIKE?ORDERBYN_IDDESC"; try{ super.getConnection(); pstm=con.prepareStatement(sql); pstm.setString(1,"%"+n_title+"%"); rs=pstm.executeQuery(); while(rs.next()){ Newsnews=newNews(); news.setN_id(rs.getInt("n_id")); news.setN_title(rs.getString("n_title")); news.setN_content(rs.getString("n_content")); news.setT_id(rs.getInt("t_id")); news.setN_publishtime(rs.getString("n_publishtime")); news.setN_source(rs.getString("n_source"));list.add(news); } }catch(Exceptione){ e.printStackTrace(); }finally{ super.closeAll(); } returnlist; }//根據(jù)新聞內(nèi)容模糊查詢新聞 publicListsearchByNcontent(Stringn_content){ Listlist=newArrayList(); Stringsql="SELECT*FROMnrc_newsWHEREN_CONTENTLIKE?ORDERBYN_IDDESC"; try{ super.getConnection(); pstm=con.prepareStatement(sql); pstm.setString(1,"%"+n_content+"%"); rs=pstm.executeQuery(); while(rs.next()){ Newsnews=newNews(); news.setN_id(rs.getInt("n_id")); news.setN_title(rs.getString("n_title")); news.setN_content(rs.getString("n_content")); news.setT_id(rs.getInt("t_id")); news.setN_publishtime(rs.getString("n_publishtime"));news.setN_source(rs.getString("n_source")); list.add(news); } }catch(Exceptione){ e.printStackTrace(); }finally{ super.closeAll(); } returnlist; }//添加新聞publicbooleanadd(Newsnews){ booleanflag=false; try{ super.getConnection(); Stringsql="insertintonrc_news(n_title,n_content,t_id,n_publishtime,n_source)values(?,?,?,?,?)"; if(i>0){ System.out.println("插入新聞成功!"); } flag=true; }catch(Exceptione){ e.printStackTrace(); }finally{ //釋放資源 super.closeAll(); } returnflag; }//根據(jù)新聞ID刪除新聞 publicbooleandelete(intid){ booleanflag=false; try{ super.getConnection(); Stringsql="deletefromnrc_newswheren_id=?"; //處理執(zhí)行結(jié)果 if(i>0){ System.out.println("刪除新聞成功!"); } flag=true; }catch(Exceptione){ e.printStackTrace(); }finally{ //釋放資源 super.closeAll(); } returnflag; }//修改新聞 publicbooleanupdate(Newsnews){ booleanflag=false; try{ super.getConnection(); Stringsql="updatenrc_newssetn_title=?,n_content=?,t_id=?,n_publishtime=?,n_source=?wheren_id=?"; //處理執(zhí)行結(jié)果 if(i>0){ System.out.println("修改新聞成功!"); } flag=true; }catch(Exceptione){ e.printStackTrace(); }finally{ //釋放資源 super.closeAll(); } returnflag; }}4.使用測(cè)試類測(cè)試數(shù)據(jù)庫(kù)訪問與結(jié)果數(shù)據(jù)顯示publicclassTest{publicstaticvoidmain(String[]args){NewsDaoImplnewsDao=newNewsDaoImpl();ListnewsList=newArr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論