[JDBC] DAO, DTO Concepts and DML Code Structure

· Tech· Database
Database

Continuing from the previous article, we summarized the practical use of DML queries using JDBC.

How should we configure program when importing information from Oracle into JDBC? First of all, I think it would be better to grasp the concepts of DAO and DTO among the MCV patterns first.

As shown in the picture, starting with the Main execution class, it can be divided into Controller, which manages execution menus, and DAO, which executes SQL statements and handles detailed methods. At this time, a DTO that transfers the property values ​​of the object exists. This is a software design format.

So what are DAO and DTO?

DAO Data Access Object / DTO Data Transfer Object (= VO Value Object)

DAO is an object with a kind of Connection Pool rather than making a connection every time a connection to retrieve information from the DB occurs, and is a means of preventing overhead. Simply put, as an example, DAO can be said to be an object that contains methods that connect to the DB and retrieve the member list if the value executed in the menu viewed by the user is member inquiry.

On the other hand, DTO is called Java Beans for data exchange between layers. It is an object composed of property values ​​to be passed for inter-layer data connection and getters and setters. 


Then, if you want to view JBDC member information using the actual DB in Oracle, how should you configure code?

I would like to explain by looking at the code structure for each class.

(The examples below are for illustrative purposes only, so developers can freely write code for each class.)


To explain according to the graph above, there will be a menu of the execution part in Main. The code structure is roughly as follows, creating a Controller object and allowing the Controller method to be executed. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Controller ct = new Controller();
        Scanner sc = new Scanner(System.in);
        
        ct.menu(sc);
 
        sc.close();
    }
}
 
cs

 


 

The important thing here is the Controller object. It allows most rough methods to be connected, and the overall program structure is created here.

In the code below, only the core parts were written, excluding variable processing for input values (letter input other than Y, N, etc.). The Controller method menu(sc); executed in Main is a menu that asks whether to search for members. If Y is input here, the next method showInfo(sc); is executed. showInfo(sc); The method is defined below and receives the value from DTO in list format (getter) and outputs it.

List userList = udao.showInfo();

Here, udao is an object of userDAO, and you can see that the return value is in List format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.*;
public class Controller {
· interUserDAO udao = new userDAO();
String menuNo;
· public void menu(Scanner sc) {
        System.out.@@T407 @@println("회원을 조회하시겠습니까? [Y/N] “);
        String answer = sc.nextLine();    
 
        switch(answer) {
            case "Y":
                showInfo(sc);
            case "N":
                showMenu(sc);
        }
            
    }
 
 
    private void showInfo(Scanner sc) {       
        List<userDTO> userList = udao.showInfo();
      
        if(userList.size()>0) {
            for(userDTO noon : userList) {
                 System.out.println("회원명   나이    성별    전화번호    이메일");
                System.out.println(noon.getName()  noon.getAge()     noon.getGender()    noon.getMobile()      noon.getEmail());
            }
           } else
            System.out.println("회원이 없스트.")
    }
}    
cs


 

Transfer is the same as the getter, setter is the same. DTO Geist East static I'm going to do this I'm going to go to I'm going to be I'm going to be I'm going to be I'm going to be I'm going to be I'm going to be I'm going to be I'm going to be I'm going to be I'm going to be I'm going to be I'm going to be able to do this I'm going to be able to do this I'm going to be able to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class userDTO {
static String name;
static int age;
static String gender;
static String mobile;
static String email;
public static String getName() {
return name;
{ }
public static void setName(String name) {
… userDTO.name = name;
{ }
    public static int getAge() {
        return age;
    }
    public static void setAge(int age) {
        userDTO.age = age;
    }
    public static String getGender() {
        return gender;
    }
    public static void setGender(String gender) {
        userDTO.gender = gender;
    }
    public static String getMobile() {
        return mobile;
    }
    public static void setMobile(String mobile) {
        userDTO.mobile = mobile;
    }
    public static String getEmail() {
        return email;
    }
    public static void setEmail(String email) {
        userDTO.email = email;
    }
    
}
 
cs

 


 

Looking briefly at

interfaceDAO, it was created as a class interDAO for implementation when creating a userDAO object in the Controller. In other words, when you create a method in the Controller, an abstract method that defines the method is created in the interface, as shown below.

1
2
3
public interface interUserDAO {
        List<userDTO@@ T920@@> showInfo(Scanner sc);
}       
cs


Now it is finally time to override the abstract method of interface in DAO. This is a class that is connected to actual SQL statements.

First, List showInfo(Scanner sc); If you think of the SQL statement for executing the method, it will be entered as follows. (The examples below are all arbitrary values.) Since it is a select statement, it is a command control word DML. This SQL statement will have one or more values, and can be retrieved as a String value from JDBC and stored as each property value. Saving will be done from DTO to setter and output will be done from Controller to getter. 

1
2
3
select name, age, gender, moblie, email
from tbl_info
order by name;
cs

Then, let's look at the code structure of the DAO class. As you can see from the previous link below, the code that applies SQL statements to Eclipse is written. 

2021.03.16 - [Back-end developer study/Java + JDBC] - [JDBC] Applying SQL statements in Eclipse (DML, DDL, DCL, TCL)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Override
public  List<userDTO> showInfo() {
    List<userDTO> udto = null;
             
     try {
          conn = dbConnection.getConn();
          
          String sql = " select name, age, gender, moblie, email "
                     + " from tbl_info order by name ";
     
         
          pstmt = conn.prepareStatement(sql);
          rs = pstmt.executeQuery();
                     
          while(rs.next()) {
              udto.setName(rs.getString(1));
              udto.setAge(rs.getString(2));
              udto.setGender(rs.getString(3));
              udto.setMobile(rs.getString(4));
              udto.setEmail(rs.getString(5));
          }    
 
     } catch (SQLException e) { 
          e.printStackTrace();
     } finally {
        rs.close();
        pstmt.close();
        conn.close();
     }      
     return udto;
  }
cs

Comments

No comments yet. Be the first!

    164 posts in 테크

    15 / 164