0. ¼Ò°³
MySQL°ú mm.mysql JDBC µå¶óÀ̹ö°¡ Àß ¿¬µ¿µÈ´Ù°í º¸°íµÈ ¹öÀüÀº ´ÙÀ½°ú °°½À´Ï´Ù.
- MySQL 3.23.47, MySQL 3.23.47 using InnoDB, MySQL 4.0.1alpha
- mm.mysql 2.0.14 (JDBC Driver)
»õ·Î MySQL mm.mysql 3.0 driver¸¦ Å×½ºÆ®ÇØºÃ´Ù¸é ¾Ë·ÁÁֽñ⠹ٶø´Ï´Ù.
1. MySQL ¼³Á¤
´Þ¸®ÇÏ¸é ¹®Á¦¸¦ ÀÏÀ¸Å³ ¼öµµ ÀÖÀ¸¹Ç·Î ÀÌ Áö½Ã¸¦ ¹Ýµå½Ã µû¸£½Ê½Ã¿À.
test À¯Àú, »õ µ¥ÀÌŸº£À̽º, Å×½ºÆ® Å×À̺í Çϳª¸¦ ¸¸µå½Ê½Ã¿À. mySQL À¯Àú´Â ÇÒ´çµÈ ÆÐ½º¿öµå°¡ ÀÖ¾î¾ßÇÕ´Ï´Ù. ºñ¾îÀÖ´Â ÆÐ½º¿öµå¸¦ °¡Áö°í¼´Â µå¶óÀ̹ö°¡ ¿¬°á¿¡ ½ÇÆÐÇÒ °ÍÀÔ´Ï´Ù.
 |  |  |
 |
mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@localhost
-> IDENTIFIED BY 'javadude' WITH GRANT OPTION;
mysql> create database javatest;
mysql> use javatest;
mysql> create table testdata (
-> id int not null auto_increment primary key,
-> foo varchar(25),
-> bar int);
|  |
 |  |  |
ÁÖÀÇ: À§ÀÇ À¯Àú´Â Å×½ºÆ®°¡ ³¡³ª¸é Á¦°ÅµÇ¾î¾ß ÇÕ´Ï´Ù.
´ÙÀ½À¸·Î testdata Å×ÀÌºí¿¡ Å×½ºÆ® µ¥ÀÌŸ¸¦ ³ÖÀ¸½Ê½Ã¿À.
 |  |  |
 |
mysql> insert into testdata values(null, 'hello', 12345);
Query OK, 1 row affected (0.00 sec)
mysql> select * from testdata;
+----+-------+-------+
| ID | FOO | BAR |
+----+-------+-------+
| 1 | hello | 12345 |
+----+-------+-------+
1 row in set (0.00 sec)
mysql>
|  |
 |  |  |
2. server.xml ¼³Á¤
$CATALINA_HOME/conf/server.xml¿¡ ¸®¼Ò½º ¼±¾ðÀ» Ãß°¡ÇÏ¿© Tomcat¿¡¼ÀÇ JNDI µ¥ÀÌŸ¼Ò½º¸¦ ¼³Á¤ÇϽʽÿÀ.
À̰ÍÀ» examples ÄÁÅØ½ºÆ®ÀÇ </Context>¿Í localhost Á¤ÀǸ¦ ´Ý´Â ű×ÀÎ </Host> »çÀÌ¿¡ Ãß°¡ÇϽʽÿÀ.
 |  |  |
 |
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB 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.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>javauser</value>
</parameter>
<parameter>
<name>password</name>
<value>javadude</value>
</parameter>
<!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>
|  |
 |  |  |
3. web.xml ¼³Á¤
ÀÌÁ¦ Å×½ºÆ® ¾ÖÇø®ÄÉÀ̼ǿ¡ ´ëÇÑ WEB-INF/web.xmlÀ» ¸¸µå½Ê½Ã¿À.
 |  |  |
 |
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<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. Å×½ºÆ® ÄÚµå
ÀÌÁ¦ ÀÌÈÄ¿¡ »ç¿ëÇÒ °£´ÜÇÑ test.jsp ÆÄÀÏÀ» ¸¸µå½Ê½Ã¿À.
 |  |  |
 |
<html>
<head>
<title>DB Test</title>
</head>
<body>
<%
foo.DBTest tst = new foo.DBTest();
tst.init();
%>
<h2>Results</h2>
Foo <%= tst.getFoo() %><br/>
Bar <%= tst.getBar() %>
</body>
</html>
|  |
 |  |  |
»õ·Î ¸¸µé¾îÁø µ¥ÀÌŸ¼Ò½º¿Í Ä¿³Ø¼ÇÇ®À» ½ÇÁ¦·Î »ç¿ëÇÒ Àڹ٠Ŭ·¡½º¸¦ ¸¸µå½Ê½Ã¿À. ÁÖÀÇ: ÀÌ ÄÚµå´Â ½Ç¹«¿¡¼ ¾²ÀÏ ¸¸ÇÑ ÄÚµå´Â ¾Æ´Õ´Ï´Ù. ´ÜÁö Å×½ºÆ® ¸ñÀûÀ¸·Î¸¸ »ç¿ëµÇ´Â °ÍÀÔ´Ï´Ù. :-)
 |  |  |
 |
package foo;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class DBTest {
String foo = "Not Connected";
int bar = -1;
public void init() {
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/jdbc/TestDB");
if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) {
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery(
"select id, foo, bar from testdata");
if(rst.next()) {
foo=rst.getString(2);
bar=rst.getInt(3);
}
conn.close();
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
public String getFoo() { return foo; }
public int getBar() { return bar;}
}
|  |
 |  |  |
¸¶Áö¸·À¸·Î À¥ ¾ÖÇø®ÄÉÀ̼ÇÀ» $CATALINA_HOME/webapps¿¡ DBTest.war¶ó´Â À̸§ÀÇ war ÆÄÀÏÀ̳ª DBTest¶ó´Â ¼ºê µð·ºÅ丮·Î ¹èÄ¡ÇϽʽÿÀ.
¹èÄ¡µÇ°í ³ª¸é ½ÇÁ¦·Î ½ÇÇàÀÌ µÇ´ÂÁö º¸±â À§ÇØ ºê¶ó¿ìÀú¿¡¼ http://localhost:8080/DBTest/test.jsp¸¦ ¿¾îº¸½Ê½Ã¿À.