<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chinese To English     英文 轉 中文             
             
    隨筆-27  評論-53  文章-0  trackbacks-0

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

    1、將MySQL的jdbc驅動包考貝到%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新建一些個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("數據連接為:" + 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數據連接為:jdbc:mysql://localhost:3306/test?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver 

    至此,大功告成!

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

    評論:
    # re: Tomcat6.x 數據源配置 2008-05-12 19:41 | 銀河使者
    在tomcat6.x中有很多位置可以配置數據源。如在<tomcat安裝目錄>\conf\Catalina\localhost目錄中放一個和上下文路徑同名的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>

      回復  更多評論
      
    # re: Tomcat6.x 數據源配置 2008-05-12 23:10 | Jak.Shen
    @銀河使者

    多謝回復!同意你的說法,我寫的只是其中的一種!
      回復  更多評論
      
    # re: Tomcat6 數據源配置 2008-11-12 23:22 | lizn
    我已經按照設置全弄好了 運行jsp 他說找不到 com.mysql.jdbc.Driver 我已經放在lib里了 是那個jar包 我用DriverManager.getConnection()都可以連接數據庫的,就是數據源這個不行,能幫幫忙嗎  回復  更多評論
      
    # re: Tomcat6 數據源配置 2008-11-12 23:24 | lizn
    噢 他說cannot load driverclass  回復  更多評論
      
    # re: Tomcat6 數據源配置 2008-11-13 19:37 | Huaxu's
    @lizn
    按照上面的配置正常的話應該是可以成功的!
    貼一下你的代碼吧,這樣我才好看。
      回復  更多評論
      
    # re: Tomcat6 數據源配置 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 轉換的時候從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("當前沒有任何留言");
    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("留言時間:"+date+" "+time);

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

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

    @lizn


    測試了你的代碼,配置是沒有問題的。測試代碼如下(只是數據庫換成了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 



    我用的驅動為:mysql-connector-java-5.0.8 下載地址:http://dev.mysql.com/downloads/connector/j/5.0.html

    Tomcat為:5.5.29版



    建議你換個驅動試一下。


      回復  更多評論
      
    # re: Tomcat6 數據源配置 2008-11-16 21:03 | lizn
    @Huaxu's
    試了一下 還是那個問題 我也是那個驅動 估計tomcat6就不一樣 我換5.5吧 謝謝了  回復  更多評論
      
    # re: Tomcat6 數據源配置 2008-11-17 23:37 | Huaxu's
    @lizn
    不是的。tomcat6是可以的。測試結果為:
    jdbc:mysql://localhost:3306/test?autoReconnect=true, UserName=root@localhost, MySQL-AB JDBC Driver
    你找一下其它的原因。  回復  更多評論
      
    # re: Tomcat6 數據源配置 2008-11-18 23:05 | lizn
    我也不清楚我這里為什么不行 我換了tomcat 5.5就ok了  回復  更多評論
      
    # 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.  回復  更多評論
      
    # re: Tomcat6 數據源配置[未登錄] 2010-02-21 22:53 | zhou
    我的是莫名其妙的好了,剛開始以為是驅動的問題,后來換了竟然通過了,可是后來又換回去還是通過了,無聊。只是還有個問題,myeclipse內置的貓為什么不行,提示無法加載驅動類?  回復  更多評論
      
    嗨117
    主站蜘蛛池模板: 中文字幕在线免费观看视频| 足恋玩丝袜脚视频免费网站| 亚洲国产精品无码专区影院 | 久久福利资源网站免费看| 亚洲人成色77777在线观看| 亚洲日韩国产一区二区三区| 日韩内射激情视频在线播放免费| 亚洲深深色噜噜狠狠网站| 亚洲真人日本在线| 114一级毛片免费| 深夜免费在线视频| 亚洲福利一区二区| 亚洲国产中文字幕在线观看| 18以下岁毛片在免费播放| 美女啪啪网站又黄又免费| 亚洲福利一区二区三区| 亚洲午夜AV无码专区在线播放| 97人妻无码一区二区精品免费| 人妻仑乱A级毛片免费看| 中文文字幕文字幕亚洲色| 亚洲老妈激情一区二区三区| 妞干网手机免费视频| 成人黄网站片免费视频| 激情小说亚洲图片| 亚洲白嫩在线观看| 亚洲人成网7777777国产| 在线观看免费为成年视频| 91精品国产免费久久国语蜜臀| 农村寡妇一级毛片免费看视频| 亚洲一级毛片视频| 亚洲AV无码成人精品区天堂| 亚洲成人一区二区| 巨胸喷奶水视频www网免费| 久久午夜伦鲁片免费无码| 一级一级一级毛片免费毛片| 一区二区亚洲精品精华液| 亚洲第一福利视频| 亚洲日韩精品一区二区三区无码| 国产精品免费播放| 成人免费视频软件网站| 国产精品视频免费观看|