MySQL+Apache Tomcat 6.0 JNDI Datasource How to

2012. 12. 12. 18:44Language/Java

반응형
데이터소스를 설정 해주는 것은 많이 있다.

그 중에 톰캣을 가지고 데이터소스 연동하는 것을 해보겠다.
Apache Tomcat 6.0

데이터베이스는 MySQL 이다.

참고 사이트 :
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

필요사항
JDBC Driver
Connector/J 3.0.11-stable (the official JDBC Driver)

다운을 받은 뒤에
Before you proceed, don't forget to copy the JDBC Driver's jar into $CATALINA_HOME/lib.
톰캣 홈쪽에 lib에 넣어준다.



뭐..그전에 MySQL과 Web어플리케이션이 있어야하겠다.
2012/10/28 - [OpenSource/Spring] - [Spring 환경 구축] step 02 - MySQL을 깔아보자~~~(Windows)
2012/10/28 - [OpenSource/Spring] - [오류] Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
2012/10/28 - [DataBase] - MySQL 기본 문법(데이터베이스 보기, 사용하기)

위의 링크를 통해 MySQL을 셋팅하면 된다.

셋팅이 다 했다는 가정하에 진행 해보겠다.^-^good~

밑에서 처럼 유저를 생성해준다.
javauser <---user명
jaadude <---password
javatest <--- database명 

 
mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@localhost 
    ->   IDENTIFIED BY 'javadude' WITH GRANT OPTION;
mysql> create database javatest;   <-- javatest로 데이터베이스를 생성
mysql> use javatest;       <-- 이 데이터베이스를 사용하겠다.
mysql> create table testdata (  <-- 테이블 생성
    ->   id int not null auto_increment primary key,
    ->   foo varchar(25), 
    ->   bar int);


insert문을 통해 값을 하나 넣어준다. 
mysql> insert into testdata values(null, 'hello', 12345);
Query OK, 1 row affected (0.00 sec)
select를 통해 들어갔는지 확인 해준다.
mysql> select * from testdata;
+----+-------+-------+
| ID | FOO   | BAR   |
+----+-------+-------+
|  1 | hello | 12345 |
+----+-------+-------+
1 row in set (0.00 sec)

mysql>


2. Context configuration(톰캣쪽에 보면 context.xml이 있다.)


 
<Context>

    <!-- maxActive: Maximum number of database connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to -1 for no limit.
         -->

    <!-- maxIdle: Maximum number of idle database connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

    <!-- maxWait: Maximum time to wait for a database connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->

    <!-- username and password: MySQL username and password for database connections  -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->
    
    <!-- url: The JDBC connection url for connecting to your MySQL database.
         -->

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest"/>

</Context>

ex) 글쓴이가 작성한 내용.



3. web.xml(tomcat쪽의 web.xml이 아니다.)
    Now create a WEB-INF/web.xml for this test application.
 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>MySQL Test App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

4. 테스트 코드
 
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from testdata
</sql:query>

<html>
  <head>
    <title>DB Test</title>
  </head>
  <body>

  <h2>Results</h2>
  
<c:forEach var="row" items="${rs.rows}">
    Foo ${row.foo}<br/>
    Bar ${row.bar}<br/>
</c:forEach>

  </body>
</html>


5. 결과



아파치에서 제공 되어지는 가이드를 보고 해보았다.
굿이다! ^-^good~

반응형

'Language > Java' 카테고리의 다른 글

번들 property 만들기  (0) 2013.05.10
JMS란?  (1) 2013.01.07
JNDI란 정확히 뭔데??  (4) 2012.12.12
[용어] JNDI  (0) 2012.11.12
Failed to create the Java Virtual Machine  (0) 2012.09.07