<aside> 💡 SQL 세미나 때 만든 테이블을 이용하여, JDBC를 복습해본다!

</aside>

Employee.java

public class Employee {

    private String code;
    private String name;
    private String mgt;
    private String sal;
    private String rcode;

    public Employee(String code, String name, String mgt, String sal, String rcode) {
        this.code = code;
        this.name = name;
        this.mgt = mgt;
        this.sal = sal;
        this.rcode = rcode;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMgt() {
        return mgt;
    }

    public void setMgt(String mgt) {
        this.mgt = mgt;
    }

    public String getSal() {
        return sal;
    }

    public void setSal(String sal) {
        this.sal = sal;
    }

    public String getRcode() {
        return rcode;
    }

    public void setRcode(String rcode) {
        this.rcode = rcode;
    }
}

DB 연결을 위한 PostgresqlAccess.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class PostgresqlAccess {
    private static Connection conn = null;

    public void init() {
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection setConnection() {
        String url = "jdbc:postgresql://db.fofcktuathtloqpbahkn.supabase.co:5432/postgres";
        String username = "postgres";
        String password = "pighoney5026@";

        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }
}

Main.java

→ 전체 관계자를 검색하시오.

public class Main {

    public static void main(String[] args) {
        EmployeeSelectService selectEmployee = new EmployeeSelectService();

        System.out.println("전체 관계자를 검색합니다.");
        selectEmployee.getEmployeeAll();
        System.out.println();
    }
}

EmployeeSelectService.java

getEmployeeAll()

public void getEmployeeAll() {

		Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    try {
		    conn = PostgresqlAccess.setConnection();
        conn.setAutoCommit(false);

        String query = "SELECT * FROM EMPLOYEE";
        pstmt = conn.prepareStatement(query);
        rs = pstmt.executeQuery();

        while (rs.next()) {
		        System.out.print("[관계자 코드] " + rs.getString(1) + " || ");
            System.out.print("[관계자 이름] " + rs.getString(2) + " || ");
            System.out.print("[관계자 관리자] " + rs.getString(3) + " || ");
            System.out.print("[관계자 급여] " + rs.getInt(4) + " || ");
            System.out.println("[직급 코드] " + rs.getString(5));
        }
    } catch (SQLException sqex) {
		    System.err.println("SQLException: " + sqex.getMessage());
        System.err.println("SQLState: " + sqex.getSQLState());
    } finally {
		    if (rs != null) {
		        try {
								rs.close();
						} catch (Exception e) {
								e.printStackTrace();
						}
		    }
		
		    if (pstmt != null) {
		        try {
								pstmt.close();
						} catch (Exception e) {
								e.printStackTrace();
						}
		    }
		
		    if (conn != null) {
		        try {
								conn.close();
						} catch (Exception e) {
								e.printStackTrace();
						}
		    }
    }
}

실행 결과

Untitled