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

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

๋ฐ˜์‘ํ˜•

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Docker์— Ubuntu๋ฅผ ๋งŒ๋“ค์–ด, Tomcat8์„ ์ด์šฉํ•˜์—ฌ JDBC ์˜ Statement ์‹ค์Šต์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

JDBC ์‹ค์Šต์„ ์›ํ• ํ•˜๊ฒŒ ํ•˜์‹œ๋ ค๋ฉด MySQL๊ณผ Launchpad(test-db)๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์•„๋ž˜์˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์…”์„œ ์‹ค์Šตํ•˜์‹œ๊ณ  ์˜ค์‹œ๋ฉด ๋„์›€์ด ๋˜์‹ค๊ฑฐ ๊ฐ™์•„์š”!

 

 

Docker๊ธฐ๋ฐ˜์˜ MySQL ์„ค์น˜ ๋ฐ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

์˜ค๋Š˜์€ ํ•™๊ต์—์„œ ๋ฐฐ์› ๋˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ˆ˜์—… ์‹ค์Šต ๋‚ด์šฉ๋“ค์„ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค! Docker๋Š” ์„ค์น˜๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ • ํ•˜ ์— ๋„์ปค๋ฅผ ํ™œ์šฉํ•œ Mysql ์„ค์น˜ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋„์ปค ๋ช…๋ น์–ด์™€ Mysql ๊ธฐ๋ณธ ๋ช…๋ น์–ด ์‚ฌ์šฉ

yunaaaas.tistory.com

 

MySQL Launchpad(test-db) ๋‹ค์šด๋กœ๋“œ ๋ฐ ์„ค์น˜

์ด๋ฒˆ์—๋Š” https://launchpad.net/test-db ์˜ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์„ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด MySQL์— Launchpad(test-db)๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์„ค์น˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ๋ ค๋“œ๋ฆฌ๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. Docker์— MySQL์„ ์„ค์น˜ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด ์•„๋ž˜.

yunaaaas.tistory.com

 

JDBC ๋ž€ ?!

Java Database Connectivity

์ฆ‰, ์ž๋ฐ”์—์„œ DBMS์— ์ ‘์†ํ•˜๊ธฐ ์œ„ํ•œ API (ํŒจํ‚ค์ง€) ์ž…๋‹ˆ๋‹ค. (ex) java.sql.* , javax.sql.*

DBMS์—์„œ ์ œ๊ณตํ•˜๋Š” JDBC ์ปค๋„ฅํ„ฐ (๋“œ๋ผ์ด๋ฒ„)๋ฅผ ์„ค์น˜ ํ•œ ํ›„ ์‚ฌ์šฉํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

JDBC ํ”„๋กœ๊ทธ๋žจ์˜ ๊ตฌ์กฐ

JDBC๋Š” ๋””์ž์ธ ํŒจํ„ด์˜ ๋นŒ๋”(Builder) ํŒจํ„ด์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋นŒ๋”(Builder) ํŒจํ„ด์ด๋ž€ ?
DriveManager -> Connection -> Statement ( -> ResultSet) 

 

1. ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ 

์ž๋ฐ” ๋ฆฌํ”Œ๋ ‰์…˜ (Reflection)์˜ ๋‹ค์ด๋‚˜๋ฏน ํด๋ž˜์Šค ๋กœ๋”ฉ (.class ํŒŒ์ผ)

Class.forName(...);

2. DB ์—ฐ๊ฒฐ -> Connection ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ

Connection conn = DriverManager.getConnection(dburl, id, password);

3. Statement ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ ( SQL์„ ๋„ฃ์–ด์„œ ์‹คํ–‰ํ•˜๋Š” ์˜ค๋ธŒ์ ํŠธ)

Statement stmt = conn.createStatement();

4. Statement ์‹คํ–‰ ( executeUpdate / executeQuery )

stmt.executeUpdate("INSERT INTO xxxx VAlUES ("") ");
ResultSet rs = stmt.executeQuery(“SELECT …”);

5. ResultSet์œผ๋กœ ๊ฒฐ๊ณผ ์ถœ๋ ฅ

while (rs.next()) {
	String sampleString = rs.getString(1);
	int sampleInteger = rs.getInt(2);
 }

 

 

Docker์— MySQL๊ณผ Launchpad(test-db) ์„ค์น˜๋Š” ์ด์ „ ๊ธ€์—์„œ ์‹ค์Šตํ•˜์˜€์œผ๋ฏ€๋กœ, ์ƒ๋žตํ•˜๊ณ  ๋ฐ”๋กœ JDBC ์‹ค์Šต๋งŒ ์†Œ๊ฐœํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ“Œ Docker์— ubuntu ์„ค์น˜ ๋ฐ ์‹คํ–‰ํ•˜๊ธฐ

JDBC ๋“œ๋ผ์ด๋ฒ„๋กœ Ubuntu๋ฅผ ์ด์šฉํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ubuntu ์„ค์น˜ํ•˜๊ธฐ

Ubuntu์˜ ํฌํŠธ ๋„˜๋ฒ„๋Š” 8080 ์ด๋ฏ€๋กœ ๊ผญ 8080:8080 ์œผ๋กœ ์ง€์ •ํ•ด์ฃผ์„ธ์š”!

Docker run ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ubuntu  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์„ค์น˜ ๋ฐ ๋ฐ”๋กœ ์‹คํ–‰ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

docker run -it -p 8080:8080 --name=ubuntu_new1 ubuntu:18.04

 

 

๐Ÿ“Œ ubuntu์— ํ•„์š”ํ•œ ๋“œ๋ผ์ด๋ฒ„ ์„ค์น˜ํ•˜๊ธฐ

ubuntu๋ฅผ ์„ค์น˜ํ•˜์…จ์œผ๋ฉด JDBC๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ช‡๊ฐœ์˜ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ์„ค์น˜ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

openjdk-8-jdk / nano / tomcat8 / libmysql-java๋ฅผ ์„ค์น˜ํ•ด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

apt upadate
apt install nano
apt install openjdk-8-jdk
apt install libmysql-java
apt install tomcat8

์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ฐจ๊ทผ์ฐจ๊ทผ ์„ค์น˜ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ“Œ tomcat8 ์„œ๋ฒ„ ์‹คํ–‰ํ•˜๊ธฐ

์œ„์˜ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋ชจ๋‘ ์„ค์น˜ํ•˜์…จ์œผ๋ฉด ์ด์ œ mysql-coonector ์™€ tomcat8์„ ์—ฐ๊ฒฐํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

cd /usr/share/java
ln -s /usr/share/java/mysql-connector-java.jar /usr/share/tomcat8/lib/
service tomcat8 start

 

โœ”๏ธ ์—ฌ๊ธฐ์„œ Fail ์ด ๋– ๋„ ํ†ฐ์บฃ์„œ๋ฒ„๋Š” ์‹คํ–‰๋˜์—ˆ์œผ๋ฏ€๋กœ, ๊ฑฑ์ •ํ•˜์‹œ์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค!!

๐Ÿ“Œ ํ†ฐ์บฃ์„œ๋ฒ„ ์‹คํ–‰ ํ™•์ธํ•˜๊ธฐ

ํฌ๋กฌ์œผ๋กœ localhost:8080์— ๋“ค์–ด๊ฐ€๋ณด๊ฒŒ ๋˜๋ฉด It works ! ๊ฐ€ ๋œฌ๋‹ค๋ฉด ๋งž๊ฒŒ ํ†ฐ์บฃ ์„œ๋ฒ„๋Š” ์‹คํ–‰๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

localhost:8080

 

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

์œ„์—์„œ ์„ค์น˜ํ•œ nano๋ฅผ ๊ฐ€์ง€๊ณ  jsp ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

nano /var/lib/tomcat8/webapps/ROOT/statemen.jsp

 

 

ํŒŒ์ผ์„ ์ž‘์„ฑ์ „ ๋จผ์ € Docker์šฉ MySQL์˜ ์ฃผ์†Œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค!

์ƒˆ๋กœ์šด ํ„ฐ๋ฏธ๋„์„ ์—ด์–ด ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ MySQL์˜ IP์ฃผ์†Œ๋ฅผ ์ฐพ์•„์ฃผ์„ธ์š”.

docker inspect mysql1 | grep IPAddress

 

 

<%@ page import = "java.sql.*" %>
<% Statement stmt = null; ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://172.17.0.2:3306/employees";
Connection conn = DriverManager.getConnection(dbUrl, "root", "yunas");
try {
        stmt = conn.createStatement();
        if( stmt.execute ("select * from employees limit 10")) {
                rs = stmt.getResultSet();
        }
        while(rs.next()) {
                out.println("emp_no : "+rs.getInt("emp_no") +" name : "+ rs.getString("first_name") +" "+ rs.getString("last_name"));
                out.write("<br/>");
        }
        rs.close();
        stmt.close();
} catch(Exception e) {
        out.println("rs.next() Error");
}
conn.close();
%>

 

String dbUrl = "jdbc:mysql://{IPAddress -192.17.0.2}:3306/{database}";
Connection conn = DriverManager.getConnection(dbUrl, "root", "{mysql_password}");

{192.17.0.2} ์ž๋ฆฌ์— ์œ„์—์„œ ์ฐพ์•˜๋˜  Docker MySQL์˜ IPAddress๋ฅผ , mysql -u root - p ๋ช…๋ น์–ด๋กœ MySQL ์„œ๋ฒ„์— ๋“ค์–ด๊ฐˆ ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ Password๋ฅผ {mysql_password} ์ž๋ฆฌ์— ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค : )

 

stmt.execute ("select * from employees limit 10")

์›ํ•˜๋Š” SQL๋ฌธ์„ execute ์•ˆ์— ๋„ฃ์–ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค : )

 

rs = stmt.getResultSet();

rs.getString("first_name")
rs.getInt("emp_no")
rs.getDate("hire_date")
out.println("emp_no : "+rs.getInt("emp_no") +" name : "+ rs.getString("first_name") +" "+ rs.getString("last_name"));

๊ฒฐ๊ณผ๊ฐ’์€ rs์— ResultSet์œผ๋กœ ์ €์žฅ๋˜๋ฉฐ, ํ•ด๋‹น Column์˜ ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ค์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

getString(), getInt() ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๋‚ด์šฉ์€ ๊ผญ Table์˜ Column๋ช…๊ณผ ๋™์ผํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค!

 

 

 

 

nano๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‚˜๊ฐ€์‹œ๋ ค๋ฉด ?! 

control(^) + X , y ๋กœ ๋‚˜๊ฐ€์‹ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

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

jsp ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” localhost:8080/Satatement.jsp ๋กœ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

MySQL ์„œ๋ฒ„์— ๋“ค์–ด๊ฐ€ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์œ„์™€ ์•„๋ž˜์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๋™์ผํ•œ ๊ฒƒ์„ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค : )

 

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

 

๋ฐ˜์‘ํ˜•