1편에서 한 내용


Oracle 사용자 계정만들기 , DB연결하기 , INSERT랑 DELETE 하기




INSERT , DELETE , UPDATE , SELECT (SELECT는 scott 계정에 맞게 작성하였음)

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
 
import config.DBConnection;
import model.Dept;
 
public class MainApp {
 
    public static void main(String[] args) {
//        추가(9);
//        삭제(1);
        찾기(10);
    }
 
    // 함수로 모듈화
    public static void 추가(int id) {
//        String sql = "INSERT INTO test1(id) VALUES("+id+")"; //이렇게하면 인젝션 뚫림
        String sql = "INSERT INTO test1(id) VALUES(?)";
        Connection conn = DBConnection.getinstance();
        // Byte Stream (?)
 
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id); // 첫번째 '?' 부분에 id를 넣겠다.
            int result = pstmt.executeUpdate(); // 변경된 row count를 리턴, 오류 시 -1를 리턴
            System.out.println("result : " + result);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    // 함수로 모듈화
    public static void 삭제(int id) {
        String sql = "DELETE FROM test1 WHERE id = ?";
        Connection conn = DBConnection.getinstance();
        // Byte Stream (?)
 
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id); // 첫번째 '?' 부분에 id를 넣겠다.
            int result = pstmt.executeUpdate(); // 변경된 row count를 리턴, 오류 시 -1를 리턴
            System.out.println("result : " + result);
//            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    // 함수로 모듈화
    public static void 수정(int id) {
        String sql = "UPDATE test1 SET 4 WHERE id = ?";
        Connection conn = DBConnection.getinstance();
        // Byte Stream (?)
 
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id); // 첫번째 '?' 부분에 id를 넣겠다.
            int result = pstmt.executeUpdate(); // 변경된 row count를 리턴, 오류 시 -1를 리턴
            System.out.println("result : " + result);
//            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    // 함수로 모듈화 , 사용자 scott으로 변경해야함
    // return값 Dept
    public static Dept 찾기(int deptno) {
        String sql = "SELECT deptno, dname, loc FROM dept WHERE deptno=?";
        Connection conn = DBConnection.getinstance();
        // Byte Stream (?)
 
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, deptno); // 첫번째 '?' 부분에 id를 넣겠다.
            ResultSet rs = pstmt.executeQuery();
            if(rs.next()) {
                Dept dept = Dept.builder()
                        .deptno(rs.getInt("deptno"))
                        .dname(rs.getString("dname"))
                        .loc(rs.getString("loc"))
                        .build();
                System.out.println(dept);
                return dept;
//                int deptno2 = rs.getInt("deptno");
//                String dname = rs.getString("dname");
//                String loc = rs.getString("loc");
//                System.out.println(deptno2);
//                System.out.println(dname);
//                System.out.println(loc);
            }
 
//            int result = pstmt.executeUpdate(); // 변경된 row count를 리턴, 오류 시 -1를 리턴
//            System.out.println("result : " + result);
//            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    
    
    // 함수로 모듈화 , 사용자 scott으로 변경해야함
    // return값 Dept
    public static List<Dept> 전체찾기() {
        String sql = "SELECT deptno, dname, loc FROM dept WHERE deptno=?";
        Connection conn = DBConnection.getinstance();
        // Byte Stream (?)
 
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
//            pstmt.setInt(1, deptno); // 첫번째 '?' 부분에 id를 넣겠다.
            ResultSet rs = pstmt.executeQuery();
            if(rs.next()) {
                Dept dept = Dept.builder()
                        .deptno(rs.getInt("deptno"))
                        .dname(rs.getString("dname"))
                        .loc(rs.getString("loc"))
                        .build();
                System.out.println(dept);
                
//                return dept;
//                int deptno2 = rs.getInt("deptno");
//                String dname = rs.getString("dname");
//                String loc = rs.getString("loc");
//                System.out.println(deptno2);
//                System.out.println(dname);
//                System.out.println(loc);
            }
 
//            int result = pstmt.executeUpdate(); // 변경된 row count를 리턴, 오류 시 -1를 리턴
//            System.out.println("result : " + result);
//            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
 
cs


모델 먼저 만드세요. [ 만드는이유 : 관리하기 편하기위해서 ? ]


참고로 올리는 scott 스키마 사진


▼Dept 클래스를 모델 패키지에 만든 모습

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package model;
 
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
 
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Dept {
    private int deptno;
    private String dname;
    private String loc;
}
 
cs


lombok.jar 다운로드


https://mvnrepository.com/artifact/org.projectlombok/lombok

알아서 빌드패스




이번에 해볼 일 : 전체찾기 (SELECT)


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
42
43
44
45
46
47
48
49
50
51
52
53
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import config.DBConnection;
import model.Dept;
 
public class MainApp {
 
    // 함수로 모듈화 , 사용자 scott으로 변경해야함
    // return값 List<Dept>
    public static List<Dept> 전체찾기() {
        String sql = "SELECT deptno, dname, loc FROM dept";
        Connection conn = DBConnection.getinstance();
        // Byte Stream (?)
 
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            
            //List 하위에 vector , ArrayList가 있음, 선언은 반복문위에
            List<Dept> listDept = new ArrayList<Dept>();
            while(rs.next()) {
                Dept dept = Dept.builder()
                        .deptno(rs.getInt("deptno"))
                        .dname(rs.getString("dname"))
                        .loc(rs.getString("loc"))
                        .build();
                System.out.println(dept); //toString() 작동 ?
                
                //컬렉션에 담기
                listDept.add(dept);
            }
            return listDept;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    public static void main(String[] args) {
//        추가(9);
//        삭제(1);
//        찾기(10);
//        Dept dept = 찾기(10);
        List<Dept> listDept = 전체찾기();
    }
}
 
cs

메서드 전체찾기()만 보면 됩니다.

+ Recent posts