티스토리 뷰

 

MYSQL 연결 및 쿼리 보내기


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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
 
 
//java코드로 min디비에 접속해서
//student 테이블을 만듬
//create table student(
//    id int,
//    name varchar(20),
//    grade int
//);
//----- 디비 라이브러리 사용.
//Class 로딩
//DBMS의 특정 DB와 연결을 갖는 Connection객체 획득.
//내가 날릴 쿼리문을 품은 Statement 객체 생성.
//Connection 객체에 Statement구문객체를 전달.
 
 
public class CreateTableTest {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        //연결할 DBMS의 주소 (디비명까지);
        String dataBase = "min"//디비명
        String url = "jdbc:mysql://localhost:3306/" + dataBase;
        //접속할 mysql id와 password 입력.
        String id = "root"//기본적으로 아이디는 root
        String pw = "****"//비번
        
        try {
            //클레스를 로딩 함.. 최신버전 자바에서는 생략 가능
            Class.forName("com.mysql.jdbc.Driver"); 
            //DB와 연결을 갖는 Connection객체 획득
            conn = DriverManager.getConnection(url,id,pw);  
            //DB와 연결된 conn객체로부터 구문객체를 획득
            stmt = conn.createStatement();
            
            //쿼리 만들기
            StringBuilder sb = new StringBuilder();
            String sql = sb.append("create table if not exists student(")    //if not exists = 존재하지 않을 때만 생성
                    .append("id int,")
                    .append("name varchar(20),")
                    .append("grade int")
                    .append(");").toString(); 
 
            //쿼리문 날리기
            stmt.execute(sql);
            
            // 구문객체를 던지는 3가지 방법
            //1. execute -> 테이블 생성, 수정,삭제 등 데이터베이스 관리 명령어 사용
            //2. excuteUpdate -> 레코드 삽입 수정 삭제등 데이터 조작 명령어 사용
            //3. excuteQuery -> 레코드 조회, 테이블 조회 등 조회 명령어 사용
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                //사용한 connection 닫기
                if(conn != null && !conn.isClosed())
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
    }
 
}
 
cs


반응형

'언어 > JAVA' 카테고리의 다른 글

[JAVA] 초기화 블럭, 인스턴스 블럭  (0) 2018.01.25
[JAVA] 자바 MYSQL 삽입,삭제,수정,검색  (1) 2016.10.21
[JAVA] 라이브러리 추가  (0) 2016.10.21
[JAVA] TCP 통신 예제  (1) 2016.09.29
[JAVA] UDP 통신해보기  (0) 2016.09.27
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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
글 보관함