본문 바로가기
Computer Base/Web&Was

Tomcat9 - Mysql8.0 JDBC연동

by 팡팡마트 2023. 10. 27.
728x90

**CentOS 8 Stream

 

1. 가상머신 2대에 각각 Tomcat9, Mysql 8.0 설치 진행

Linux : Tomcat9 설치 (tistory.com)

 

Linux : Tomcat9 설치

** CentOS 8 Stream **open JDK가 설치되어야함 Linux : open jdk11 설치 (tistory.com) Linux : open jdk11 설치 **Centos 8 stream 1. 패키지 설치 # yum install java-11-openjdk-devel.x86_64 2. 설치 확인 # java –version 3. 환경변수 설정

pangpangmart.tistory.com

MySQL 8.0 설치 (tistory.com)

 

MySQL 8.0 설치

** CentOS 8 Stream 1. mysql 다운로드 링크 확인 MySQL :: MySQL Community Edition MySQL :: MySQL Community Edition MySQL Community Edition MySQL Community Edition is the freely downloadable version of the world's most popular open source database. It

pangpangmart.tistory.com

테스트용 DB 및 테이블 생성

계정, 허용IP 등 설정

 

2. Mysql-connector 다운로드

MySQL :: Download MySQL Connector/J (Archived Versions)

 

MySQL :: Download MySQL Connector/J (Archived Versions)

Please note that these are old versions. New releases will have recent bug fixes and features! To download the latest release of MySQL Connector/J, please visit MySQL Downloads. MySQL open source software is provided under the GPL License.

downloads.mysql.com

mysql 버전 및 Platform Independent로 선택하여 tar 다운로드 링크 복사

톰캣 서버에서 패키지 다운로드

# wget https://downloads.mysql.com/archives/get/p/3/file/mysql-connector-java-8.0.26.tar.gz

 

압축해제

# tar zxvf mysql-connector-java-8.0.26.tar.gz

 

압축 해제한 폴더안의 mysql-connector-java-8.0.26.jar 파일을 톰캣폴더의 lib로 이동

# cp -a /home/azure/mysql-connector-java-8.0.26/mysql-connector-java-8.0.26.jar /home/azure/apache-tomcat-9.0.82/lib

# cp -a /home/azure/mysql-connector-java-8.0.26/mysql-connector-java-8.0.26.jar /usr/lib/jvm/java-11-openjdk-1.0.18.0.9-0.3.ea.el8.x86_64/lib

 

3. 톰캣 설정

# vi /home/azure/apache-tomcat-9.0.82/conf/context.xml

context 안에 아래 내용 입력

<Resource name="jdbc/DB이름"
        auth="Container"
        type="javax.sql.DataSource"
        username="root"
        password="password"
        driverClassName="cohttp://m.mysql.jdbc.Driver"
        url="jdbc:mysql://[DB서버IP]:3306/DB이름" 
        maxActive="15"
        maxIdle="3"/>

 

# vi /home/azure/apache-tomcat-9.0.82/conf/web.xml

web-app 안에 아래 내용 입력

    <resource-ref>
        <res-ref-name>jdbc/[DB이름]</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

 

4. 테스트용 jsp 작성

index.jsp 편집

# vi /home/azure/apache-tomcat-9.0.82/webapps/ROOT/index.jsp

<%@ page import = "java.sql.*" %>
<%
  try{
        Class.forName("cohttp://m.mysql.jdbc.Driver");
        String url = "jdbc:mysql://10.0.0.5:3306/tomcat";
        Connection conn = DriverManager.getConnection(url, "root", "[DB비밀번호]");
        out.println("Tomcat9-Mysql8.0");
        PreparedStatement ps=(PreparedStatement)conn.prepareStatement("SELECT * from test1");
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
                String number=rs.getString("name");
                out.println(number);
        }
  }
  catch(Exception e){
        out.print(e.toString());
  }
%>

 

페이지 확인

 

728x90