JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » JDBC/SQL討論區 » Connection Pool  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:[問題]如何實作Connection Pool?? [Re:ㄚ孝]
OHNO

雨聲街



發文: 49
積分: 0
於 2003-07-31 21:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這個是網站中教我們用 tomcat 去設定和執行範例的設定和程式
1. server.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<Context path="/csc" docBase="D:\My Webs\csc"
        debug="5" reloadable="true" crossContext="true">
 
  <Logger className="org.apache.catalina.logger.FileLogger"
             prefix="localhost_DBTest_log." suffix=".txt"
             timestamp="true"/>
 
  <Resource name="jdbc/csc"
               auth="Container"
               type="javax.sql.DataSource"/>
 
  <ResourceParams name="jdbc/csc">
    <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>300</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>sa</value>
    </parameter>
    <parameter>
     <name>password</name>
     <value>xxxxx</value>
    </parameter>
 
    <parameter>
       <name>driverClassName</name>
       <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </parameter>
 
   
    <parameter>
      <name>url</name>
      <value>jdbc:microsoft:sqlserver://140.127.xx.xxx:1433;DatabaseName=csc</value>
    </parameter>
  </ResourceParams>
</Context>
2. web.xml configuration
<?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>csc test app</description>
  <resource-ref>
      <description>db</description>
      <res-ref-name>jdbc/csc</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

3. 程式demo

jsp 的部份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@page contentType="text/html;charset=Big5"%>
<html>
  <head>
    <title>DB Test</title>
  </head>
  <body>
 
  <%
  
    test.test tst = new test.test();
    tst.init();
  %>
 
    索引:<%=tst.getIndex()%><br>
    姓名:<%=tst.getName()%><br>
    名言:<%=tst.getCooio()%><p>
   
  </body>
</html>


javabean 的部份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package test;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
 
public class test {
 
  int index = 0;
  String name = "你又失敗了!!!!";
  String cooio = "真是笨!!";
    
  public void init() {
    try{
      Context ctx = new InitialContext();
      if(ctx == null ) 
          throw new Exception("Oh!NO! 沒有連上去!");
 
      DataSource ds = 
            (DataSource)ctx.lookup(
               "java:comp/env/jdbc/csc");
 
      if (ds != null) {
        Connection conn = ds.getConnection();
              
        if(conn != null)  {
           
            Statement stmt = conn.createStatement();
            ResultSet rst =  stmt.executeQuery("select * from test");
            if(rst.next()) {
               index=rst.getInt("testindex");
               name=rst.getString("testname");
               cooio = rst.getString("teststring");
            }
            conn.close();
        }
      }
    }catch(Exception e) {
      e.printStackTrace();
    }
 }
 public int getIndex(){return index;}
 public String getName() { return name; }
 public String getCooio() { return cooio;}
}


anthonychen edited on 2004-04-27 15:20
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
30703 [精華] [問題]如何實作Connection Pool?? ㄚ孝 300 2003-06-16 09:27
29074 Re:[問題]如何實作Connection Pool?? browser 460 2003-06-16 09:50
28042 Re:[問題]如何實作Connection Pool?? jini 72 2003-07-30 13:29
27846 Re:[問題]如何實作Connection Pool?? Forth 40 2003-07-30 13:42
27713 Re:[問題]如何實作Connection Pool?? jini 105 2003-07-30 13:59
27449 Re:[問題]如何實作Connection Pool?? Forth 13 2003-07-30 14:09
27331 Re:[問題]如何實作Connection Pool?? browser 100 2003-07-30 14:08
30085 Re:[問題]如何實作Connection Pool?? anthonychen 96 2003-07-30 17:48
27035 Re:[問題]如何實作Connection Pool?? browser 213 2003-07-30 18:12
27204 Re:[問題]如何實作Connection Pool?? sdargon 415 2003-08-11 13:46
27231 Re:[問題]如何實作Connection Pool?? jansmin 132 2003-08-11 14:29
27748 Re:[問題]如何實作Connection Pool?? browser 415 2003-08-11 14:41
27602 Re:[問題]如何實作Connection Pool?? OHNO 3993 2003-07-31 21:04
19311 Re:[問題]如何實作Connection Pool?? dg0712 4109 2005-07-21 17:21
26880 Re:[問題]如何實作Connection Pool?? gene 38 2003-08-03 00:36
26808 Re:[問題]如何實作Connection Pool?? snpshu 128 2003-08-03 01:15
26884 Re:[問題]如何實作Connection Pool?? anthonychen 630 2003-08-03 01:38
23649 Re:[問題]如何實作Connection Pool?? ppbaby 112 2004-10-29 00:46
21002 Re:[問題]如何實作Connection Pool?? metavige 169 2005-03-11 17:34
21865 Re:[問題]如何實作Connection Pool?? alpha_dyz 59 2005-03-11 16:53
21000 Re:[問題]如何實作Connection Pool?? je 489 2005-09-16 12:38
27438 Re:[問題]如何實作Connection Pool?? ㄚ孝 646 2003-06-17 10:48
27245 Re:[問題]如何實作Connection Pool?? ㄚ孝 243 2003-06-18 12:27
27802 Re:[問題]如何實作Connection Pool?? Forth 45 2003-06-18 12:32
27073 Re:[問題]如何實作Connection Pool?? ㄚ孝 84 2003-06-18 14:10
27833 Re:[問題]如何實作Connection Pool?? ROACH 54 2003-07-02 20:11
28252 Re:[問題]如何實作Connection Pool?? rEdMiddLe 1570 2003-07-09 13:02
26945 Re:[問題]如何實作Connection Pool?? ray_linn 149 2003-07-17 08:52
26864 Re:[問題]如何實作Connection Pool?? macross 67 2003-07-29 13:50
26622 Re:[問題]如何實作Connection Pool?? duba 26 2003-12-21 14:43
» JWorld@TW »  JDBC/SQL討論區 » Connection Pool

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8