ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring] 게시판 만들기(9) - MySQL 연동
    Spring/4.3.x - 게시판 만들기 2018. 12. 4. 15:49
    반응형

     지난 포스팅에서 로컬에 설치한 MySQL을 자바에서 접근할 수 있도록 JDBC(Java DataBase Connectivity)와 MySQL을 연동하는 작업을 진행하도록 하겠습니다.

     

    1. POM.xml에 의존성 추가

     POM.xml에 MySQL과 관련된 의존성을 추가하세요.

    1
    2
    3
    4
    5
    6
    7
    <mysql.version>5.1.46</mysql.version>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>
     
    cs

     

     

     

     

     

     

     

    2. POM.xml에 junit 버전을 변경

     JUnit은 MySql 정상적으로 연동되었는지 확인하기 위해서 버전을 변경해주세요. ( JUnit 사용법 - https://tychejin.tistory.com/152 )

    1
    2
    3
    4
    5
    6
    7
    <junit.version>4.12</junit.version>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>    
    cs

     

     

     

     

     

     


     

    3. MySQL 연결 테스트
    3_1. src/test/java 폴더 com.spring.board 패키지에 MySQLConnectionTest.java 파일을 추가하세요. 

    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
    package com.spring.board;
     
    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 MySQLConnectionTest {
     
        static final String DRIVER = "com.mysql.jdbc.Driver";
        static final String URL = "jdbc:mysql://127.0.0.1:3306/board?useSSL=false";
        static final String USERNAME = "board";
        static final String PASSWORD = "password1!";
     
        @Test
        public void getMySQLConnectionTest() {
            
            Connection conn = null;
            Statement stmt = null;
            
            try {
                
                System.out.println("==================== MySQL Connection START ====================");
                
                Class.forName(DRIVER);
                
                conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
                stmt = conn.createStatement();
     
                String sql = "SELECT BOARD_SUBJECT, BOARD_CONTENT, BOARD_WRITER FROM TB_BOARD";
     
                ResultSet rs = stmt.executeQuery(sql);
                while (rs.next()) {
                    
                    String boardSubject = rs.getString("BOARD_SUBJECT");
                    String boardContent = rs.getString("BOARD_CONTENT");
                    String boardWriter = rs.getString("BOARD_WRITER");
     
                    System.out.print("boardSubject : " + boardSubject + ", ");
                    System.out.print("boardContent: " + boardContent + ", ");
                    System.out.println("boardWriter: " + boardWriter);
                }
     
                rs.close();
                stmt.close();
                conn.close();
     
            } catch (SQLException se1) {
                se1.printStackTrace();
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                    }
                } catch (SQLException se) {
                    se.printStackTrace();
                }
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException se) {
                    se.printStackTrace();
                }
            }
            
            System.out.println("==================== MySQL Connection END ====================");
        }
    }
     
    cs

     

     

    3_2. MySQLConnectionTest.java 파일을 JUnit으로 실행하세요.

     MySQLConnectionTest.java을 선택한 후 우클릭 -> Run As -> JUnit Test

     

    3_3. Console창에 값이 출력되는지 확인하세요.

     

    소스 코드는 Github Repository - https://github.com/tychejin1218/board_v1 (branch : section09) 를 참조하세요.
    Github에서 프로젝트 가져오기 - https://tychejin.tistory.com/33

     

    반응형

    댓글

Designed by Tistory.