ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JSP] 이클립스(Eclipse) + 아파치 톰캣(Apache Tomcat) + MySQL 연동하기
    프로그래밍/JSP 2019. 5. 20. 14:14

    준비물 : Eclipse, JDK 1.8, Apache Tomcat 8.0 or 8.5 (본인은 8.5), MySQL 5, MySQL Connector5

    ※ 위와 버전이 안맞으면 호환이 안될수도 있음!

     

    준비물들이 모두 설치되어있다는 가정하에 Apache Tomcat과 MySQL 연동하는 방법을 알아보겠습니다.

     

    1. 

    C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext

    C:\Program Files\Java\jre1.8.0_201\lib\ext 

    이 두 곳에 MySQL Connector를 각각 넣어준다.

     

    C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext에 mySQL connector를 복사
    C:\Program Files\Java\jre1.8.0_201\lib\ext에 mySQL connector를 복사

     

    2. 

    C:\Program Files\apache-tomcat-8.5.38-windows-x64\apache-tomcat-8.5.38\lib

    이 곳에 mySQL Connector를 넣어준다. 

     

    C:\Program Files\apache-tomcat-8.5.38-windows-x64\apache-tomcat-8.5.38\lib에 mySQL connector를 복사

     

    3.

    MySQL Commanline Client를 실행하여 연동 테스트를 위한 데이터베이스를 다음과 같이 생성하였다.

    Database : TestDB

    Table : TestTB

    Field : a(varchar), b(varchar), c(varchar)

    먼저 이클립스와 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
    <%@ page import = "java.sql.*" %>
    <%
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            String url = "jdbc:mysql://localhost:3306/TestDB";
            String id = "root"
            String pw = "abcd1234"
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, id, pw); // jsp와 db 연결
            out.println("mysql connection");
            
            
            
            conn = DriverManager.getConnection(url, id, pw);
            String sql = "insert into TestTB values(?,?,?)"// 필드 수를 잘 맞춰야함
            pstmt = conn.prepareStatement(sql); // sql 실행
            pstmt.setString(1"I");
            pstmt.setString(2"want");
            pstmt.setString(3"Jong-Gang");
            pstmt.executeUpdate();
            out.println("insert Ok");
     
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("output.jsp");
     
     
    %>
     
     
    cs

    a.jsp

    output.jsp

    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
    <%@ page  contentType="text/html;charset=utf-8"
            import="java.sql.DriverManager,
                       java.sql.Connection,
                       java.sql.Statement,
                       java.sql.ResultSet,
                       java.sql.SQLException" %>
    <%
      response.setContentType("text/html;charset=utf-8;");
      request.setCharacterEncoding("euc-kr");       Class.forName("com.mysql.jdbc.Driver");    
      String DB_URL =
              "jdbc:mysql://localhost:3306/TestDB?useUnicode=true&characterEncoding=utf-8";
    String DB_USER = "root";
      String DB_PASSWORD= "abcd1234";
      Connection conn= null;
      Statement stmt = null;
      ResultSet rs   = null;
      try {
          conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
          stmt = conn.createStatement();
          String query = "select * from TestTB";
          rs = stmt.executeQuery(query);
     %>
    <table border="1" cellspacing="0">
    <tr>
    <td>a</td>
    <td>b</td>
    <td>c</td>
    </tr>
    <%
        while(rs.next()) { 
    %><tr>
    <td><%=rs.getString("a")%></td>
    <td><%=rs.getString("b")%></td>
    <td><%=rs.getString("c")%></td>
    </td>
    </tr>
    <%
        } // end while
    %></table>
    </form>
    <%
      rs.close();        
      stmt.close();     
      conn.close();    
    }
    catch (SQLException e) {
          out.println("err:"+e.toString());
    }
    %>
     
    cs

     

     

    이를 실행해보면 다음 결과를 통해 연동 성공을 확인할 수 있다.

     

    Eclipse에서 실행 한 결과
    크롬에서 실행 한 결과
    MySQL Command Line에서 실행 한 결과

     

    '프로그래밍 > JSP' 카테고리의 다른 글

    [JSP + Oracle] 병원 업무 관리 프로젝트  (0) 2019.01.16

    댓글

Designed by Tistory.