?
?
?? 最近在網上看到的 java+jsp+ tomcat6+ mysql 連接池大多數是tomcat5 的,很多都說得不詳細,并且配置不起,讓我們很費時間,也很腦火,今天我終于把最新的tomcat6+mysql的連接池配置成功了,現在分享如下:
? 1.需要的文件: mysql-5.0.27-win32.zip(安裝文件), mysql-connector-java-5.0.4-bin.jar(連接驅動程序), apache-tomcat-6.0.10.exe (安裝文件)
? 2.配置tomcat下的conf下的 context.xml文件,在<context></context>之間添加連接池如下:
- < Resource ? name = "jdbc/mysql" ??
- ??????? auth = "Container" ??
- ??????? ??
- ??????? type = "javax.sql.DataSource" ??
- ??????? driverClassName = "com.mysql.jdbc.Driver" ??
- ??????? url = "jdbc:mysql://localhost/test" ??
- ??????? username = "root" ??
- ??????? password = "root" ??
- ??????? maxActive = "100" ??
- ??????? maxIdle = "30" ??
- ??????? maxWait = "10000" ? /> ??
?????????????????? 上面的參數不用我說了吧,這些都知道是什么意思吧.
3.配置你的應用下的web.xml中的 <web-app> </web-app>之間 加入:
- < resource-ref > ??
- ???? < description > DB?Connection </ description > ??
- ???? < res-ref-name > jdbc/mysqlx </ res-ref-name > ??
- ???? < res-type > javax.sql.DataSource </ res-type > ??
- ???? < res-auth > Container </ res-auth > ??
- ?? </ resource-ref > ??
4.大功告成,不用在原來的server.xml里面配置了,下面就可以編寫測試程序了,這個網上就很多了,主要的就上面,當然要把連接驅動程序都放到tomcat6下的lib下面.測試代碼如下:
- <!doctype?html? public ? "-//w3c//dtd?html?4.0?transitional//en" ? ??
- ??
- "http://www.w3.org/TR/REC-html40/strict.dtd" > ??
- ??
- <%@?page? import = "java.sql.*" %> ??
- ??
- <%@?page? import = "javax.sql.*" %> ??
- ??
- <%@?page? import = "javax.naming.*" %> ??
- ??
- <%@?page?session= "false" ?%> ??
- ??
- <html> ??
- ??
- <head> ??
- ??
- <meta?http-equiv= "Content-Type" ?content= "text/html;?charset=gb2312" > ??
- ??
- <title></title> ??
- ??
- <%? ??
- ??
- ???out.print( "我的測試開始" ); ??
- ??
- ???DataSource?ds?=? null ; ??
- ??
- ??? try { ??
- ??
- ???InitialContext?ctx= new ?InitialContext(); ??
- ??
- ???ds=(DataSource)ctx.lookup( "java:comp/env/jdbc/mysql" ); ??
- ??
- ???Connection?conn?=?ds.getConnection(); ??
- ??
- ???Statement?stmt?=?conn.createStatement(); ??
- ??
- ????? //提示:users必須是數據庫已有的表, ??
- ??
- //這里的數據庫前文提及的Data?Source?URL配置里包含的數據庫。 ??
- ??
- ???String?strSql?=? "?select?*?from?users" ; ??
- ??
- ???ResultSet?rs?=?stmt.executeQuery(strSql); ??
- ??
- ??? while (rs.next()){ ??
- ??
- ??????out.print(rs.getString( 1 ));????????????????? ??
- ??
- ?????} ??
- ??
- out.print( "我的測試結束" ); ??
- ??
- ???} ??
- ??
- ??? catch (Exception?ex){ ??
- ??
- ???????out.print(“出現例外,信息是:”+ex.getMessage()); ??
- ??
- ????ex.printStackTrace(); ??
- ??
- ???} ??
- ??
- %> ??
- ??
- </head> ??
- ??
- <body> ??
- ??
- </body> ??
- ??
- </html> ??
- ??
上面的保證能行,已經測試過了.
轉自:http://tophump.javaeye.com/blog/60375
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
