package pupustory.derby.dao;
import java.sql.*;
import pupustory.derby.bean.*;
// º¸µå »ý¼º Äõ¸® 
//CREATE TABLE TB_PUPUSTORY_BRD (USR_ID VARCHAR(10),TITLE VARCHAR(50), TEXT VARCHAR(500));
public class DerbyBoardDAO {
	private DerbyBoardDAO instance = null;
	// µå¶óÀÌ¹ö
	private final static String DERBY_DB_DRIVE = "org.apache.derby.jdbc.ClientDriver";
	// URL
	private final static String DERBY_DB_URL = "jdbc:derby://localhost:1527/pupustoryDB";
	// ¸ñ·Ï °¡Á®¿À±â Äõ¸®
	private final static String BRD_LST = "SELECT USR_ID,TITLE,TEXT FROM TB_PUPUSTORY_BRD";
	// ±Û ¿Ã¸®±â Äõ¸®
	private final static String BRD_INSERT = "INSERT INTO TB_PUPUSTORY_BRD VALUES (?,?,?)";
	
	// º¸µå ¸ñ·Ï °¡Á®¿À±â
	public java.util.List getBrdLst() {
		java.util.List list = null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet record = null;
		BoardBean bean = null;
		
		try {
			Class.forName(DERBY_DB_DRIVE);
			conn = DriverManager.getConnection(DERBY_DB_URL); 
			stmt = conn.createStatement();
			record = stmt.executeQuery(BRD_LST);
			list = new java.util.ArrayList();
			while (record.next()) {
				bean = new BoardBean();
				bean.setUsr_id(record.getString(1));
				bean.setTitle(record.getString(2));
				bean.setText(record.getString(3));
				list.add(bean);
			}
			record.close();
			stmt.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (record != null) record.close();
				if (stmt != null) stmt.close();
				if (conn != null) conn.close();
			}catch (Exception e) {}
		}
		return list;
	}
	
	// º¸µå ±Û µî·ÏÇÏ±â
	public boolean setBrdInsert(BoardBean bean) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		Boolean isComplete = false;
		
		try {
			Class.forName(DERBY_DB_DRIVE);
			conn = DriverManager.getConnection(DERBY_DB_URL); 
			pstmt = conn.prepareStatement(BRD_INSERT);
			pstmt.setString(1,bean.getUsr_id());
			pstmt.setString(2,bean.getTitle());
			pstmt.setString(3,bean.getText());
			pstmt.execute();
			
			if (pstmt.getUpdateCount() > 0) isComplete = true;
			
			pstmt.close();
			conn.close();
			
		} catch (Exception  e) {
			e.printStackTrace();
			
		} finally {
			try {
				if (pstmt != null) pstmt.close();
				if (conn != null) conn.close();
			} catch (Exception e) {}
		}
	
		return isComplete;
	
	}
}

