hyunjeong

Untitled

Aug 29th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. package bbs;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8.  
  9. public class BbsDAO {
  10. private Connection con;
  11. private PreparedStatement pstmt;
  12. private ResultSet rs;
  13.  
  14. public BbsDAO (){
  15. try {
  16. String dburl="jdbc:mysql://localhost:3306/shift";
  17. String dbId ="shift";
  18. String dbPassword = "shift";
  19. Class.forName("com.mysql.jdbc.Driver");
  20. con= DriverManager.getConnection(dburl, dbId, dbPassword);
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25.  
  26. public String getDate (){
  27. String SQL = "SELECT NOW()";
  28. try {
  29. PreparedStatement pstmt = con.prepareStatement(SQL);
  30. rs = pstmt.executeQuery();
  31. if(rs.next()) {
  32. return rs.getString(1);
  33. }
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. return ""; // 데이터메이스 오류
  38. }
  39.  
  40.  
  41. public int getNext (){
  42. String SQL = "SELECT bbsId FROM BBS ORDER BY bbsId DESC";
  43. try {
  44. PreparedStatement pstmt = con.prepareStatement(SQL);
  45. rs = pstmt.executeQuery();
  46. if(rs.next()) {
  47. return rs.getInt(1)+1;
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. return -1; // 데이터메이스 오류
  53. }
  54.  
  55.  
  56. public int write (String bbsTitle, String userId, String bbsContent){
  57. String SQL = "INSERT INTO BBS VALUES (?, ?, ? , ? ,? ,? )";
  58. try {
  59. PreparedStatement pstmt = con.prepareStatement(SQL);
  60. pstmt.setInt(1, getNext());
  61. pstmt.setString(2, bbsTitle);
  62. pstmt.setString(3, userId);
  63. pstmt.setString(4, getDate());
  64. pstmt.setString(5, bbsContent);
  65. pstmt.setInt(6, 1);
  66. return pstmt.executeUpdate();
  67.  
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. return -1; // 데이터메이스 오류
  72. }
  73.  
  74.  
  75. public ArrayList <Bbs> getList (int pageNumber) {
  76. String SQL = "SELECT * FROM BBS WHERE bbsId < ? AND bbsAvailable = 1 ORDER BY bbsId DESC LIMIT 10";
  77. ArrayList <Bbs> list = new ArrayList<Bbs> ();
  78. try {
  79. PreparedStatement pstmt = con.prepareStatement(SQL);
  80. pstmt.setInt(1, getNext() - (pageNumber -1) * 10);
  81. rs = pstmt.executeQuery();
  82. while (rs.next()) {
  83. Bbs bbs = new Bbs();
  84. bbs.setBbsId(rs.getInt(1));
  85. bbs.setBbsTitle(rs.getString(2));
  86. bbs.setUserId(rs.getString(3));
  87. bbs.setBbsDate(rs.getString(4));
  88. bbs.setBbsContent(rs.getString(5));
  89. bbs.setBbsAvailable(rs.getInt(6));
  90. list.add(bbs);
  91. }
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. return list;
  96.  
  97. }
  98.  
  99. public boolean nextPage (int pageNumber) {
  100. String SQL = "SELECT * FROM BBS WHERE bbsId < ? AND bbsAvailable =1";
  101. try {
  102. PreparedStatement pstmt = con.prepareStatement(SQL);
  103. pstmt.setInt(1, getNext()- (pageNumber-1)*10);
  104. rs = pstmt.executeQuery();
  105. if (rs.next()) {
  106. return true;
  107. }
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111. return false;
  112. }
  113.  
  114.  
  115. public Bbs getBbs (int bbsId) {
  116. String SQL = "SELECT * FROM bbs WHERE bbsId= ?";
  117. try {
  118. PreparedStatement pstmt = con.prepareStatement(SQL);
  119. pstmt.setInt(1, bbsId);
  120. rs = pstmt.executeQuery();
  121. if (rs.next()) {
  122. Bbs bbs = new Bbs();
  123. bbs.setBbsId(rs.getInt(1));
  124. bbs.setBbsTitle(rs.getString(2));
  125. bbs.setUserId(rs.getString(3));
  126. bbs.setBbsDate(rs.getString(4));
  127. bbs.setBbsContent(rs.getString(5));
  128. bbs.setBbsAvailable(rs.getInt(6));
  129. return bbs;
  130. }
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. return null;
  135. }
  136.  
  137. public int update (int bbsId, String bbsTitle, String bbsContent) {
  138. String SQL = "update bbs set bbsTitle = ? , bbsContent = ? where bbsId= ? ";
  139. try {
  140. PreparedStatement pstmt = con.prepareStatement(SQL);
  141. pstmt.setString(1, bbsTitle);
  142. pstmt.setString(2, bbsContent);
  143. pstmt.setInt(3, bbsId);
  144. return pstmt.executeUpdate();
  145.  
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. return -1;
  150. }
  151.  
  152. public int delete (int bbsId) {
  153. String SQL = "update bbs set bbsAvailable = 0 where bbsId= ? ";
  154. try {
  155. PreparedStatement pstmt = con.prepareStatement(SQL);
  156. pstmt.setInt(1, bbsId);
  157. return pstmt.executeUpdate();
  158.  
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. }
  162. return -1;
  163. }
  164.  
  165. }
Add Comment
Please, Sign In to add comment