2017년 1월 23일 월요일

[자바교육/스프링교육/스프링프레임워크/스프링부트학원추천_탑크리에듀]JdbcTemplate.queryForObject

public Person selectPersonObject(String id) { 
String sql = "" + "SELECT * FROM person WHERE id = ? "; 
Object[] args = new Object[] { id }; 
Person person = (Person) this.jdbcTemplate.query(sql, args, 
new RowMapper() { 
public Object mapRow(ResultSet rs, int index) 
throws SQLException { 
Person person = new Person(); 
person.setId(rs.getString("id")); 
person.setPersonName(rs.getString("personname")); 
person.setZipCode(rs.getString("zipcode")); 
person.setZipAddress(rs.getString("zipaddress")); 
person.setUserAddress(rs.getString("useraddress")); 
return person; 

}); 
return person; 
}//: 


JdbcTemplate.queryForXXX(Quesy가 단순한 타입을 리턴만 하는 경우)  

public int selectInt() { 
  String sql = " SELECT count(*) FROM emp "; 
  return this.jdbcTemplate.queryForInt(sql); 


public long selectLong() { 
String sql = " SELECT longCol FROM emp"; 
return this.jdbcTemplate.queryForLong(sql); 


public Date selectDate() { 
String sql = " SELECT sysdate FROM dual"; 
Date d  = (Date)this.jdbcTemplate.queryForObject(sql, Date.class); 
return d; 

댓글 없음:

댓글 쓰기