How to use JDBC to join and store values in entity

Asked 2 years ago, Updated 2 years ago, 97 views

I am currently learning JDBC for the first time.

For example, if you run a select statement and create an entity class to store the returned values, I would like to ask you a question about how to do it.
Please see the code below first

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 | SQLExceptione){
        // 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)throwsSQLException{
    Statement stmt = null;
    ResultSetters = null;

    stmt = conn.createStatement();
    rs = stmt.executeQuery("select\r\n" + 
            US>"*\r\n" + 
            "from\r\n" + 
            "employee_basic\r\n" + 
            "left join\r\n" + 
            US>"employee_skill\r\n" + 
            US>"on\r\n" + 
            "employee_basic.employee_no=employee_skill.employee_no\r\n";

    List<EmployeeBasic>result=new ArrayList<>();

    EmployeeBasic preEntity = null;
    EmployeeBasicity=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 is here
EnThere is also an entity called EntitySkill, but it is omitted because it is only a field variable.

package co.jp.*******

import java.util.ArrayList;
import java.util.List;

public class EmployeeBasic {

public long employeeNo;
public String employeeName;


publicList<EmployeeSkill>skills=new ArrayList<>();
}

SQL is here

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

Could you explain the while() part of selectTest(){} of this code?Please explain why you add the first line of the combined table to entity and the else if after that follows.
Thank you for your cooperation!

java sql postgresql mvc jdbc

2022-09-29 20:25

1 Answers

I'll explain it in pictures.

Based on these DB search results (records),

Enter a description of the image here

You are trying to create such a Java object.

Enter a description of the image here

Compare the picture with the source code to understand the intent of the source code.

The first if statement determines the first line of the search results, and the second if statement ( else if...) determines the first record of a different employee (employeeNo).

By the way, these two conditions are the same, so they can be combined into one.


2022-09-29 20:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.