이클립스 톰캣에서 mysql 연동
1. server.xml
<Context docBase="이클립스 프로젝트명" path="" reloadable="true" source="org.eclipse.jst.jee.server:이클립스 프로젝트명"> //이 줄은 기본으로 있을듯
<Resource name="ABCD" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="100"
username="mysql 아이디" password="mysql 비밀번호"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://IP주소:3306/데이터베이스명"/>
</Context>
* name="ABCD" 여기는 DB명이 아니고 연결 확인을 위한 이름
* 3306은 mysql 접속 기본 포트
* jdbc:mysql://IP주소:3306/데이터베이스명
ex) jdbc:mysql://127.0.0.1:3306/test_dev
2. web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>ABCD</res-ref-name> <- <res-ref-name>ABCD</res-ref-name>여기도 연결확인을 위한 이름
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3. context.xml
<ResourceLink name ="jdbc/mysql"
global="ABCD" <- global="ABCD" 여기도 연결확인을 위한 이름
type="com.mysql.jdbc.Driver"/>
잘은 모르겠지만.
context.xml에
<WatchedResource>WEB-INF/web.xml</WatchedResource>
이 부분이 있다면 web.xml에만 추가를 하고
3번 context.xml은 수정하지 않아도 되는 것 같다.
4. DBConnection.java
public class DBConnection {
private static DataSource ds;
private static final String sDsName = "ABCD";
//sDsName = "ABCD" 여기는 DB명이 아니고 연결 확인을 위한 이름
public static Connection getConnection() throws Exception {
Connection con=null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup(sDsName);
con= ds.getConnection();
if (con != null) {
System.out.println("데이터베이스 연결됐음");
con.setAutoCommit(false);
}
} catch (NamingException ne) {
System.out.println(ne);
}
return con;
}
}