IT/잡다구리

[Linux] Centos7 JDK 1.8 설치 , Tomcat 설치 , Mysql 연동

월공 2021. 4. 26. 17:10
728x90
300x250

jdk 압축파일(jdk-8u281-linux-x64.tar.gz)은 wget 해서 다운받거나 , 혹은 FTP로 보내서 갖고있다는 가정하에 진행합니다.

 

톰캣 설치 및 세팅

//경로변경
cd /usr/local/lib

//톰캣 설치파일 다운
wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.5.59/bin/apache-tomcat-8.5.59.tar.gz

//톰캣 압축해제
tar zxvf apache-tomcat-8.5.59.tar.gz

//환경설정 (CLASSPATH 의 mysql 연동 부분도 포함)
vi /etc/profile

##################################################################

JAVA_HOME=/usr/local/java/jdk1.8.0_281
JRE_HOME=$JAVA_HOME/jre
CATALINA_HOME=/usr/local/lib/apache-tomcat-8.5.59
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/lib/jsp-api.jar:$CATALINA_HOME/lib/servlet-api.jar:%JAVA_HOME%/lib/mysql-connector-java-8.0.24.jar
PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
export JAVA_HOME JRE_HOME CLASSPATH PATH CATALINA_HOME

##################################################################

//환경설정 반영
source /etc/profile

//설정 확인
echo $CATALINA_HOME

//실행 or 중지
cd /usr/local/lib/apache-tomcat-8.5.59/bin

//실행
./startup.sh

//중지
./shutdown.sh

 

톰캣 가동시 기본포트는 8080

 

mysql-connector-java-8.0.24.jar
2.32MB

톰캣의 lib , jdk의 lib 폴더에 각각 mysql-connector-java.jar 파일을 갖다놓고

profile CLASSPATH 설정해주니 연동이 이상없이 잘되었다

/usr/local/lib/apache-tomcat-8.5.59/lib/mysql-connector-java-8.0.24.jar
/usr/local/java/jdk1.8.0_281/lib/mysql-connector-java-8.0.24.jar

 

 

### /usr/local/lib/apache-tomcat-8.5.59/webapps/ROOT/test.jsp

<%@ page import="java.sql.*" contentType="text/html;charset=utf-8"%>
<%
         String DB_URL = "jdbc:mysql://DB IP주소/DB명";
         String DB_USER = "유저명";
         String DB_PASSWORD= "비밀번호";

         ResultSet rs = null;

         Connection conn;

         Statement stmt;

         try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
                stmt = conn.createStatement();
                // conn.close();
                out.println("MySQL Connection Success!");
				
                /*
                rs = stmt.executeQuery("테스트 쿼리");
                while(rs.next()) {
                    out.println("<tr>");
                    out.println("<td>" + rs.getString("test1") + "</td>");
                    out.println("<td>" + rs.getString("test2") + "</td>");
                    out.println("</tr>");
                }
                */

         }

         catch(Exception e){

              out.println(e);

         }

%>
~

 

 

728x90
300x250