JDBC实现增删改查.doc_第1页
JDBC实现增删改查.doc_第2页
JDBC实现增删改查.doc_第3页
JDBC实现增删改查.doc_第4页
JDBC实现增删改查.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. publicclassNoteDAOImplimplementsNoteDAO 2. /增加操作 3. publicvoidinsert(Notenote)throwsException 4. Stringsql=INSERTINTOnote(id,title,author,content)VALUES(note_sequ.nextVal,?,?,?); 5. PreparedStatementpstmt=null; 6. DataBaseConnectiondbc=null; 7. dbc=newDataBaseConnection(); 8. try 9. pstmt=dbc.getConnection().prepareStatement(sql); 10. pstmt.setString(1,note.getTitle(); 11. pstmt.setString(2,note.getAuthor(); 12. pstmt.setString(3,note.getContent(); 13. pstmt.executeUpdate(); 14. pstmt.close(); 15. catch(Exceptione) 16. /System.out.println(e); 17. thrownewException(操作中出现错误!); 18. finally 19. dbc.close(); 20. 21. 22. /修改操作 23. publicvoidupdate(Notenote)throwsException 24. Stringsql=UPDATEnoteSETtitle=?,author=?,content=?WHEREid=?; 25. PreparedStatementpstmt=null; 26. DataBaseConnectiondbc=null; 27. dbc=newDataBaseConnection(); 28. try 29. pstmt=dbc.getConnection().prepareStatement(sql); 30. pstmt.setString(1,note.getTitle(); 31. pstmt.setString(2,note.getAuthor(); 32. pstmt.setString(3,note.getContent(); 33. pstmt.setInt(4,note.getId(); 34. pstmt.executeUpdate(); 35. pstmt.close(); 36. catch(Exceptione) 37. thrownewException(操作中出现错误!); 38. finally 39. dbc.close(); 40. 41. 42. /删除操作 43. publicvoiddelete(intid)throwsException 44. Stringsql=DELETEFROMnoteWHEREid=?; 45. PreparedStatementpstmt=null; 46. DataBaseConnectiondbc=null; 47. dbc=newDataBaseConnection(); 48. try 49. pstmt=dbc.getConnection().prepareStatement(sql); 50. pstmt.setInt(1,id); 51. pstmt.executeUpdate(); 52. pstmt.close(); 53. catch(Exceptione) 54. thrownewException(操作中出现错误!); 55. finally 56. dbc.close(); 57. 58. 59. /按ID查询,主要为更新使用 60. publicNotequeryById(intid)throwsException 61. Notenote=null; 62. Stringsql=SELECTid,title,author,contentFROMnoteWHEREid=?; 63. PreparedStatementpstmt=null; 64. DataBaseConnectiondbc=null; 65. dbc=newDataBaseConnection(); 66. try 67. pstmt=dbc.getConnection().prepareStatement(sql); 68. pstmt.setInt(1,id); 69. ResultSetrs=pstmt.executeQuery(); 70. if(rs.next() 71. note=newNote(); 72. note.setId(rs.getInt(1); 73. note.setTitle(rs.getString(2); 74. note.setAuthor(rs.getString(3); 75. note.setContent(rs.getString(4); 76. 77. rs.close(); 78. pstmt.close(); 79. catch(Exceptione) 80. thrownewException(操作中出现错误!); 81. finally 82. dbc.close(); 83. 84. returnnote; 85. 86. /查询全部 87. publicListqueryAll()throwsException 88. Listall=newArrayList(); 89. Stringsql=SELECTid,title,author,contentFROMnote; 90. PreparedStatementpstmt=null; 91. DataBaseConnectiondbc=null; 92. dbc=newDataBaseConnection(); 93. try 94. pstmt=dbc.getConnection().prepareStatement(sql); 95. ResultSetrs=pstmt.executeQuery(); 96. while(rs.next() 97. Notenote=newNote(); 98. note.setId(rs.getInt(1); 99. note.setTitle(rs.getString(2); 100. note.setAuthor(rs.getString(3); 101. note.setContent(rs.getString(4); 102. all.add(note); 103. 104. rs.close(); 105. pstmt.close(); 106. catch(Exceptione) 107. System.out.println(e); 108. thrownewException(操作中出现错误!); 109. finally 110. dbc.close(); 111. 112. returnall; 113. 114. /模糊查询 115. publicListqueryByLike(Stringcond)throwsException 116. Listall=newArrayList(); 117. Stringsql=SELECTid,title,author,contentFROMnoteWHEREtitleLIKE?orAUTHORLIKE?orCONTENTLIKE?; 118. PreparedStatementpstmt=null; 119. DataBaseConnectiondbc=null; 120. dbc=newDataBaseConnection(); 121. try 122. pstmt=dbc.getConnection().prepareStatement(sql); 123. pstmt.setString(1,%+cond+%); 124. pstmt.setString(2,%+cond+%); 125. pstmt.setString(3,%+cond+%); 126. ResultSetrs=pstmt.executeQuery(); 127. while(rs.next() 128. Notenote=newNote(); 129. note.setId(rs.getInt(1); 130. note.setTitle(rs.getString(2); 131. note.setAuthor(rs.getString(3); 132. note.setContent(rs.getString(4); 133. all.add(note); 134. 135. rs.close(); 136. pstmt.close(); 137

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论