jdbc实现增删改查

上传人:小** 文档编号:88022888 上传时间:2019-04-17 格式:DOC 页数:10 大小:39.50KB
返回 下载 相关 举报
jdbc实现增删改查_第1页
第1页 / 共10页
jdbc实现增删改查_第2页
第2页 / 共10页
jdbc实现增删改查_第3页
第3页 / 共10页
jdbc实现增删改查_第4页
第4页 / 共10页
jdbc实现增删改查_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《jdbc实现增删改查》由会员分享,可在线阅读,更多相关《jdbc实现增删改查(10页珍藏版)》请在金锄头文库上搜索。

1、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.get

2、Connection().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.

3、 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);

4、 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. /删除操

5、作 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. ps

6、tmt.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. DataBaseConn

7、ectiondbc=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.getSt

8、ring(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,

9、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.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号