[java] Apache Derby를 사용하여 데이터베이스 테이블 조인을 수행하는 방법은 무엇인가요?
아래는 Derby를 사용하여 데이터베이스 테이블 조인을 수행하는 예시 코드입니다:
import java.sql.*;
public class DerbyJoinExample {
public static void main(String[] args) {
Connection connection = null;
try {
// Derby 데이터베이스에 연결
connection = DriverManager.getConnection("jdbc:derby:myDatabase");
// 조인을 수행할 SQL 쿼리 작성
String sql = "SELECT * FROM Table1 INNER JOIN Table2 ON Table1.id = Table2.id";
// 쿼리 실행
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
// 결과 처리
while (resultSet.next()) {
// 결과 가져오기
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 결과 출력
System.out.println("ID: " + id + ", Name: " + name);
}
// 자원 해제
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 연결 종료
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
위의 예시 코드는 Derby 데이터베이스에 연결하고, Table1과 Table2 두 테이블을 조인하여 결과를 가져오는 방법을 보여줍니다. 테이블 간의 조인 조건은 ON
절에 지정하면 됩니다.
자세한 내용은 Apache Derby 공식문서를 참고하시기 바랍니다.