DML
Insert
1
2
insert into member ( name1 , name2 , ....)
values ( val1 , val2 ...);
` Auto increment : primary key에서 자동으로 데이터 개수를 알 수 있도록
Update
1
2
update table set name1 = val1 , [ name2 = val2 ... namen = valn ]
where name = 'a'
Delete
1
2
delete from table
where name = 'a'
Select
1
select a from table where condition
distinct : 중복 행 제거
expression, alias 등
ifnull(expr1, expr2) : expr1이 null이면 expr2 출력
case..then : 조건에 대한 값을 바꿔서 보여줘야 할 때
1
2
3
4
select salary
case when salary > 15000 then '고액'
when salary > 8000 then '평균'
else '저액'
where id in ('a','b','c')
: 안에 포함되는지 여부
where salary between 60 and 100
: 사이값
where salary is not null
: null 아닌거 나오게
Like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
--A로 시작하는 문자를 찾기--
SELECT 컬럼명 FROM 테이블 WHERE 컬럼명 LIKE 'A%'
--A로 끝나는 문자 찾기--
SELECT 컬럼명 FROM 테이블 WHERE 컬럼명 LIKE '%A'
--끝에서 3번째 자리가 A인 문자 찾기--
SELECT 컬럼명 FROM 테이블 WHERE 컬럼명 LIKE '%A__'
--A를 포함하는 문자 찾기--
SELECT 컬럼명 FROM 테이블 WHERE 컬럼명 LIKE '%A%'
--A로 시작하는 두글자 문자 찾기--
SELECT 컬럼명 FROM 테이블 WHERE 컬럼명 LIKE 'A_'
--첫번째 문자가 'A''가 아닌 모든 문자열 찾기--
SELECT 컬럼명 FROM 테이블 WHERE 컬럼명 LIKE '[^A]'
--첫번째 문자가 'A'또는'B'또는'C'인 문자열 찾기--
SELECT 컬럼명 FROM 테이블 WHERE 컬럼명 LIKE '[ABC]'
SELECT 컬럼명 FROM 테이블 WHERE 컬럼명 LIKE '[A-C]'
논리 연산시 주의
NOT NULL → NULL
NULL AND TRUE → NULL , NULL AND FALSE → FALSE
NULL OR FALSE → NULL , NULL OR TRUE → TRUE
order by : 정렬(default : ASC), DESC : 내림차순
JDBC API
JDBC 연결 순서
Connection 생성 : com.mysql.cj.jdbc.Driver 로딩
연결
1
2
3
4
5
con = DriverManager . getConnection (
"jdbc:mysql://127.0.0.1:포트번호/db이름?serverTimezone=UTC&useUniCode=yes&characterEncoding=UTF-8" ,
id , pwd
);
statement 생성
1
Statement st = con . createStatement ();
execute() or exectueQuery() 실행
1
2
3
4
5
rs = st . executeQuery ( "select * from emp" );
while ( rs . next ()) {
System . out . println ( rs . getString ( "ename" )+ " , " + rs . getInt ( "sal" ));
}
preparedStatement
resultSet
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
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
import java.sql.Statement ;
// 기능만 제공한다 -> 멤버 변수, 즉 state가 없다 -> stateless
public class DBUtil {
private static DBUtil util = new DBUtil ();
public static DBUtil getUtil () {
return util ;
}
private DBUtil () {
try {
// 1. Driver Loading
Class . forName ( "com.mysql.cj.jdbc.Driver" );
} catch ( ClassNotFoundException e ) {
e . printStackTrace ();
}
}
// 2. Connection
public Connection getConnection () {
String url = "jdbc:mysql://127.0.0.1:포트번호/db이름?erverTimezone=UTC&useUniCode=yes&characterEncoding=UTF-8" ;
Connection con = null ;
try {
con = DriverManager . getConnection ( url , id , pwd );
} catch ( SQLException e ) {
e . printStackTrace ();
}
return con ;
}
public void close ( ResultSet rset , Statement pstmt , Connection con ) {
try {
if ( rset != null ) {
rset . close ();
}
if ( pstmt != null ) {
pstmt . close ();
}
if ( con != null ) {
con . close ();
}
} catch ( SQLException e ) {
e . printStackTrace ();
}
}
}
private void selectTest ( int idx ) {
Connection con = util . getConnection ();
// 3. Statement Create
PreparedStatement pstmt = null ;
ResultSet rset = null ;
try {
String sql = "select * from member where idx=?" ;
// idx는 pk이므로 반복문 쓸 필요x..
// 4. SQL Prapare and Execute
pstmt = con . prepareStatement ( sql );
pstmt . setInt ( 1 , idx );
rset = pstmt . executeQuery ();
if ( rset . next ()) {
String userid = rset . getString ( "userid" );
String username = rset . getString ( "username" );
String userpwd = rset . getString ( "userpwd" );
String emailid = rset . getString ( "emailid" );
System . out . println ( userid + " : " + username + " : " + userpwd + " : " + emailid );
}
} catch ( SQLException e ) {
e . printStackTrace ();
} finally {
//5. close
util . close ( rset , pstmt , con );
}
}
Statement는 사용하지 않음 → sql injection 삽입공격 때문
statement 는 sql을 그대로 전달
preparedstatement는 값들을 인자처럼 처리