Advertisement
Guest User

Untitled

a guest
May 5th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Context reloadable="true">
  3.  
  4. <Resource name="jdbc/freeboardDB" auth="Container" type="javax.sql.DataSource"
  5. maxActive="100" maxIdle="30" maxWait="10000" removeAbandoned="true"
  6. username="root" password="1024" driverClassName="com.mysql.jdbc.Driver"
  7. url="jdbc:mysql://localhost:3306/freeboard" />
  8. </Context>
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. package board.dao;
  17.  
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24.  
  25. import javax.naming.InitialContext;
  26. import javax.naming.NamingException;
  27. import javax.sql.DataSource;
  28.  
  29. public class MysqlcpPostDAO implements PostDAO {
  30. DataSource pool;
  31.  
  32. public MysqlcpPostDAO() {
  33. try {
  34. InitialContext ctx = new InitialContext();
  35.  
  36. pool = (DataSource) ctx.lookup("java:comp/env/jdbc/freeboardDB");
  37. } catch (NamingException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. @Override
  43. public List<PostVO> getPostList(int page) {
  44. Connection conn = null;
  45. PreparedStatement pstmt = null;
  46. ResultSet rs = null;
  47. List<PostVO> postList = null;
  48. String sql = "SELECT id, title, hits, date FROM post ORDER BY id DESC LIMIT ?, 10";
  49. int num = page * 10 - 10;
  50.  
  51. try {
  52. conn = pool.getConnection();
  53. pstmt = conn.prepareStatement(sql);
  54. pstmt.setInt(1, num);
  55. rs = pstmt.executeQuery();
  56. postList = new ArrayList<PostVO>();
  57.  
  58. while (rs.next()) {
  59. int id = rs.getInt("id");
  60. String title = rs.getString("title");
  61. String date = rs.getString("date");
  62. int hits = rs.getInt("hits");
  63.  
  64. postList.add(new PostVO(id, title, date, hits));
  65. }
  66.  
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. } finally {
  70. try {
  71. if (rs != null) {rs.close();}
  72. if (pstmt != null) {pstmt.close();}
  73. if (conn != null) {conn.close();}
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78.  
  79. return postList;
  80. }
  81.  
  82. @Override
  83. public PostVO getPostById(PostVO postVO) {
  84. Connection conn = null;
  85. PreparedStatement pstmt = null;
  86. ResultSet rs = null;
  87. String sql = "SELECT body FROM post WHERE id=?";
  88.  
  89. try {
  90. conn = pool.getConnection();
  91. pstmt = conn.prepareStatement(sql);
  92. pstmt.setInt(1, postVO.getNo());
  93. rs = pstmt.executeQuery();
  94.  
  95. rs.next();
  96.  
  97. postVO.setBody(rs.getString("body"));
  98.  
  99. } catch (SQLException e) {
  100. e.printStackTrace();
  101. } finally {
  102. try {
  103. if (rs != null) {rs.close();}
  104. if (pstmt != null) {pstmt.close();}
  105. if (conn != null) {conn.close();}
  106. } catch (SQLException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110.  
  111. return postVO;
  112. }
  113.  
  114. @Override
  115. public void insertPost(String title, String body) {
  116. Connection conn = null;
  117. PreparedStatement pstmt = null;
  118. String sql = "INSERT INTO post (title, body) VALUES (?, ?)";
  119.  
  120. try {
  121. conn = pool.getConnection();
  122. pstmt = conn.prepareStatement(sql);
  123. pstmt.setString(1, title);
  124. pstmt.setString(2, body);
  125. pstmt.executeUpdate();
  126.  
  127. } catch (SQLException e) {
  128. e.printStackTrace();
  129. } finally {
  130. try {
  131. if (pstmt != null) {pstmt.close();}
  132. if (conn != null) {conn.close();}
  133. } catch (SQLException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. }
  138.  
  139. @Override
  140. public void updateHits(int id, PostVO postVO) {
  141. Connection conn = null;
  142. PreparedStatement pstmt = null;
  143. ResultSet rs = null;
  144. String sql = "UPDATE post SET hits=hits+1 WHERE id=?";
  145. String query = "SELECT hits FROM post WHERE id="+id;
  146.  
  147. try {
  148. conn = pool.getConnection();
  149. pstmt = conn.prepareStatement(sql);
  150. pstmt.setInt(1, id);
  151. pstmt.executeUpdate();
  152.  
  153. rs = pstmt.executeQuery(query);
  154.  
  155. rs.next();
  156.  
  157. postVO.setHits(rs.getInt("hits"));
  158. } catch (SQLException e) {
  159. e.printStackTrace();
  160. } finally {
  161. try {
  162. if (rs != null) {rs.close();}
  163. if (pstmt != null) {pstmt.close();}
  164. if (conn != null) {conn.close();}
  165. } catch (SQLException e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. }
  170.  
  171. @Override
  172. public int countPost() {
  173. Connection conn = null;
  174. PreparedStatement pstmt = null;
  175. ResultSet rs = null;
  176. int total = 0;
  177. String sql = "SELECT COUNT(*) AS totalPost FROM post";
  178.  
  179. try {
  180. conn = pool.getConnection();
  181. pstmt = conn.prepareStatement(sql);
  182. rs = pstmt.executeQuery();
  183.  
  184. rs.next();
  185.  
  186. total = rs.getInt("totalPost");
  187.  
  188. } catch (SQLException e) {
  189. e.printStackTrace();
  190. } finally {
  191. try {
  192. if (rs != null) {rs.close();}
  193. if (pstmt != null) {pstmt.close();}
  194. if (conn != null) {conn.close();}
  195. } catch (SQLException e) {
  196. e.printStackTrace();
  197. }
  198. }
  199.  
  200. return total;
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement