package board.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import board.dto.BoardDTO;

public class BoardDAO {
	private String driver="oracle.jdbc.OracleDriver";
	private String url = "jdbc:oracle:thin:@localhost:1521:xe";
	private String db_id = "DBTEST";
	private String db_pw = "1234";
	private Connection con = null;
	private PreparedStatement pstmt = null;
	private ResultSet res = null;
	
	public BoardDAO() {
		try {
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Connection getConnection(String url, String db_id, String db_pw) {
		try {
			con=DriverManager.getConnection(url,db_id,db_pw);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(con!=null) con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return con;
	}
	
	// db 통로
	public void streamClose(ResultSet res, PreparedStatement pstmt, Connection con) {
		try {
			if(res != null) res.close();
			if(pstmt != null) pstmt.close();
			if(con != null) con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	// streamClase end
	
	// 글쓰기
	public int write(BoardDTO dto) {
		int su = 0;
		int hit = 1;
		try {
			String sql = "insert into board values(board_seq.nextval,?,?,?,?,?,sysdate)";
			con = DriverManager.getConnection(url,db_id,db_pw);
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getId());
			pstmt.setString(2, dto.getName());
			pstmt.setString(3, dto.getSubject());
			pstmt.setString(4, dto.getContent());
			pstmt.setInt(5, hit);
			su=pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			streamClose(res,pstmt,con);
		}
		return su;
	}
	
	// insert() end
	
}