09009

[Java] 고객 관리 (1) - Intellij와 DB 연동, 클래스 생성 본문

Back-End/JAVA
[Java] 고객 관리 (1) - Intellij와 DB 연동, 클래스 생성
09009

작업할 프로젝트에 DB 연동

ojdbc8.jar가 있는 경로를 확인한다.

 

Intellij에서 작업을 진행할 프로젝트를 열고 [File - Project Structure]를 클릭한다.

[Project Structure] 창이 열리면 [Libraries] -  + 버튼 누르고 [Java]를 클릭한다.

아래에 보이는 것과 같이 해당 경로에 있는 ojdbc8.jar 파일을 찾아 클릭한다.

해당 프로젝트(필자의 경우 프로젝트명: customer)에 ojdbc8.jar가 추가될 것이라는 알림창이 뜨면 [OK]를 클릭한다.

계속 [OK]를 클릭하면 아래와 같이 작업할 프로젝트에 ojdbc8.jar가 성공적으로 업로드된다.

!!! module-info.java 파일을 꼭 삭제해주어야 한다. 삭제하지 않으면 실행 시 에러가 발생할 것이다. !!!


customer 테이블 생성 (DB Oracle)

create table customer(
  id varchar2(20) primary key,
  pass varchar2(20),
  email varchar2(20),
  name varchar2(20),
  reg_date date
  );
insert into customer values('haaland','1234','h@h1.com','홀란드',sysdate);
insert into customer values('son','1222','h1@h1.com','손흥민',sysdate);
insert into customer values('kane','1114','h2@h1.com','케인',sysdate);
insert into customer values('ozil','1111','h3@h1.com','외질',sysdate);

commit;
select * from customer;

주의할 점

오라클에서 commit을 반드시 해주어야 java와 연동할 수 있다.

 


 

Java에서 클래스 생성

✍ Customer.java [DTO(Data Transfer Object), VO(Value Object) ]

package CustomerManagement;

import java.util.Date;

public class Customer {
    private String id;
    private String pass;
    private String email;
    private String name;
    private Date reg_date;

    public Customer() {}

    public Customer(String id, String pass, String email, String name) {
        this.id = id; this.pass = pass; this.email = email; this.name = name;
    }
    // 객체를 출력할 형태를 사용자가 지정, toString()을 재정의하지 않으면 패키지명.클래스명@해시코드로 출력됨.
    @Override
    public String toString() {
        return "아이디: " + id + ", 암호: " + pass + ", 이메일: " + email + ", 이름: " + name +", 가입일: " + reg_date;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    public Date getReg_date() {
        return reg_date;
    }

    public void setReg_date(Date reg_date) {
        this.reg_date = reg_date;
    }
}

 

CustomerDaoImpl.java [DAO]

java와 DB 연결을 위해 생성한 클래스이다.  DB 입력/수정/삭제/조회 역할을 수행하기 위해 생성함.

package CustomerManagement;

import java.sql.*;
public class CustomerDaoImpl {
    public Connection getConnection() { // 한 번 연결하면 입력/수정/삭제/조회 공용
        Connection conn = null; // static : 객체를 생성하지 않고 바로 쓸 수 있다. private static Connection conn = null;도 가능
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, "c##scott", "tiger");

        } catch (Exception e) {
            System.out.println("연결 에러발생 : " + e.getMessage());
        }
        return conn;
    }
}

 

CustomerServiceImpl.java

업무처리를 위해 만든 클래스

package CustomerManagement;

// 업무처리 클래스
public class CustomerServiceImpl {
    private static CustomerDaoImpl cd = new CustomerDaoImpl();
}

 

Ex01.java

사용자에게 입력,수정,삭제, 조회 요청을 받는 클래스

package CustomerManagement;

// 입력,수정,삭제, 조회 요청을 받는 클래스
// ex) 주문을 받는 클래스
public class Ex01 {
    private static CustomerServiceImpl cs = new CustomerServiceImpl();
    public static void main(String[] args) {

    }
}