BoardDao.java
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(); Vectorboardlist; 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 Vector list() throws ClassNotFoundException, SQLException{ boardlist = new Vector (); 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(); return boardlist; } 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; } }
'Web > 게시판' 카테고리의 다른 글
BoardDao 추가 (0) | 2012.08.23 |
---|---|
페이징 할때 쿼리 (0) | 2012.08.21 |
list.jsp 및 결과 (0) | 2012.08.19 |
BoardDao 추가 (0) | 2012.08.19 |
BoardDao (0) | 2012.08.19 |