MYSQL - Tomcat 연동
mysql-connector-java 설치
[root@localhost]# wget http://mysql.new21.com/Downloads/Connector-J/mysql-connector-java-3.0.17-ga.tar.gz
[root@localhost]# tar xvfz mysql-connector-java-3.0.17-ga.tar.gz
[root@localhost]# cd mysql-connector-java-3.0.17-ga
[root@localhost]# cp mysql-connector-java-3.0.17-ga-bin.jar /usr/local/j2sdk/jre/lib/ext/
[root@localhost]# vi /etc/profile
JAVA_HOME=/usr/local/j2sdk
CATALINA_HOME=/usr/local/tomcat
PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
export JAVA_HOME CATALINA_HOME
위의 부분을 다음과 같이 수정한다.
JAVA_HOME=/usr/local/j2sdk
CATALINA_HOME=/usr/local/tomcat
PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$CATALINA_HOME/common/lib/servlet.jar:$JAVA_HOME/jre/lib/ext/mysql-connector-java-3.0.17-ga-bin.jar:/usr/local/mysql/bin
export JAVA_HOME CATALINA_HOME
저장 하고 나온다.
[root@localhost]# source /etc/profile
[root@localhost]# set | grep PATH
[root@localhost]# javap org.gjt.mm.mysql.Driver
하고 아래메시지가 나오면 정상이다.
Compiled from "Driver.java"
public class org.gjt.mm.mysql.Driver extends com.mysql.jdbc.Driver{
public org.gjt.mm.mysql.Driver();
throws java/sql/SQLException
}
[root@localhost]# cd /usr/local/tomcat/common/lib
[root@localhost]# ln -s /usr/local/j2sdk/jre/lib/ext/mysql-connector-java-3.0.17-ga-bin.jar /usr/local/tomcat/common/lib/mysql-connector-java-3.0.17-ga-bin.jar
자바 소스 테스트
[root@localhost]# cd /usr/local/src
[root@localhost]# vi jdbc.java
//--여기서부터-------------------------------------------------
import java.sql.*;
class jdbc
{
public static void main(String argv[])
{
try
{
Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("jdbc 드라이버 로딩 성공");
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
try
{
String url = "jdbc:mysql://자신의 아이피:3306/mysql";
Connection con = DriverManager.getConnection(url,"root","");
System.out.println("mysql 접속 성공");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select user from user where user='root'");
System.out.println("Got result:");
while(rs.next())
{
String no= rs.getString(1);
String tblname = rs.getString(1);
System.out.println(" no = " + no);
System.out.println(" tblname= "+ tblname);
}
stmt.close();
con.close();
}
catch(java.lang.Exception ex)
{
ex.printStackTrace();
}
}
}
//--여기까지-------------------------------------------------
이제 부터 jsp에서 mysql연결 테스트 입니다.
[root@localhost]# cd /home/www
[root@localhost]# vi test.jsp
//--여기서부터-------------------------------------------------
<%@ page import="java.sql.*" contentType="text/html;charset=euc-kr"%>
<%
String DB_URL = "jdbc:mysql://자신의 아이피:3306/mysql";
String DB_USER = "root";
String DB_PASSWORD= "root비밀번호입력";
Connection conn;
Statement stmt;
try
{
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USER,DB_PASSWORD);
stmt = conn.createStatement();
conn.close();
out.println("MySQL 연결 성공");
}
catch(Exception e)
{
out.println(e);
}
%>
//--여기까지-------------------------------------------------
[root@localhost /]# /usr/local/apache2/bin/apachectl stop
[root@localhost /]# /usr/local/tomcat/bin/catalina.sh stop
[root@localhost /]# /usr/local/tomcat/bin/catalina.sh start
[root@localhost /]# /usr/local/apache2/bin/apachectl start
브라우저로 http://자신의 아이피/test.jsp
해서 "MySQL 연결 성공"이라는 메시지가 나오면 성공!!
출처: https://applejara.tistory.com/19 [애플자라]
'Database > mysql' 카테고리의 다른 글
mysql 암호 잃어버렸을때.. (0) | 2020.07.13 |
---|---|
레드햇 부팅시 mysql 자동시작(apache포함) (0) | 2020.07.13 |
MYSQL DB생성 & 사용자계정생성 / 삭제 (0) | 2020.07.13 |
mysql 사용자등록,기본사용법 (0) | 2020.07.13 |
Mysql 설치 (0) | 2020.07.13 |
[Mysql] GROUP_CONCAT 사용 방법 (0) | 2020.07.13 |
[Mysql] DB 이중화하기 (Replication) (0) | 2020.07.13 |
Mysql 기본 명령어 정리 (0) | 2020.07.13 |