๋ทฐ:
๋ณ๋์ ์ฐฝ์ด๋ค.
๋ทฐ์ ์ฅ์ ์ ๊ฐ์ํ๋ค.
์ปฌ๋ผ๋ช ์ผ์ผ์ด ๋์ดํ๋๊ฑด ๋๋ฌด ํ๋ฌ, ๋ทฐ๋ฅผ ์ด์ฉํด์ ์ ํํ ์ปฌ๋ผ๋ง ๋ณด๊ฒํด์ค ์ ์์.
๋ทฐ์ ํน์ง -- p363
1. ์ฟผ๋ฆฌ๋ฅผ ๊ฐ์ํ
2. ๋ค๋ฅธ ์ฌ๋์๊ฒ ์ ํด์ง ๊ฒ๋ง ๋ณด์ฌ์ค๋ค(๋ณด์์ )
3. ๋ทฐ๋ ์กฐํ๋๋ ๊ฒ ๋ฟ๋ง ์๋๋ผ, ์ ๋ ฅ, ์์ , ์ญ์ ๋ชจ๋ ๊ฐ๋ฅํ ๋ณ๋์ ์ฐฝ์ด๋ค.
4. ๋ทฐ๋ ์ค์ฒด ๊ณต๊ฐ์ด ์์ด์ ํฌ์๋์ด ์๋ณธ ํ ์ด๋ธ์์ ์ฒ๋ฆฌํ๊ณ ์จ๋ค.
View01.sql
-- View01.sql --
CREATE TABLE emp3
select * from emp; -- ํ
์ด๋ธ ๊ทธ๋๋ก ์นดํผ (์ปฌ๋ผ ์ฌ์ด์ฆ,ํ
์ด๋ธ์ ๋ณต์ฌ๋๋๋ฐ ์ ์ฝ์กฐ๊ฑด, ์ธ๋ฑ์ค ๋ฑ์ ๋ณต์ฌ์ )
SELECT * from emp3;
desc emp3; -- ์ค์ ๋ค์ ํ์ธํ ์ ์์
create view v1_emp3
as
SELECT empno, ename, job, hiredate, sal, comm, deptno from emp3;
select * from v1_emp3;
SELECT * from v1_emp3 ve
where sal < 2000;
SELECT ve.ename , d.dname
from v1_emp3 ve , dept d
where (ve.deptno = d.deptno)
and (d.dname = 'sales');
-- emp 3์์ ๊ธ์ฌ๊ฐ 2000์ด์์ธ ์ง์๋ค์ ์ฌ๋ฒ, ์ด๋ฆ, ๊ธ์ฌ, ๋ถ์๋ฒํธ
-- ์ด ๋ถ๋ถ์ view๋ก ์์ฑํ๊ธฐ
create view v2_emp3
as
select e.empno , e.ename , e.sal , e.deptno from emp3 e
WHERE e.sal > 2000;
SELECT * from v2_emp3 ve ;
-- ๋ทฐ๋ฅผ ํตํด์ ์
๋ฐ์ดํธ๋ ๋ ๊น์?
-- v2_emp3 ์์ ์ฌ๋ฒ์ด 7566์ธ ์ง์์ ๋ถ์๋ฒํธ๋ฅผ 30์ผ๋ก ์์ ํด๋ณด์ธ์.
update v2_emp3
set deptno = 30
where empno = 7566;
SELECT deptno
from v2_emp3 ve
where empno = 7566;
SELECT deptno
from emp3
where empno = 7566;
-- view ์์ ๋ณ๊ฒฝ์ฌํญ์ด ์์ผ๋ฉด ์๋ณธ์์๋ ๋๊ฐ์ ๋ณํ๊ฐ ์ผ์ด๋จ
delete from v2_emp3
where empno = 7566;
-- v2_emp3์์ ์ง์(7777, Tom, 4000, 20) ์
๋ ฅ
insert into v2_emp3 values(7777, 'Tom', 4000, 20);
DELETE from v2_emp3
WHERE empno = 7777;
-- v2_emp3์์ ์ง์(7788, Jane, 1000, 20) ์
๋ ฅ
-- (view์์ sal2000์ด์์ผ๋ก ๋ง๋ค์๊ธฐ ๋๋ฌธ์ view ์๋ ์๋ณธ์๋ง ๋ค์ด๊ฐ )
INSERT into v2_emp3 values (7788, 'Jane', 1000, 20);
DELETE from emp3
where ename = 'Jane';
SELECT * from v2_emp3 ve ;
SELECT * from emp3 e ;
-- ์ถ๊ฐ๋ ๋ฒ์ ๋ฐ์์๋ ๋๋๋ฐ ์ญ์ /๋ณ๊ฒฝ์ ๋์ง ์๋๋ค.
DELETE from v2_emp3 where (sal = 1000);
UPDATE v2_emp3 set deptno = 40 where (sal = 1000);
-- View ๋ณ๊ฒฝcreate view v1_emp3
alter view v2_emp3
as
SELECT empno, ename, job, hiredate, sal, comm, deptno
from emp3
where (sal >= 2000)
with check option;
INSERT into v2_emp3 values (9999, 'Alice', 999, 20);
create view v3_emp3
as
select ename, empno, ((12*sal) + ifnull(null, 0 )) "์ฐ์๋"
from emp3;
DROP view v3_emp ; -- ๋ทฐ์ญ์
create view v4_emp
as
select ee.ename '์ง์', ee.job, ed.dname '์ง์๋ถ์', ee.sal,
es.grade , me.ename '์๊ด', md.dname '์๊ด๋ถ์'
from emp ee, dept ed, salgrade es , emp me, dept md
where (ee.deptno = ed.deptno)
and (ee.sal between es.losal and es.hisal)
and (ee.mgr = me.empno)
and (me.deptno = md.deptno)
and (ee.sal < ( select avg(sal)
from emp
where (job = ( select job
from emp
where (ename = 'Martin')))));
Web Server ๊ตฌ์ฑ - JDBC
์น ๊ด๋ จ ํ๋ก์ ํธ์๋
1. UI
2. Front end
3. Back end
4. Database
5. VM
ํด๋ผ์ด์ธํธ ์ชฝ์ ๊ฐ๊น์ด -- html (html์๋ ๊ธ์์ฒด, ์ ์ ํ๋ ๊ฒ css), ์ฑ๋ฅ, ๊ธฐ๋ฅ์ ๋๋ฆด ์ ์๋๊ฑฐ java script, jquery,
ui ์คํ ๋ฆฌ๋ณด๋
๋ฐฑ์๋ : jsp(ํ๋ฉด ๋จ view) / servlet(๋ด๋ถ์ java๊ฐ ๋, control) / ๋ฐ์ดํฐ ๋ฒ ์ด์ค์ ์ฐ๊ฒฐ์ํค๋ ๊ฒ, db class (๋ชจ๋ธ)
--> MVC ํจํด (์ด ํจํด์ ๋ํด ํ๋ ์์ํฌ ์ ๊ณตํ๋ ํ๋ ์์ํฌ๋ฅผ spring์ด๋ผ๊ณ ํจ --> ์ข ๋ ๋น ๋ฅด๊ฒ --> ์คํ๋ง ๋ถํธ)
VM์ ๋์ต์ค๋ก ๊ฐ๋ ๊ฒ. ๋์ปค(๋คํธ์ํฌ), ์ฝ๋ณด๋ ธ์ธ (๊ฐ์ํ์ ๋ํ ์ด์ผ๊ธฐ) ๋ผ๊ณ ํ๋ ๊ฒ๋ค์ด ํ์๋์ด์ ๋์ด.
์คํ๋ง๋ถํธ๋ VM๊ณผ ๋ง๋ฟ๋ ๊ฒ -- Spring Cloud
์ฌ๋ฌ๊ฐ์ง ๊ฐ์ํ๊ฐ ์๋๋ฐ DevOps์์ ๋งํ๋ ๊ฐ์ํ๋ ํ๋ก์ธ์ค ๊ฐ์ํ(OS ๊ฐ์ํ ์๋)
DB์์ ์ฐ๋ฆฌ๋ RDB๋ผ๋ ๊ฒ์ ๋ฐฐ์ ๋ค.
RDB๋ผ๊ณ ํ๋ ๊ฒ์, ํ ์ด๋ธ ERD ๋ผ๋ ๊ฒ์ ํจ. (Entity, Attribute, Relationship)
๋ฐ์ดํฐ ์กฐํ ( SubQuery, Join, where, group by having)
DML ( insert, update, delete) ๊ทธ ๋ค์์ ์ํฐํฐ๋ฅผ ๊ตฌ์ฑํ๋ ค๋ฉด, ERD ์ค๊ณ ํ DML ๋ง๋ฟ๋ ๊ณณ์์
DDL ( Table(Datatype, size, constraint) ์ด ๋ฐ์. ์ด๋ (create, alter, drop)์ ํจ.
์ด๋ ๊ฒ ์ ์์ ํ ๊ฒ์ ์๋ฐ์์ ๋์ด์ค๋ ค๋ฉด ๋ชจํ์ ๋ง๋ค์ด์ผ.
์ด์ ์ด ๋ชจํ์ ๋ง๋ค์ด๋ณด์.
์ฐ๊ฒฐ๊ณ ๋ฆฌ๊ฐ ํ์ํ ํ ๋ฐ? ๊ทธ ์ฐ๊ฒฐ๊ณ ๋ฆฌ๊ฐ JDBC๋ผ๋ ์ฐ๊ฒฐ๊ณ ๋ฆฌ!
์๋ฐ์ชฝ์์ mysql์ ์ฐ๊ฒฐํ๋ JDBC๊ฐ ํ์ํ๊ตฌ๋.
JDBC MySQL ์ฐ๋ํ๊ธฐ
https://m.blog.naver.com/pjok1122/221727915740
๋งฅ์ ์ด๊ฑฐ ์ฐธ๊ณ : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=suda552&logNo=220725289307
์ ์ด๊ฑฐ ์๋์ด์ ํ์ฐธ ๊ณ ์ํ๋ค.....
์ดํ ์ดํด๋ฆฝ์ค์์ ์์ฑ
MyConn01.java
import java.sql.*;
import java.util.*;
public class MyConn01 {
public static void main(String[] args) {
// JDBC ๋๋ผ์ด๋ธ ๋ก๋ (์ฐ๊ฒฐ)
// Database Connection ์ฐ๊ฒฐ
// statement ์์ฑ(์ฌ๋ค๋ฆฌ์ฐจ ํ๋ฌ)
// SQL ์ ์ก --> executeQuery(), executeUpdate()
// Result set(๊ฒฐ๊ณผ) ๋ฐ๊ธฐ
// Database connection ํด์
/////////////////////////////////////////////////
// JDBC ๋๋ผ์ด๋ธ ๋ก๋ (์ฐ๊ฒฐ)
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
String jdbcUrl = "jdbc:mysql://localhost/empdb";
try {
Class.forName(jdbcDriver);
// Database Connection ์ฐ๊ฒฐ
Connection conn = DriverManager.getConnection(jdbcUrl,"root","1234");
// Statement ์์ฑ
String sql = "select * from dept";
Statement stmt = conn.createStatement();
// SQL ์ ์ก --> executeQuery(), executeUpdate()
// ResultSet(๊ฒฐ๊ณผ) ๋ฐ๊ธฐ
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.printf("%d | %-10s | %-10s \n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
}
// Database Connection ํด์
stmt.close();
conn.close();
}
catch (Exception e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
}
}
MyConn02.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class MyConn02 {
public static void main(String[] args) {
Connection conn = null;
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
String jdbcUrl = "jdbc:mysql://localhost:3306/empdb?serverTimezone=UTC";
String dbUser = "root";
String dbPwd = "1234";
String sqlSelect = "select * from dept10";
PreparedStatement pstmt = null;
ResultSet rs = null;
int iDeptno = 0; String sDname = null; String sLoc = null;
Scanner sc = new Scanner(System.in);
System.out.println("๋ถ์ ๋ฒํธ ์
๋ ฅ => "); iDeptno = sc.nextInt();
System.out.println("๋ถ์ ์ด๋ฆ ์
๋ ฅ => "); sDname = sc.next();
System.out.println("๋ถ์ ์์น ์
๋ ฅ => "); sLoc = sc.next();
String sqlInsert = "insert into dept10 values(?,?,?)";
try {
// 1. JDBC ๋๋ผ์ด๋ธ ๋ก๋ (์ฐ๊ฒฐ)
Class.forName(jdbcDriver);
// 2. Database Connection ์ฐ๊ฒฐ
conn = DriverManager.getConnection(jdbcUrl,dbUser,dbPwd );
System.out.println("Connection ์ฐ๊ฒฐ");
// 3. statement ์์ฑ
pstmt = conn.prepareStatement(sqlInsert);
// 4. SQL ์ ์ก --> executeQuery(), executeUpdate()
pstmt.setInt(1, iDeptno);
pstmt.setString(2, sDname);
pstmt.setString(3, sLoc);
int resultCount = pstmt.executeUpdate();
// 5. ResultSet(๊ฒฐ๊ณผ) ๋ฐ๊ธฐ ‚
rs = pstmt.executeQuery(sqlSelect);
if(resultCount > 0 ) {System.out.println("์ ์ ์
๋ ฅ"); }
else {System.out.println("์
๋ ฅ ์๋จ "); }
while(rs.next()) {
System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
}
// 6. Database Connection ํด์
System.out.println("Connection ํด์ ");
pstmt.close();
conn.close();
}
catch(SQLException e) {
System.out.printf("Exception: \r\n %s", e);
System.out.println("๋ก๋ ์คํจ ");
}
catch(ClassNotFoundException e) {
System.out.printf("Exception: \r\n %s", e);
System.out.println("Driver ์ฐ๊ฒฐ ์๋จ");
}
}
}
MyConn03.sql (Update)
- ๋ถ์ ๋ฒํธ๋ก ๋ถ์ ์์น๋ฅผ ๋ณ๊ฒฝ
IT --> Jeju
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class MyConn03 {
public static void main(String[] args) {
Connection conn = null;
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
String jdbcUrl = "jdbc:mysql://localhost:3306/empdb?serverTimezone=UTC";
String dbUser = "root";
String dbPwd = "1234";
String sqlSelect = "select * from dept10";
PreparedStatement pstmt = null;
ResultSet rs = null;
int iDeptno = 0; String sDname = null; String sLoc = null;
Scanner sc = new Scanner(System.in);
System.out.println("๋ถ์ ๋ฒํธ ์
๋ ฅ => "); iDeptno = sc.nextInt();
System.out.println("๋ณ๊ฒฝ ์ง์ญ ์
๋ ฅ => "); sLoc = sc.next();
String sqlUpdate = "update dept10 set loc = ? where deptno = ?;";
try {
// 1. JDBC ๋๋ผ์ด๋ธ ๋ก๋ (์ฐ๊ฒฐ)
Class.forName(jdbcDriver);
// 2. Database Connection ์ฐ๊ฒฐ
conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPwd);
System.out.println("Connection ์ฐ๊ฒฐ ");
// statement ์์ฑ(์ฌ๋ค๋ฆฌ์ฐจ ํ๋ฌ)
pstmt = conn.prepareStatement(sqlUpdate);
// SQL ์ ์ก --> executeQuery(), executeUpdate()
pstmt.setInt(2, iDeptno);
pstmt.setString(1, sLoc);
int resultCount = pstmt.executeUpdate();
// 5. ResultSet(๊ฒฐ๊ณผ) ๋ฐ๊ธฐ ‚
rs = pstmt.executeQuery(sqlSelect);
if(resultCount > 0 ) {System.out.println("์ ์ ์
๋ ฅ"); }
else {System.out.println("์
๋ ฅ ์๋จ "); }
while(rs.next()) {
System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
}
// 6. Database Connection ํด์
System.out.println("Connection ํด์ ");
pstmt.close();
conn.close();
}
catch(SQLException e) {
System.out.printf("Exception: \r\n %s", e);
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
catch(ClassNotFoundException e) {
System.out.printf("Exception: \r\n %s", e);
System.out.println("Driver ๋ก๋ ์คํจ");
}
}
}
MyConn04.sql (Delete)
- ๋ถ์ ๋ฒํธ๋ฅผ ์ ๋ ฅ
- ํด๋น ๋ถ์๊ฐ ์ ๊ฑฐ
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class MyConn04 {
public static void main(String[] args) {
Connection conn = null;
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
String jdbcUrl = "jdbc:mysql://localhost:3306/empdb?serverTimezone=UTC";
String dbUser = "root";
String dbPwd = "1234";
String sqlSelect = "select * from dept10";
PreparedStatement pstmt = null;
ResultSet rs = null;
int iDeptno = 0; String sLoc = null;
Scanner sc = new Scanner(System.in);
System.out.println("๋ถ์ ๋ฒํธ ์
๋ ฅ => "); iDeptno = sc.nextInt();
String sqlDelete = "delete from dept10 where deptno = ?;";
try {
// 1. JDBC ๋๋ผ์ด๋ธ ๋ก๋ (์ฐ๊ฒฐ)
Class.forName(jdbcDriver);
// 2. Database Connection ์ฐ๊ฒฐ
conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPwd);
System.out.println("Connection ์ฐ๊ฒฐ ");
// 3. statement ์์ฑ(์ฌ๋ค๋ฆฌ์ฐจ ํ๋ฌ)
pstmt = conn.prepareStatement(sqlDelete);
// 4. SQL ์ ์ก --> executeQuery(), executeUpdate()
pstmt.setInt(1, iDeptno);
int resultCount = pstmt.executeUpdate();
// 5. Result set(๊ฒฐ๊ณผ) ๋ฐ๊ธฐ
rs = pstmt.executeQuery(sqlSelect);
if(resultCount > 0 ) {System.out.println("์ ์ ์
๋ ฅ");}
else {System.out.println("์
๋ ฅ ์๋จ "); }
while(rs.next()) {
System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
}
// 6. Database connection ํด์
System.out.println("Connection ํด์ ");
pstmt.close();
conn.close();
}
catch(SQLException e) {
System.out.printf("Exception: \r\n %s", e);
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
catch(ClassNotFoundException e) {
System.out.printf("Exception: \r\n %s", e);
System.out.println("Driver ๋ก๋ ์คํจ");
}
}
}
DAO, DTO, VO ๊ฐ ๋ฌด์์ธ์ง ์กฐ์ฌํด์ ์ ์ถ :