Chinese To English     英文 轉(zhuǎn) 中文             
                   
          隨筆-27  評(píng)論-53  文章-0  trackbacks-0

                  Tomcat數(shù)據(jù)源的配置是件很有意義的事情,因?yàn)樗山o我程序提供更好的性能,所以決定寫這篇隨筆給java初學(xué)者一個(gè)參考。磨刀不誤砍柴工,我們先來看一個(gè)檔。
                  啟動(dòng)Tomcat6.x ——在IE中輸入http://localhost:8080——點(diǎn)左邊的Tomcat Documentation超鏈接——再點(diǎn)擊JNDI ResourcesJDBC DataSources兩個(gè)鏈接到此Tomcat數(shù)據(jù)源的配置答案應(yīng)該以經(jīng)找到了,文檔中寫的很清楚!下面我給出一個(gè)MySQL數(shù)據(jù)源配置的示例:

          1、將MySQL的jdbc驅(qū)動(dòng)包考貝到%CATALINA_HOME%/lib目錄下;
          2、打開%CATALINA_HOME%/conf/context.xml文件,并在context元素之間添加以下代碼:

          <Resource name="jdbc/MySQL" auth="Container" type="javax.sql.DataSource"
                         maxActive
          ="100" maxIdle="30" maxWait="10000"
                         username
          ="root" password="mysql" driverClassName="com.mysql.jdbc.Driver"
                         url
          ="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>

          3、打開MyEclipse新建一些個(gè)WEB工程,修改index.jsp頁面如下:

           1<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
           2<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>
           3
           4<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
           5<html>
           6    <head>
           7        <title>My JSP 'index.jsp' starting page</title>
           8    </head>
           9
          10    <body>
          11            <%
          12                Context initCtx = null;
          13                final String JNDINAME = "java:comp/env/jdbc/MySQL";//java:comp/env/是固定的,jdbc/MySQL則是我們配置的JNDI名稱
          14                Connection conn = null;
          15                try {
          16                    initCtx = new InitialContext();
          17                    DataSource ds = (DataSource) initCtx.lookup(JNDINAME);
          18                    conn = ds.getConnection();
          19                    out.println("數(shù)據(jù)連接為:" + conn);
          20                }
           catch (NamingException e) {
          21                    e.printStackTrace();
          22                }
           catch (SQLException e) {
          23                    e.printStackTrace();
          24                }
           finally {
          25                    if (conn != null{
          26                        try {
          27                            conn.close();
          28                        }
           catch (SQLException e) {
          29                            e.printStackTrace();
          30                        }

          31                    }

          32                }

          33            %>
          34        <br>
          35    </body>
          36</html>
          37
          4、訪問http://localhost:8080/工程名/index.jsp頁面,輸出如下:

          1數(shù)據(jù)連接為:jdbc:mysql://localhost:3306/test?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver 

          至此,大功告成!

          杰森 
          郵箱:json.shen(at)gmail.com
          網(wǎng)站:www.shenjia.org
          posted on 2008-05-11 20:07 杰森 閱讀(2021) 評(píng)論(12)  編輯  收藏 所屬分類: JavaEE

          評(píng)論:
          # re: Tomcat6.x 數(shù)據(jù)源配置 2008-05-12 19:41 | 銀河使者
          在tomcat6.x中有很多位置可以配置數(shù)據(jù)源。如在<tomcat安裝目錄>\conf\Catalina\localhost目錄中放一個(gè)和上下文路徑同名的xml文件,并使用如下的格式配置:

          <Context path="/samples" docBase="samples" debug="0">
          <Resource name="jdbc/jdbcdemo" auth="Container"
          type="javax.sql.DataSource"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/mydb?characterEncoding=GBK"
          username="root"
          password="1234"
          maxActive="200"
          maxIdle="50"
          maxWait="3000"/>

          </Context>

            回復(fù)  更多評(píng)論
            
          # re: Tomcat6.x 數(shù)據(jù)源配置 2008-05-12 23:10 | Jak.Shen
          @銀河使者

          多謝回復(fù)!同意你的說法,我寫的只是其中的一種!
            回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置 2008-11-12 23:22 | lizn
          我已經(jīng)按照設(shè)置全弄好了 運(yùn)行jsp 他說找不到 com.mysql.jdbc.Driver 我已經(jīng)放在lib里了 是那個(gè)jar包 我用DriverManager.getConnection()都可以連接數(shù)據(jù)庫的,就是數(shù)據(jù)源這個(gè)不行,能幫幫忙嗎  回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置 2008-11-12 23:24 | lizn
          噢 他說cannot load driverclass  回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置 2008-11-13 19:37 | Huaxu's
          @lizn
          按照上面的配置正常的話應(yīng)該是可以成功的!
          貼一下你的代碼吧,這樣我才好看。
            回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置 2008-11-15 18:53 | lizn
          @Huaxu's
          ok 謝謝了
          <Context>


          <WatchedResource>WEB-INF/web.xml</WatchedResource>
          <Resource name="jdbc/bookstore" auth="Container" type="javax.sql.DataSource"
          maxActive="100" maxIdle="30" maxWait="10000"
          username="root" password="123456" driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/bookstore?autoReconnect=true"/>

          </Context> //這是context文件

          //以下是index.jsp 轉(zhuǎn)換的時(shí)候從conn=ds.connection()拋出異常 說
          //can not load"com.mysql.jdbc.Driver"
          <%@ page contentType="text/html;charset=gb2312" import="java.sql.*,javax.sql.*,javax.naming.*" %>

          <html>
          <head>
          <title>留言板</title>
          </head>
          <body>
          <a href="say.html">我要留言</a>
          <%
          Context ctx=new InitialContext();
          DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
          Connection conn=ds.getConnection();

          Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
          ResultSet rs=stmt.executeQuery("select * from guestbook order by gst_time desc");
          rs.last();

          int rowCount=rs.getRow();
          if(rowCount==0){
          out.println("當(dāng)前沒有任何留言");
          return;
          }

          String strCurPage=request.getParameter("page");

          int curPage;

          if(strCurPage==null){
          curPage=1;
          }
          else
          curPage=Integer.parseInt(strCurPage);

          int countPerPage=5;

          int pageCount=(rowCount+countPerPage-1)/countPerPage;

          rs.absolute((curPage-1)*countPerPage+1);

          if(curPage==1){
          %>
          第一頁&nbsp;&nbsp;&nbsp;&nbsp;
          上一頁&nbsp;&nbsp;&nbsp;&nbsp;
          <%
          }
          else{
          %>
          <a href="index.jsp?page=<%=1%>">第一頁</a>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <a href="index.jsp?page=<%=curPage-1%>">上一頁</a>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <%
          }
          if(curPage==pageCount){
          %>
          下一頁&nbsp;&nbsp;&nbsp;&nbsp;
          最后頁&nbsp;&nbsp;&nbsp;&nbsp;
          <%
          }
          else{
          %>
          <a href="index.jsp?page=<%=curPage+1%>">下一頁</a>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <a href="index.jsp?page=<%=pageCount%>">最后頁</a>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <%

          }

          int i=0;

          while(i<countPerPage&&!rs.isAfterLast()){
          out.println("<hr color='blue' size='2'><br>");
          out.println("用戶名:"+rs.getString("gst_user"));
          out.println("&nbsp;&nbsp;");

          Timestamp ts=rs.getTimestamp("gst_time");
          long lms=ts.getTime();
          Date date=new Date(lms);
          Time time=new Time(lms);

          out.println("留言時(shí)間:"+date+" "+time);

          out.println("&nbsp;&nbsp;");
          out.println("用戶IP: "+rs.getString("gst_ip")+"<br>");
          out.println("主題:"+rs.getString("gst_title")+"<br>");
          out.println("內(nèi)容:"+rs.getString("gst_content"));
          i++;
          rs.next();
          }

          rs.close();
          stmt.close();
          conn.close();
          %>
          </body>
          </html>  回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置 2008-11-16 00:58 | Huaxu's

          @lizn


          測(cè)試了你的代碼,配置是沒有問題的。測(cè)試代碼如下(只是數(shù)據(jù)庫換成了test,密碼換成我的mysql):

          tomcat配置如下:


          1<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource" 

          2maxActive="100" maxIdle="30" maxWait="10000" 

          3username="root" password="mysql" driverClassName="com.mysql.jdbc.Driver" 

          4url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>



          jsp代碼如下:


           1<%@ page contentType="text/html;charset=gb2312"

           2 import="java.sql.*,javax.sql.*,javax.naming.*"
          %>

           3

           4<html>

           5 <head>

           6  <title>留言板</title>

           7 </head>

           8 <body>

           9  <href="#">我要留言</a>

          10  <%

          11   Context ctx = new InitialContext();

          12   DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/test");

          13   Connection conn = ds.getConnection();

          14   out.println(conn);

          15  
          %>

          16 </body>

          17</html>

          18

          輸出為:


          1org.apache.tomcat.dbcp.dbcp.PoolableConnection@160c4b0 



          我用的驅(qū)動(dòng)為:mysql-connector-java-5.0.8 下載地址:http://dev.mysql.com/downloads/connector/j/5.0.html

          Tomcat為:5.5.29版



          建議你換個(gè)驅(qū)動(dòng)試一下。


            回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置 2008-11-16 21:03 | lizn
          @Huaxu's
          試了一下 還是那個(gè)問題 我也是那個(gè)驅(qū)動(dòng) 估計(jì)tomcat6就不一樣 我換5.5吧 謝謝了  回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置 2008-11-17 23:37 | Huaxu's
          @lizn
          不是的。tomcat6是可以的。測(cè)試結(jié)果為:
          jdbc:mysql://localhost:3306/test?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver
          你找一下其它的原因。  回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置 2008-11-18 23:05 | lizn
          我也不清楚我這里為什么不行 我換了tomcat 5.5就ok了  回復(fù)  更多評(píng)論
            
          # Virusnyi Marketing 2009-05-18 12:25 | Virusnyi Marketing
          This blog is very informative and thanks for the updates and the blog colour and desiging is very beautiful after seeing this u must be a creative man.
          I am from Vietnam and learning to read in English, please tell me right I wrote the following sentence: "The article describes the effectiveness of seo analysis as one of the essentials.Expert search engine optimisation services by our seo works company consultants deliver firm page google organic results for companies across australia."

          Regards :o Gerda.  回復(fù)  更多評(píng)論
            
          # re: Tomcat6 數(shù)據(jù)源配置[未登錄] 2010-02-21 22:53 | zhou
          我的是莫名其妙的好了,剛開始以為是驅(qū)動(dòng)的問題,后來換了竟然通過了,可是后來又換回去還是通過了,無聊。只是還有個(gè)問題,myeclipse內(nèi)置的貓為什么不行,提示無法加載驅(qū)動(dòng)類?  回復(fù)  更多評(píng)論
            
          嗨117
          主站蜘蛛池模板: 洮南市| 自贡市| 山西省| 旌德县| 喜德县| 丰宁| 吴忠市| 博湖县| 乌鲁木齐县| 泸水县| 兖州市| 浏阳市| 阳泉市| 武定县| 江津市| 冀州市| 同仁县| 宜阳县| 庐江县| 谢通门县| 八宿县| 扎赉特旗| 尼木县| 涿鹿县| 蓬莱市| 开江县| 平泉县| 襄汾县| 阜南县| 舟山市| 治县。| 罗平县| 会理县| 洛川县| 赤水市| 惠安县| 三明市| 龙胜| 宣汉县| 亳州市| 长子县|