[JSP] 커넥션 풀(connection pool) - mac, mysql

반응형

나는 지금 mac 과 mysql로 jsp를 공부하고 있는 중이다.  


참고로 맥에서는 oracle을 지원하지 않는 관계로..... mysql을 사용해서 게시판 만들기를 진행하려니 많은 어려움이 앞을 가로 막는다...


커넥션 풀 공부에 도움이 되는 자료 링크들

 

이론 및 사용법 


http://devbox.tistory.com/entry/JSP-커넥션-풀-1


-https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-j2ee-concepts-connection-pooling.html  (mysql 공식 홈페이지 - connect/j)


-https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Example(mysql 공식 홈페이지)


정리


tomcat의 context.xml

1
2
3
4
5
6
<Context>
    <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>
cs


작업 프로젝트의 WEB-INF/web.xml

1
2
3
4
5
6
7
8
<web-app
    <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>
cs


마지막으로


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ 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>
cs


이것이 test.jsp 인데 아직 JSTL은 진도를 나가지 않아서 태그같은걸 모르겠다.... 오라클이였다면 손쉽게 했을텐데.....

맥이 원망스러워지는 순간이다.... (아직 테스트를 못해봄... ㅜㅜ) 

여하튼 태그를 이해하고 나서 다시 도전해보자!!

(중간의 TestDB에 나의 리소스명을 그냥 넣으면 됨)


ps.혹여나 테스트 성공하신분 댓글이라도... 구걸구걸..


ps2. 마지막 test.jsp가 아니더라도 그냥 oracle처럼 사용 해도 되더라.... (import 다 제대로 한경우!! - dataSource 특히 조심 sql.dataSource)


1
2
3
4
5
Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
//etc.
cs


반응형

이 글을 공유하기

댓글

Designed by JB FACTORY