Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.12 KB | None | 0 0
  1. package board;
  2.  
  3. import java.sql.*;
  4. import java.util.*;
  5.  
  6. public class BoardDBBean {
  7.    
  8.     private static BoardDBBean instance = new BoardDBBean();
  9.    
  10.     public static BoardDBBean getInstance() {
  11.         return instance;
  12.     }
  13.    
  14.     private BoardDBBean() {}
  15.    
  16.     private Connection getConnection() throws Exception {
  17.         Connection conn=null;
  18.          
  19.         String jdbcUrl="jdbc:oracle:thin:@localhost:1521:orcl";
  20.         String dbId="jsp";
  21.         String dbPass="jsppass";
  22.          
  23.         Class.forName("oracle.driver.OracleDriver");
  24.         conn=DriverManager.getConnection(jdbcUrl,dbId ,dbPass );
  25.         return conn;
  26.     }
  27.  
  28.     public void insertArticle(BoardDataBean article)
  29.             throws Exception {
  30.         Connection conn = null;
  31.         PreparedStatement pstmt = null;
  32.         ResultSet rs = null;
  33.  
  34.         int num=article.getNum();
  35.         int ref=article.getRef();
  36.         int re_step=article.getRe_step();
  37.         int re_level=article.getRe_level();
  38.         int number=0;
  39.         String sql="";
  40.  
  41.         try {
  42.             conn = getConnection();
  43.  
  44.             pstmt = conn.prepareStatement("select max(num) from board");
  45.             rs = pstmt.executeQuery();
  46.            
  47.             if (rs.next())
  48.               number=rs.getInt(1)+1;
  49.             else
  50.               number=1;
  51.            
  52.             if (num!=0)   //
  53.             {  
  54.               sql="update board set re_step=re_step+1 where ref= ? and re_step> ?";
  55.               pstmt = conn.prepareStatement(sql);
  56.               pstmt.setInt(1, ref);
  57.               pstmt.setInt(2, re_step);
  58.               pstmt.executeUpdate();
  59.               re_step=re_step+1;
  60.               re_level=re_level+1;
  61.              }else{
  62.               ref=number;
  63.               re_step=0;
  64.               re_level=0;
  65.              }   
  66.             // 쿼리를 작성
  67.             sql = "insert into board(writer,email,subject,passwd,reg_date,";
  68.             sql+="ref,re_step,re_level,content,ip) values(?,?,?,?,?,?,?,?,?,?)";
  69.  
  70.             pstmt = conn.prepareStatement(sql);
  71.             pstmt.setString(1, article.getWriter());
  72.             pstmt.setString(2, article.getEmail());
  73.             pstmt.setString(3, article.getSubject());
  74.             pstmt.setString(4, article.getPasswd());
  75.             pstmt.setTimestamp(5, article.getReg_date());
  76.             pstmt.setInt(6, ref);
  77.             pstmt.setInt(7, re_step);
  78.             pstmt.setInt(8, re_level);
  79.             pstmt.setString(9, article.getContent());
  80.             pstmt.setString(10, article.getIp());
  81.            
  82.             pstmt.executeUpdate();
  83.         } catch(Exception ex) {
  84.             ex.printStackTrace();
  85.         } finally {
  86.             if (rs != null) try { rs.close(); } catch(SQLException ex) {}
  87.             if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
  88.             if (conn != null) try { conn.close(); } catch(SQLException ex) {}
  89.         }
  90.     }
  91.    
  92.     public int getArticleCount()
  93.              throws Exception {
  94.         Connection conn = null;
  95.         PreparedStatement pstmt = null;
  96.         ResultSet rs = null;
  97.  
  98.         int x=0;
  99.  
  100.         try {
  101.             conn = getConnection();
  102.            
  103.             pstmt = conn.prepareStatement("select count(*) from board");
  104.             rs = pstmt.executeQuery();
  105.  
  106.             if (rs.next()) {
  107.                x= rs.getInt(1);
  108.             }
  109.         } catch(Exception ex) {
  110.             ex.printStackTrace();
  111.         } finally {
  112.             if (rs != null) try { rs.close(); } catch(SQLException ex) {}
  113.             if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
  114.             if (conn != null) try { conn.close(); } catch(SQLException ex) {}
  115.         }
  116.         return x;
  117.     }
  118.  
  119.     public List getArticles(int start, int end)
  120.              throws Exception {
  121.         Connection conn = null;
  122.         PreparedStatement pstmt = null;
  123.         ResultSet rs = null;
  124.         List articleList=null;
  125.         try {
  126.             conn = getConnection();
  127.            
  128.             pstmt = conn.prepareStatement(
  129.                 "select * from board order by ref desc, re_step asc limit ?,? ");
  130.             pstmt.setInt(1, start-1);
  131.             pstmt.setInt(2, end);
  132.             rs = pstmt.executeQuery();
  133.  
  134.             if (rs.next()) {
  135.                 articleList = new ArrayList(end);
  136.                 do{
  137.                   BoardDataBean article= new BoardDataBean();
  138.                   article.setNum(rs.getInt("num"));
  139.                   article.setWriter(rs.getString("writer"));
  140.                   article.setEmail(rs.getString("email"));
  141.                   article.setSubject(rs.getString("subject"));
  142.                   article.setPasswd(rs.getString("passwd"));
  143.                   article.setReg_date(rs.getTimestamp("reg_date"));
  144.                   article.setReadcount(rs.getInt("readcount"));
  145.                   article.setRef(rs.getInt("ref"));
  146.                   article.setRe_step(rs.getInt("re_step"));
  147.                   article.setRe_level(rs.getInt("re_level"));
  148.                   article.setContent(rs.getString("content"));
  149.                   article.setIp(rs.getString("ip"));
  150.                  
  151.                   articleList.add(article);
  152.                 }while(rs.next());
  153.             }
  154.         } catch(Exception ex) {
  155.             ex.printStackTrace();
  156.         } finally {
  157.             if (rs != null) try { rs.close(); } catch(SQLException ex) {}
  158.             if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
  159.             if (conn != null) try { conn.close(); } catch(SQLException ex) {}
  160.         }
  161.         return articleList;
  162.     }
  163.  
  164.     public BoardDataBean getArticle(int num)
  165.             throws Exception {
  166.         Connection conn = null;
  167.         PreparedStatement pstmt = null;
  168.         ResultSet rs = null;
  169.         BoardDataBean article=null;
  170.         try {
  171.             conn = getConnection();
  172.  
  173.             pstmt = conn.prepareStatement(
  174.                 "update board set readcount=readcount+1 where num = ?");
  175.             pstmt.setInt(1, num);
  176.             pstmt.executeUpdate();
  177.  
  178.             pstmt = conn.prepareStatement(
  179.                 "select * from board where num = ?");
  180.             pstmt.setInt(1, num);
  181.             rs = pstmt.executeQuery();
  182.  
  183.             if (rs.next()) {
  184.                 article = new BoardDataBean();
  185.                 article.setNum(rs.getInt("num"));
  186.                 article.setWriter(rs.getString("writer"));
  187.                 article.setEmail(rs.getString("email"));
  188.                 article.setSubject(rs.getString("subject"));
  189.                 article.setPasswd(rs.getString("passwd"));
  190.                 article.setReg_date(rs.getTimestamp("reg_date"));
  191.                 article.setReadcount(rs.getInt("readcount"));
  192.                 article.setRef(rs.getInt("ref"));
  193.                 article.setRe_step(rs.getInt("re_step"));
  194.                 article.setRe_level(rs.getInt("re_level"));
  195.                 article.setContent(rs.getString("content"));
  196.                 article.setIp(rs.getString("ip"));    
  197.             }
  198.         } catch(Exception ex) {
  199.             ex.printStackTrace();
  200.         } finally {
  201.             if (rs != null) try { rs.close(); } catch(SQLException ex) {}
  202.             if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
  203.             if (conn != null) try { conn.close(); } catch(SQLException ex) {}
  204.         }
  205.         return article;
  206.     }
  207.  
  208.     public BoardDataBean updateGetArticle(int num)
  209.           throws Exception {
  210.         Connection conn = null;
  211.         PreparedStatement pstmt = null;
  212.         ResultSet rs = null;
  213.         BoardDataBean article=null;
  214.         try {
  215.             conn = getConnection();
  216.  
  217.             pstmt = conn.prepareStatement(
  218.                 "select * from board where num = ?");
  219.             pstmt.setInt(1, num);
  220.             rs = pstmt.executeQuery();
  221.  
  222.             if (rs.next()) {
  223.                 article = new BoardDataBean();
  224.                 article.setNum(rs.getInt("num"));
  225.                 article.setWriter(rs.getString("writer"));
  226.                 article.setEmail(rs.getString("email"));
  227.                 article.setSubject(rs.getString("subject"));
  228.                 article.setPasswd(rs.getString("passwd"));
  229.                 article.setReg_date(rs.getTimestamp("reg_date"));
  230.                 article.setReadcount(rs.getInt("readcount"));
  231.                 article.setRef(rs.getInt("ref"));
  232.                 article.setRe_step(rs.getInt("re_step"));
  233.                 article.setRe_level(rs.getInt("re_level"));
  234.                 article.setContent(rs.getString("content"));
  235.                 article.setIp(rs.getString("ip"));    
  236.             }
  237.         } catch(Exception ex) {
  238.             ex.printStackTrace();
  239.         } finally {
  240.             if (rs != null) try { rs.close(); } catch(SQLException ex) {}
  241.             if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
  242.             if (conn != null) try { conn.close(); } catch(SQLException ex) {}
  243.         }
  244.         return article;
  245.     }
  246.  
  247.     public int updateArticle(BoardDataBean article)
  248.           throws Exception {
  249.         Connection conn = null;
  250.         PreparedStatement pstmt = null;
  251.         ResultSet rs= null;
  252.  
  253.         String dbpasswd="";
  254.         String sql="";
  255.         int x=-1;
  256.         try {
  257.             conn = getConnection();
  258.            
  259.             pstmt = conn.prepareStatement(
  260.                 "select passwd from board where num = ?");
  261.             pstmt.setInt(1, article.getNum());
  262.             rs = pstmt.executeQuery();
  263.            
  264.             if(rs.next()){
  265.               dbpasswd= rs.getString("passwd");
  266.               if(dbpasswd.equals(article.getPasswd())){
  267.                 sql="update board set writer=?,email=?,subject=?,passwd=?";
  268.                 sql+=",content=? where num=?";
  269.                 pstmt = conn.prepareStatement(sql);
  270.  
  271.                 pstmt.setString(1, article.getWriter());
  272.                 pstmt.setString(2, article.getEmail());
  273.                 pstmt.setString(3, article.getSubject());
  274.                 pstmt.setString(4, article.getPasswd());
  275.                 pstmt.setString(5, article.getContent());
  276.                 pstmt.setInt(6, article.getNum());
  277.                 pstmt.executeUpdate();
  278.                 x= 1;
  279.               }else{
  280.                 x= 0;
  281.               }
  282.             }
  283.         } catch(Exception ex) {
  284.             ex.printStackTrace();
  285.         } finally {
  286.             if (rs != null) try { rs.close(); } catch(SQLException ex) {}
  287.             if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
  288.             if (conn != null) try { conn.close(); } catch(SQLException ex) {}
  289.         }
  290.         return x;
  291.     }
  292.    
  293.     public int deleteArticle(int num, String passwd)
  294.         throws Exception {
  295.         Connection conn = null;
  296.         PreparedStatement pstmt = null;
  297.         ResultSet rs= null;
  298.         String dbpasswd="";
  299.         int x=-1;
  300.         try {
  301.             conn = getConnection();
  302.  
  303.             pstmt = conn.prepareStatement(
  304.                 "select passwd from board where num = ?");
  305.             pstmt.setInt(1, num);
  306.             rs = pstmt.executeQuery();
  307.            
  308.             if(rs.next()){
  309.                 dbpasswd= rs.getString("passwd");
  310.                 if(dbpasswd.equals(passwd)){
  311.                     pstmt = conn.prepareStatement(
  312.                       "delete from board where num=?");
  313.                     pstmt.setInt(1, num);
  314.                     pstmt.executeUpdate();
  315.                     x= 1; //글삭제 성공
  316.                 }else
  317.                     x= 0; //비밀번호 틀림
  318.             }
  319.         } catch(Exception ex) {
  320.             ex.printStackTrace();
  321.         } finally {
  322.             if (rs != null) try { rs.close(); } catch(SQLException ex) {}
  323.             if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
  324.             if (conn != null) try { conn.close(); } catch(SQLException ex) {}
  325.         }
  326.         return x;
  327.     }
  328.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement