본문 바로가기
Programming Language/Java

mysql db 연동 테스트

by 뒹굴거리는프로도 2018. 7. 4.
반응형
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
package stairsNoise;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import org.junit.Test;
 
 
 
public class MySqlTest {
 
 
    Connection conn = null;
    Statement stmt = null;
    private final String DRIVER = "com.mysql.jdbc.Driver";
    private final String URL = "jdbc:mysql://localhost:3307/jianDB?useSSL=false&serverTimezone=UTC";
    private final String USER ="root";
    private final String PW = "U3cnc@12345";
 
 
    @Test
    public void test() {
 
        String sql;
 
        try {
            Class.forName(DRIVER);
 
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
 
        try {
            conn = DriverManager.getConnection(URL, USER, PW);
            System.out.println(">> 연결 성공");
 
            stmt = conn.createStatement();
 
            sql = "SELECT SNO, ID, PW, TITLE, CONTENTS, REG_DATE FROM JIANBOARD";
            ResultSet rs = stmt.executeQuery(sql);
 
            while(rs.next()){
                String title = rs.getString("title");
                System.out.println(" title >> " + title);
            }
            rs.close();
            stmt.close();
            conn.close();
 
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try{
                if(stmt!=null)
                    stmt.close();
            }catch(SQLException se2){
            }
            try{
                if(conn!=null)
                    conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
 
    }
 
 
}
 
cs


반응형