20.03 ~ 20.08 국비교육/MySQL・ORACLE

JDBC 테이블 생성 및 활용

찹키리 2020. 4. 9. 10:31

<db에 테이블 생성>

 

create table owner_info

(

id int not null, #주민번호

nm varchar(50) null, #이름

handphone varchar(50), #핸드폰

gender varchar(4) null #성별

) engine = innodb default charset = euckr;

 

 

 

 

 

<Insert, Update, Delete>

 

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
 
import java.sql.*;
 
public class InsertDB {
    public static void main(String args[]) {
String url = "jdbc:mysql://localhost:3306/javadb?
useUnicode=true&characterEncoding=euckr";
        Connection con = null;
        Statement stmt = null;
 
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            System.err.println(e.getMessage());
        } // 드라이버 호출
 
        try {
            con = DriverManager.getConnection(url, "javauser""비밀번호");
            //con으로 자바db 연동, 정보 가
String sqlStr = "insert into owner_info(id, nm, handphone, gender) values" +
                "(2, '아로미', '000-222-3333', '여')";
 
            stmt = con.createStatement();
            //con객체를 이용해 statement객체생성
            stmt.executeUpdate(sqlStr);
            //괄호 안 변수를 실행
            System.out.println("레코드가 추가되었습니다.");
        }catch(SQLException e) { 
            System.out.println(e.getMessage());
        }finally {
                    try {
                            stmt.close();
                            con.close(); //닫을 때는 아래부터
                    } catch(Exception e) {
                        System.out.println(e.toString());
                    }
        }
    }
}`
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

*Insert, Update, Delete문은 기본적으로 이 소스코드를 바탕으로 실행된다.

 

 

 

 

 

<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
 
import java.sql.*;
 
public class SelectDB {
    public static void main(String args[]) {
String url = "jdbc:mysql://localhost:3306/javadb?
useUnicode=true&characterEncoding=euckr";
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null// select문에서는 반드시 필요하다
 
        try {
            Class.forName("org.gjt.mm.mysql.Driver");
            System.err.println(e.getMessage());
        } // 드라이버 호출
 
        try {
            con = DriverManager.getConnection(url, "javauser""비밀번호");
            //con으로 자바db 연동, 정보 가져옴
String sqlStr = "select id, nm, handphone, gender from owner_info";
 
            stmt = con.createStatement();
            //con객체를 이용해 statement객체생성
            rs = stmt.executeQuery(sqlStr);
            //쿼리 실행 뒤 rs에 결과를 처리(반환)
        }catch(SQLException e) { 
            System.out.println(e.getMessage());
        }finally {
                    try {
                            rs.close();
                            stmt.close();
                            con.close(); //닫을 때는 아래부터
                    } catch(Exception e) {
                        System.out.println(e.toString());
                    }
        }
    }
}`
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

*Select문에서는 ResultSet객체가 반드시 필요하다

'20.03 ~ 20.08 국비교육 > MySQL・ORACLE' 카테고리의 다른 글

Oracle 설치 및 세팅  (0) 2020.05.25
PreparedStatement  (0) 2020.04.09
드라이버 연동 테스트  (0) 2020.04.08
Java에서 MySQL 연동하기(드라이버 설치)  (0) 2020.04.07
기초 SQL  (0) 2020.04.06