ÃֽŠ°Ô½Ã±Û(OS/WAS)
2018.11.17 / 23:39

Tomcat 7.x + JDBC MySQL

hanulbit
Ãßõ ¼ö 184
1. JDBC ¼³Ä¡
 MySQL - Download - MySQL Connectors - Connector/J  Download
 Tomcat/lib Æú´õ¿¡ ³Ö¾îÁØ´Ù.

 È¤Àº Maven - poem.xml ¼³Á¤À¸·Î Çϸé ÀÚµ¿À¸·Î Ãß°¡ÇØÁØ´Ù. 
 http://www.jarvana.com/jarvana/browse ¿¡¼­ µå¶óÀ̹ö Á¤º¸¸¦ ãÀ»¼öÀÖ´Ù.
 <!-- MySQL -->
 <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.16</version>
  </dependency> 

2. DataSource ¼³Á¤ ¹× Å×½ºÆ® - ( Tomcat 7.x ±âÁØ )
 ( http://tomcat.apache.org/ ÀÇ document ¿¡¼­ JDBC DataSources ÂüÁ¶. )
i) µðºñ Áغñ
  create database test;
  use test;
  create table testdata(
   id int not null auto_increment primary key,
   foo varchar(25), 
   bar int);
  insert into testdata values(null, 'hello', 12345);
ii) ¿Í½º ¼³Á¤
 context.xml
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
       maxActive="100" maxIdel="30" maxWait="1000"
       username="¾ÆÀ̵ð" password="ºñ¹ø" driverClassName="com.mysql.jdbc.Driver"
       url="jdbc:mysql://localhost:3306/test"/>
( ÆĶõ»öÀ¸·Î Ç¥½ÃÇÑ°Ç Àڽſ¡°Ô ¸Â°Ô Àû´çÈ÷ ¹Ù²Ù¸é µÈ´Ù. )
iii) web ¼³Á¤
 web.xml 
 <resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/mysql</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
 </resource-ref>
iv) test
 
  test.jsp
 
<%@ 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>