
Result::getWarnings 메서드는 SQL 쿼리 실행 시 발생하는 경고 메시지를 반환합니다. 이 메서드는 QueryException 객체를 반환하는데, QueryException 객체에는 경고 메시지와 함께 오류 코드가 포함됩니다.
Connection 객체가 null 인 경우, getWarnings 메서드를 호출할 때 NullPointerException이 발생합니다. 따라서 Connection 객체를 null 체크하는 것이 좋습니다.
경고 메시지가 없을 경우, getWarnings 메서드는 null을 반환합니다. 따라서 null 체크를 하여 오류를 방지할 수 있습니다.
예외 처리 방법은 다음과 같습니다.
#hostingforum.kr
java
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "user", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM 테이블명");
// 경고 메시지 얻기
QueryException qe = (QueryException) conn.getWarnings();
if (qe != null) {
System.out.println("경고 메시지: " + qe.getMessage());
System.out.println("오류 코드: " + qe.getErrorCode());
} else {
System.out.println("경고 메시지가 없습니다.");
}
// Connection 객체가 null 인 경우
if (conn == null) {
System.out.println("Connection 객체가 null 인 경우");
} else {
// 경고 메시지 얻기
qe = (QueryException) conn.getWarnings();
if (qe != null) {
System.out.println("경고 메시지: " + qe.getMessage());
System.out.println("오류 코드: " + qe.getErrorCode());
} else {
System.out.println("경고 메시지가 없습니다.");
}
}
위 예시를 통해 Result::getWarnings 메서드를 사용하여 SQL 쿼리 실행 시 발생한 경고 메시지를 얻을 수 있습니다. 또한, Connection 객체가 null 인 경우와 경고 메시지가 없을 경우에 대한 예외 처리 방법을 보여줍니다.
2025-05-24 08:30