[Docker/MySQL] JDBC - PreparedStatement ์‹ค์Šต

2020. 12. 31. 21:05ใ†๐Ÿ™‹๐Ÿป‍โ™€๏ธ Study/โœ”๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ( Database )

๋ฐ˜์‘ํ˜•

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ €๋ฒˆ ๊ธ€์— ์ด์–ด PreparedStatement ์‹ค์Šต์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Docker์™€ MySQL์„ ์ด์šฉํ•œ JDBC ๊ธฐ๋ณธ ์‹ค์Šต ๊ณผ์ •์€ ์•„๋ž˜ ๊ธ€์—์„œ ์„ค๋ช…ํ•˜์—ฌ ์ƒ๋žตํ•˜์˜€์Šต๋‹ˆ๋‹ค.

JDBC ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•๊ณผ Statement ์‹ค์Šต์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•˜์‹œ๋‹ค๋ฉด ์•„๋ž˜๊ธ€์„ ๋จผ์ € ์ฝ๊ณ  ์˜ค์‹œ๋ฉด ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค : )

 

[Docker/MySQL] JDBC - Statement ์‹ค์Šต

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Docker์— Ubuntu๋ฅผ ๋งŒ๋“ค์–ด, Tomcat8์„ ์ด์šฉํ•˜์—ฌ JDBC ์˜ Statement ์‹ค์Šต์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. JDBC ์‹ค์Šต์„ ์›ํ• ํ•˜๊ฒŒ ํ•˜์‹œ๋ ค๋ฉด MySQL๊ณผ Launchpad(test-db)๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์•„๋ž˜์˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜..

yunaaaas.tistory.com

 

Statement ์™€ PreparedStatement ์ฐจ์ด์ 

SQL์€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ๊ตฌ์กฐ๋กœ, ๋งค ๋ผ์ธ์„ ํŒŒ์‹ฑ์„ ๊ฑฐ์ณ ์‹คํ–‰ํ•˜๋Š” ๋ฐ ๋™์ผํ•œ SQL์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•  ๋•Œ Statement์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์—ฌ๋Ÿฌ๋ฒˆ ํŒŒ์‹ฑํ•œ ํ›„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋˜๊ณ , SQL Injection์— ์ทจ์•ฝํ•˜๋‹ค.

ํ•˜์ง€๋งŒ PreparedStatement๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฏธ๋ฆฌ SQL๋ฌธ์„ DBMS์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋˜์–ด ( Pre-compiled SQL ) Statement์˜ SQL Injection ๋ฌธ์ œ๋ฅผ ์˜ˆ๋ฐฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Statement๋Š” Dynamic SQL / PreparedStatement๋Š” Static SQL ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค : )

Prepared Statement๋Š” ๋ฏธ๋ฆฌ ํŒŒ์‹ฑํ•˜์—ฌ Pre - Compile ํ•ด๋†“๊ณ  ํ˜ธ์ถœ๋งŒ ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ํ˜•ํƒœ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

SQL Injection์ด๋ž€?

SELECT * FROM Users WHERE id = ' ' OR 1=1; ๊ณผ ๊ฐ™์ด where์ ˆ ๊ฐ’์— 'OR 1 = 1' ์„ ์ž…๋ ฅํ•˜์—ฌ ๋ชจ๋“  where ์ ˆ์„ ์ฐธ์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒฝ์šฐ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

Prepared Statement๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ด์Šค์ผ€์ดํ”„์ฒ˜๋ฆฌ(ํŠน์ • ๋ฌธ์ž ๊ฐ’์„ ๋‹ค๋ฅธ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ)๋ฅผ ํ•˜์—ฌ SQL Injection์„ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค !

 

JDBC ๋“œ๋ผ์ด๋ฒ„ ์„ค์น˜ ๋ฐ Tomcat8 ์„ค์น˜๋Š” ์ด์ „ ๊ธ€์—์„œ ์ž์„ธํžˆ ๋‹ค๋ค˜์œผ๋ฏ€๋กœ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ“Œ JDBC PreparedStatement jsp ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

๋จผ์ € ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•œ ubuntu๋ฅผ ์‹คํ–‰ํ•ด์ค€๋‹ค.

docker exec -it ubuntu_new1 bash

 

 

tomcat์˜ ํ™ˆ์œผ๋กœ ์ด๋™ํ•œ๋’ค, prestatement.jsp ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

cd /var/lib/tomcat8/webapps/ROOT
nano prestatement.jsp

 

๐Ÿ“Œ PreparedStatement.jsp ์ž‘์„ฑํ•˜๊ธฐ

๊ฒฐ๊ณผ๋ฅผ ๋ฐ”๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ฒŒ Statement๋ฌธ๋„ ๋งŒ๋“ค์–ด ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.

<%@ page import = "java.sql.*" %>
<% PreparedStatement pstmt = null; ResultSet rs = null; Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://172.17.0.2:3306/employees";
Connection conn = DriverManager.getConnection(dbUrl,"root","yunas");
String SQL = "insert into employees (emp_no,birth_date,first_name,last_name,gender,hire_date) values (?,?,?,?,?,?);";
try {
        pstmt= conn.prepareStatement(SQL);
        pstmt.setInt(1,10000);
        pstmt.setString(2,"2020-12-31");
        pstmt.setString(3,"kim");
        pstmt.setString(4,"yuna");
        pstmt.setString(5,"F");
        pstmt.setString(6,"2021-01-01");
        pstmt.executeUpdate();
        out.println("recored Insert!");

        stmt = conn.createStatement();
        if (stmt.execute("select * from employees where emp_no = 10000;")) {
                rs = stmt.getResultSet();
        }
        while(rs.next()) {
                out.println("emp_no : "+rs.getInt("emp_no"));
                out.println("birthdate : " + rs.getDate("birth_date"));
                out.println("name : " + rs.getString("first_name") + " " + rs.getString("last_name"));
                out.println("gender : " + rs.getString("gender"));
                out.println("hiredate : " + rs.getDate("hire_date"));
        }
        rs.close(); pstmt.close(); stmt.close();
} catch(Exception e) {
        out.println("PreparedStatement Error!"+e);
}
conn.close();%>

 

Statement๊ณผ ๋‹ค๋ฅด๊ฒŒ  PreparedStatement๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , 

SQL๋ฌธ์žฅ์„ ์‹คํ–‰ํ•  ๋•Œ pstmt.executeQuery() (select) / pstmt.executeUpdate() (insert , update, delete ... ) ๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค!

 

 

 

๐Ÿ“Œ Prepared Statement ์‹คํ–‰ ๊ฒฐ๊ณผ

localhost:8080/prestatement.jsp

 

โœ”๏ธ์ฃผ์˜ํ•ด์•ผํ•  ์  !

PreparedStatement์— Insert๋ฌธ์„ ๋„ฃ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— localhost:8080/prestatement.jsp๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค !

 

 

Statement์™€ PreparedStatement์— ๋Œ€ํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์‹ค์Šต์„ ์ง„ํ–‰ํ•ด๋ณด์•˜๋Š”๋ฐ์š”, ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” 'ํŠธ๋žœ์žญ์…˜'์— ๋Œ€ํ•œ ๊ธ€์„ ์จ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค : ) 

 

 

๋ฐ˜์‘ํ˜•