Web/게시판2012. 8. 23. 23:34
package endless.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

public class BoardDao {
	Board board;
	Connection connection;
	PreparedStatement preparedStatement;
	DataBaseChoice dbchoice = new DataBaseChoice();
	Vector boardlist;
	ResultSet resultset;

	public void insert(Object object) throws SQLException,
			ClassNotFoundException {
		board = (Board) object;
		connection = dbchoice.makeConnection();
		String insertQuery = "INSERT INTO BOARD (id,title,content,hit,writedate) VALUES (?,?,?,0,now())";
		preparedStatement = connection.prepareStatement(insertQuery);
		preparedStatement.setString(1, board.getId());
		preparedStatement.setString(2, board.getTitle());
		preparedStatement.setString(3, board.getContent());
		preparedStatement.executeUpdate();

		preparedStatement.close();
		connection.close();

	}

	public int getBoardCount() throws SQLException, ClassNotFoundException {
		int value = 0;
		connection = dbchoice.makeConnection();
		String countQuery = "SELECT COUNT(*) FROM BOARD";
		preparedStatement = connection.prepareStatement(countQuery);
		resultset = preparedStatement.executeQuery();

		if (resultset.next()) {
			value = resultset.getInt(1);
		}
		return value;
	}

	public Vector getBoard(int startRow, int endRow)
			throws SQLException, ClassNotFoundException {
		boardlist = new Vector();

		connection = dbchoice.makeConnection();
		String pagingQuery = "SELECT * FROM (SELECT @ROWNUM :=@ROWNUM +1 AS ROW, A.* FROM (SELECT * FROM BOARD ORDER BY WRITENUM DESC) A, (SELECT @ROWNUM := 0) B) C WHERE C.ROW BETWEEN ? AND ?";
		preparedStatement = connection.prepareStatement(pagingQuery);
		preparedStatement.setInt(1, startRow);
		preparedStatement.setInt(2, endRow);

		resultset = preparedStatement.executeQuery();

		while (resultset.next()) {
			board = new Board();

			board.setWritenum(resultset.getInt("writenum"));
			board.setId(resultset.getString("id"));
			board.setTitle(resultset.getString("title"));
			board.setContent(resultset.getString("content"));
			board.setHit(resultset.getInt("hit"));
			board.setWritedate(resultset.getTimestamp("writedate"));

			boardlist.add(board);
		}

		preparedStatement.close();
		connection.close();

		return boardlist;

	}

}

'Web > 게시판' 카테고리의 다른 글

결과화면  (0) 2012.08.23
list.jsp 최종  (0) 2012.08.23
BoardDao 추가  (0) 2012.08.23
페이징 할때 쿼리  (0) 2012.08.21
전체글 갯수 구하기  (0) 2012.08.20
Posted by NeverTry