Mysql - JAVA DB 연동시키기

1. Mysql Connector_J를 다운받는다
https://dev.mysql.com/downloads/connector/





2. 다운받은 파일을 C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext의 폴더에 넣는다.



3. 자바 파일

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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConect {
    public static void main(String[] args) {
        try {
        // The newInstance() call is a work around for some
        // broken Java implementations
        Class.forName("com.mysql.jdbc.Driver").newInstance();
//        Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception ex) {
            System.out.println("ex=========SQLException: " + ex.getMessage());
        }
        Connection conn = null;
        try{
            // ("jdbc:mysql://127.0.0.1/해당DB","아이디" ,"비밀번호");
            conn =DriverManager.getConnection("jdbc:mysql://127.0.0.1/test","아이디" ,"비밀번호");
            if(conn != null){
                System.out.println("db 연결성공===========");    
                conn.close();    
            }    
        } catch (SQLException ex) {
        // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        } finally {
            
        }    
    }
}
cs

4. 연결 확인

db 연결성공이 뜨면 정상적인 작동이다.




댓글