tomcat中資料來源的配置

來源:互聯網
上載者:User
Tomcat資料來源串連池的配置
@@@@@@首先在lib目錄下安裝驅動
一、首先利用tomcat的web管理配置資料來源,有以下幾項
1 Jndi name :資料來源串連池命名
2 Data Source URL: jdbc:mysql://localhost/test mysql是資料庫類型,test是資料庫名字 localhost是表示用本地的資料庫
3 JDBC Driver Class: 資料庫驅動類 mysql使用org.gjt.mm.mysql.Driver
4 User name 是資料庫的使用者名稱 5 Password是資料庫的密碼
6 Max.Active Connections: 最大的活動串連數
6 Max.ldle Connections:最大的空閑串連數
7 Max.Wait for Connection: 最大的等待串連數
以上配置完畢點擊Save--Commit Changes--Log out
二、在tomcat安裝目錄下/conf/web.xml中添加如下資訊
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name> //要使用的資料來源的名字
<res-type>javax.sql.DataSource</res-type> //資料來源的類型
<res-auth>Container</res-auth> //和一中的一致
</resource-ref>
三、在tomcat安裝目錄下/conf/catalina/localhost目錄下找到當前項目的配置資訊(項目名.xml),添加
<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSource"/>
以上完成了串連池的配置

第二種就是
1 把第一種步驟1裡產生的代碼放入tomcat安裝目錄下/conf/catalina/localhost/項目名.xml中
產生代碼如下:
<Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
<Resource name="jdbc/mysql" type="javax.sql.DataSource"/>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/test</value>
</parameter>
<parameter>
<name>password</name>
<value></value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
</ResourceParams>
2 在tomcat安裝目錄下/conf/web.xml中添加如下資訊
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name> //要使用的資料來源的名字
<res-type>javax.sql.DataSource</res-type> //資料來源的類型
<res-auth>Container</res-auth>
</resource-ref>
兩者的區別是範圍不同
資料庫連接測試:
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
中文顯示問題:
public static String toChinese(String strvalue) {
try{
if(strvalue==null)
{
return null;
}
else {
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}catch(Exception e){
return null;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.