TIP/JSP

JDBC 관련

devjjin 2018. 9. 26. 18:28

Statement

실제 DB에 쿼리를 보내기 위한 객체




ResultSet

Satatement, PreparedStatement 객체로 select문을 사용해 얻어온 레코드 값들을 테이블 형태로 갖게 되는 객체


public List<SaramDto> selectAll() throws SQLException {

List<SaramDto> list = new ArrayList<>();


Statement stmt = null;

ResultSet rs = null;


try {

stmt = conn.createStatement();

rs = stmt.executeQuery(sql);


// 레코드가 존재하지 않을 때까지 계속 레코드 행을 다음으로 이동하며 레코드 값 가져옴

while (rs.next()) {

String id = rs.getString(1);

String name = rs.getString(2);

int age = rs.getInt(3);


SaramDto saramDto = new SaramDto(id, name, age);


list.add(saramDto);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (rs != null) {

rs.close();

}

if (stmt != null) {

stmt.close();

}

}

return list;

}