JDBCを使いjoinして手に入れた値をentityに格納する方法
現在JDBCを初学習しています。
例えばselect文を実行し帰ってきた値をentityクラスを作成して格納すると思いますがそのやり方について質問したいことがあります
まずは下記コードを御覧ください
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import co.jp.*******.jdbc.entites.EmployeeBasic;
import co.jp.aaaaaaa.jdbc.entites.EmployeeSkill;
public class AppEntry {
public static void main(String[] args) {
try {
connectionTest();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void connectionTest() throws ClassNotFoundException, SQLException {
Connection conn = null;
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "postgres");
//insertTest(conn);
selectTest(conn);
}
public static List<EmployeeBasic> selectTest(Connection conn) throws SQLException {
Statement stmt = null;
ResultSet rs = null;
stmt = conn.createStatement();
rs = stmt.executeQuery("select\r\n" +
"*\r\n" +
"from\r\n" +
"employee_basic\r\n" +
"left join\r\n" +
"employee_skill\r\n" +
"on\r\n" +
"employee_basic.employee_no = employee_skill.employee_no\r\n");
List<EmployeeBasic> result = new ArrayList<>();
EmployeeBasic preEntity = null;
EmployeeBasic entity = null;
while (rs.next()) {
if(preEntity == null) {
entity = new EmployeeBasic();
entity.employeeNo = rs.getLong("employee_no");
entity.employeeName = rs.getString("employee_name");
result.add(entity);
preEntity = entity;
} else if(preEntity.employeeNo != rs.getLong("employee_no")) {
entity = new EmployeeBasic();
entity.employeeNo = rs.getLong("employee_no");
entity.employeeName = rs.getString("employee_name");
result.add(entity);
preEntity = entity;
}
EmployeeSkill skill = new EmployeeSkill();
skill.skillName = rs.getString("skill_name");
entity.skills.add(skill);
}
return result;
}
}
entityはこちらです
※EntitySkillというentityもありますがフィールド変数のみなので省略します
package co.jp.*******
import java.util.ArrayList;
import java.util.List;
public class EmployeeBasic {
public long employeeNo;
public String employeeName;
public List<EmployeeSkill> skills = new ArrayList<>();
}
SQLはこちらです
create table employee_basic (
employee_no bigint,
employee_name varchar(32),
primary key(employee_no)
)
create table employee_skill (
employee_skill_lnk serial,
employee_no bigint,
skill_name varchar(32),
primary key(employee_skill_lnk)
)
insert into employee_basic values (1, 'user1')
insert into employee_basic values (2, 'user2')
insert into employee_skill (employee_no, skill_name) values (1, 'sql')
insert into employee_skill (employee_no, skill_name) values (1, 'java')
insert into employee_skill (employee_no, skill_name) values (1, 'cpp')
insert into employee_skill (employee_no, skill_name) values (1, 'c#')
select
*
from
employee_basic
left join
employee_skill
on
employee_basic.employee_no = employee_skill.employee_no
このコードのselectTest(){}のwhile()の部分を説明していただけないでしょうか?何故結合されたテーブルの初めの一行をentityに追加するのか、またその後のelse ifについても解説をお願いします。
よろしくおねがいします!