jsp Mysql Datasource 的設定

資料庫管理及MySQL資料庫系統相關議題
回覆文章
dtchang
Site Admin
文章: 84
註冊時間: 2017-01-22, 16:54

jsp Mysql Datasource 的設定

文章 dtchang » 2018-04-28, 21:17

參考資料: DBCP BasicDataSource Configuration Parameters
http://commons.apache.org/proper/common ... ation.html


在自己的程式目錄下,建構二個子目錄
index.jsp
WEB-INF/web.xml
WEB-INF/sun-web.xml (應該是不用)
META-INF/context.xml

WEB-INF/web.xml

代碼: 選擇全部

<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 chucec</description>
      <res-ref-name>jdbc/chucec</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  
  <resource-ref>
      <description>DB Connection TestDB</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
  
  <welcome-file-list> 
                <!-- Use index.swf if present, or splash.jsp, otherwise just look for the normal defaults --> 
                <welcome-file>index.html</welcome-file> 
                <welcome-file>index.htm</welcome-file> 
                <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
  
</web-app>
META-INF/context.xm

代碼: 選擇全部

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/chucec" 
              auth="Container"
              type="javax.sql.DataSource" 
              username="帳號" 
              password="密碼"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/chucec"	
connectionProperties="useSSL=false;useUnicode=yes;characterEncoding=utf8;"			  
              validationQuery="select 1"
              removeAbandoned="true"
              removeAbandonedTimeout="120"
              logAbandoned="true"
              maxWait="6000"
              maxActive="150" 
              maxIdle="4" />
			  			  
 <ResourceLink name="jdbc/chucec"
             global="jdbc/chucec"
              type="javax.sql.DataSource" />					  
</Context>
測試程式: test.jsp

代碼: 選擇全部

<%@ page contentType="text/html;charset=utf-8" %>
<%@ 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/chucec">
select ID, NAME from cec_user
</sql:query>

<html>
  <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <title>DB Test</title>
  </head>
  <body>

  <h2>Results</h2>
<c:forEach var="i" begin="0" end="10" step="1">
<span>${ i }</span>
</c:forEach>
<c:forEach var="row" items="${rs.rows}">
    Foo ${row.ID}<br/>
    Bar ${row.NAME}<br/>
</c:forEach>

  </body>
</html>
WEB-INF/sun-web.xml

代碼: 選擇全部

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd">
<sun-web-app>
  <resource-ref>
    <res-ref-name>jdbc/chucec</res-ref-name>
    <jndi-name>jdbc/chucec</jndi-name>
  </resource-ref>
</sun-web-app>

回覆文章