Web/게시판2012. 8. 19. 11:20

글을 썼다면 간단한 내용으로 게시판의 화면에 보여질 글을 읽어와야한다.

즉. 글번호라든지 글제목 글쓴이 등 이 부분을 게시판에 보여줘야한다.

이부분을 BoardDao에 추가한다.

추가된 메소드



완성된 BoardDao

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){
		board = (Board) object;
		try{
		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();
		
		}catch(SQLException e){
			e.printStackTrace();
		}catch(ClassNotFoundException cnfe){
			cnfe.printStackTrace();
		}
		
	}
	
	public Vector list(){
		boardlist = new Vector();
		
		try {
			connection = dbchoice.makeConnection();
			String query = "SELECT * FROM BOARD";
			preparedStatement = connection.prepareStatement(query);
			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();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}catch(ClassNotFoundException cnfe){
			cnfe.printStackTrace();
		}
		
		return boardlist;
	}
}


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

전체글 갯수 구하기  (0) 2012.08.20
list.jsp 및 결과  (0) 2012.08.19
BoardDao  (0) 2012.08.19
write.jsp  (0) 2012.08.19
수정  (0) 2012.08.19
Posted by NeverTry