// 增加操作
3. public void insert(Note note) throws Exception {
4. String sql = "INSERT INTO note(id,title,author,content) VALUES(note_sequ.nextVal,?,?,?)" ;
5. PreparedStatement pstmt = null ;
6. DataBaseConnection dbc = null ;
7. dbc = new DataBaseConnection() ;
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 (Exception e) {
16. // System.out.println(e) ;
17. throw new Exception("操作中出现错误!!!") ;
18. } finally {
19. dbc.close() ;
20. }
21. }
// 修改操作
23. public void update(Note note) throws Exception {
24. String sql = "UPDATE note SET title=?,author=?,content=? WHERE id=?" ;
25. PreparedStatement pstmt = null ;
26. DataBaseConnection dbc = null ;
27. dbc = new DataBaseConnection() ;
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 (Exception e) {
37. throw new Exception("操作中出现错误!!!") ;
38. } finally {
39. dbc.close() ;
40. }
41. }
42. // 删除操作
43. public void delete(int id) throws Exception {
44. String sql = "DELETE FROM note WHERE id=?" ;
45. PreparedStatement pstmt = null ;
46. DataBaseConnection dbc = null ;
47. dbc = new DataBaseConnection() ;
48. try {
49. pstmt = dbc.getConnection().prepareStatement(sql) ;
50. pstmt.setInt(1,id) ;
51. pstmt.executeUpdate() ;
52. pstmt.close() ;
53. } catch (Exception e) {
54. throw new Exception("操作中出现错误!!!") ;
55. } finally {
56. dbc.close() ;
57. }
58. }
59. // 按ID查询,主要为更新使用
60. public Note queryById(int id) throws Exception {
61. Note note = null ;
62. String sql = "SELECT id,title,author,content FROM note WHERE id=?" ;
63. PreparedStatement pstmt = null ;
64. DataBaseConnection dbc = null ;
65. dbc = new DataBaseConnection() ;
66. try {
67. pstmt = dbc.getConnection().prepareStatement(sql) ;
68. pstmt.setInt(1,id) ;
69. ResultSet rs = pstmt.executeQuery() ;
70. if(rs.next()) {
71. note = new Note() ;
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 (Exception e) {
80. throw new Exception("操作中出现错误!!!") ;
81. } finally {
82. dbc.close() ;
83. }
84. return note ;
85. }
86. // 查询全部
87. public List queryAll() throws Exception {
88. List all = new ArrayList() ;
89. String sql = "SELECT id,title,author,content FROM note" ;
90. PreparedStatement pstmt = null ;
91. DataBaseConnection dbc = null ;
92. dbc = new DataBaseConnection() ;
93. try {
94. pstmt = dbc.getConnection().prepareStatement(sql) ;
95. ResultSet rs = pstmt.executeQuery() ;
96. while(rs.next()) {
97. Note note = new Note() ;
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 (Exception e) {
107. System.out.println(e) ;
108. throw new Exception("操作中出现错误!!!") ;
109. } finally {
110. dbc.close() ;
111. }
112. return all ;
113. }
114. // 模糊查询
115. public List queryByLike(String cond) throws Exception {
116. List all = new ArrayList() ;
117. String sql = "SELECT id,title,author,content FROM note WHERE title LIKE ? or AUTHOR LIKE ? or CONTENT LIKE ?" ;
118. PreparedStatement pstmt = null ;
119. DataBaseConnection dbc = null ;
120. dbc = new DataBaseConnection() ;
121. try {
122. pstmt = dbc.getConnection().prepareStatement(sql) ;
123. pstmt.setString(1,"%"+cond+"%") ;
124. pstmt.setString(2,"%"+cond+"%") ;
125. pstmt.setString(3,"%"+cond+"%") ;
126. ResultSet rs = pstmt.executeQuery() ;
127. while(rs.next()) {
128. Note note = new Note() ;
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. } catch (Exception e) {
138. System.out.println(e) ;
139. throw new Exception("操作中出现错误!!!") ;
140. } finally {
141. dbc.close() ;
142. }
143. return all ;
144. }
145. };
|