소스코드 다운받기

context.xml에 추가하기

<Resource
	auth="Container"
	driverClassName="oracle.jdbc.driver.OracleDriver"
	maxActive="50"
	maxWait="1000"
	name="jdbc/Oracle11g"
	type="javax.sql.DataSource"
	url="jdbc:oracle:thin:@localhost:1521:xe"
	username="데이터베이스 username"
	password="데이터베이스 비밀번호"
/>

18-0 list 페이지 만들기

DAO 설정

  1. 생성자 만들기. 데이터 소스를 구함
  2. list 메서드에서 sql 질의
// BDao.java

public class BDao {

	DataSource dataSource;

	public BDao(){ // 1
		try{
			Context context = new InitialContext();
			dataSource = (DataSource)context.lookup("java:comp/env/jdbc/Oracle11g");
		} catch (NamingException e){
			e.printStackTrace();
		}
	}

	public ArrayList<BDto> list(){ // 2

		ArrayList<BDto> dtos = new ArrayList<BDto>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try{
			connection = datasource.getConnection();
			String query = "select bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent from mvc_board order by bGroup desc, bStep asc";
			preparedStatement = connection.preparedStatement(query);
			resultSet = preparedStatement.executeQuery();

			while(resultSet.next()){
				int bId = resultSet.getInt("bId");
				String bName = resultSet.getString("bName");
				String bTitle = resultSet.getString("bTitle");
				String bContent = resultSet.getString("bContent");
				Timestamp bDate = resultSet.getTimestamp("bDate");
				int bHit = resultSet.getInt("bHit");
				int bGroup = resultSet.getInt("bGroup");
				int bStep = resultSet.getInt("bStep");
				int bIndent = resultSet.getInt("bIndent");

				BDto dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
				dtos.add(dto);
			}

		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try{
				if(resultSet != null) resultSet.close();
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch(Exception e2) {
				e2.printStackTrace();
			}
		}
		return dtos;
	}

}

list.jsp 만들기

  • 지금 중요한 내용이 아니니 복붙

18-1 글 작성 페이지 만들기

Bcontroller.class 설정


@RequestMapping("/write_view")
public String write_view(Model model){
	System.out.println("write_view()");

	return "write_view";
}

@RequestMapping("/write")
public String write(HttpServletRequest request, Model model){
	System.out.println("write()");

	model.addAttribute("request", request);
	command = new BWriteCommand();
	command.execute(model);

	return "redirect:list";
}

BWriteCommand.class 설정


@Override
public void execute(Model model){

	Map<String, Object> map = model.asMap();
	HttpServletRequest request = (HttpServletRequest) map.get("request");
	String bName = request.getParameter("bName");
	String bTitle = request.getParameter("bTitle");
	String bContent = request.getParameter("bContent");

	BDao dao = new BDao();
	dao.write(bName, bTitle, bContent);

}

BDao.java 설정

public class BDao {

	DataSource dataSource;

	// 생략

	public void write(String bName, String bTitle, String bContent){

		Connection connection = null;
		PreparedStatement preparedStatement = null;

		try{
			connection = datasource.getConnection();
			String query = "insert into mvc_board (bId, bName, bTitle, bContet, bDate, bHit, bGroup, bStep, bIndent) values(mvc_board_seq.nextval, ?, ?, ?, SYSDATE, 0, mvc_board_seq.currval, 0, 0)";
			preparedStatement = connection.preparedStatement(query);
			preparedStatement.setString(1, bName);
			preparedStatement.setString(2, bTitle);
			preparedStatement.setString(3, bContent);

			// 받은 정보 DB에 업데이트
			int rn = preparedStatement.executeUpdate();

		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try{
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch(Exception e2) {
				e2.printStackTrace();
			}
		}
	}

}

write_view.jsp

18-2 글 내용 페이지 만들기

BController 작성

	@RequestMapping("/content_view")
	public String content_view(HttpServletRequest request, Model model){
		System.out.println("content_view()");
		
		model.addAttribute("request", request);
		command = new BContentCommand();
		command.execute(model);
		
		return "content_view";
	}

BContentCommand 작성

public class BContentCommand implements BCommand {

	@Override
	public void execute(Model model) {
		// TODO Auto-generated method stub

		Map<String, Object> map = model.asMap();
		HttpServletRequest request = (HttpServletRequest) map.get("request");
		String bId = request.getParameter("bId");
		
		BDao dao = new BDao();
		BDto dto = dao.contentView(bId);
		
		model.addAttribute("content_view", dto);
		
	}

}

BDao - contentView 작성

public BDto contentView(String strID) {
		// TODO Auto-generated method stub
		
		upHit(strID);
		
		BDto dto = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		try {
			
			connection = dataSource.getConnection();
			
			String query = "select * from mvc_board where bId = ?";
			preparedStatement = connection.prepareStatement(query);
			preparedStatement.setInt(1, Integer.parseInt(strID));
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next()) {
				int bId = resultSet.getInt("bId");
				String bName = resultSet.getString("bName");
				String bTitle = resultSet.getString("bTitle");
				String bContent = resultSet.getString("bContent");
				Timestamp bDate = resultSet.getTimestamp("bDate");
				int bHit = resultSet.getInt("bHit");
				int bGroup = resultSet.getInt("bGroup");
				int bStep = resultSet.getInt("bStep");
				int bIndent = resultSet.getInt("bIndent");
				
				dto = new BDto(bId, bName, bTitle, bContent, bDate, bHit, bGroup, bStep, bIndent);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				if(resultSet != null) resultSet.close();
				if(preparedStatement != null) preparedStatement.close();
				if(connection != null) connection.close();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		return dto;
	}

참고자료