java jsp tomcat6 MySQL 連接池的正確配置
以下的文章主要講述的是java jsp tomcat6 MySQL 連接池的正確配置,有很多人都是用tomcat5 對(duì)java jsp tomcat6 MySQL 連接池進(jìn)行配置,但是我個(gè)人認(rèn)為用tomcat5 太費(fèi)時(shí)間也很麻煩,現(xiàn)在分享如下:
1.需要的文件:MySQL-5.0.27-win32.zip(安裝文件),MySQL-connector-java-5.0.4-bin.jar(連接驅(qū)動(dòng)程序),apache-tomcat-6.0.10.exe(安裝文件)
2.配置tomcat下的conf下的context.xml文件,在<context></context>之間添加java jsp tomcat6 MySQL 連接池如下:
- <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" />
上面的參數(shù)不用我說了吧,這些都知道是什么意思吧.
3.配置你的應(yīng)用下的web.xml中的<web-app></web-app>之間加入:
- xml 代碼<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里面配置了,下面就可以編寫測試程序了,這個(gè)網(wǎng)上就很多了,主要的就上面,當(dāng)然要把連接驅(qū)動(dòng)程序都放到tomcat6下的lib下面.測試代碼如下:
- java 代碼<!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必須是數(shù)據(jù)庫已有的表,
這里的數(shù)據(jù)庫前文提及的Data Source URL配置里包含的數(shù)據(jù)庫。
- String strSql = " select * from users";
- ResultSet rs = stmt.executeQuery(strSql);
- while(rs.next()){
- out.print(rs.getString(1));
- }
- out.print("我的測試結(jié)束");
- }
- catch(Exception ex){
- out.print(“出現(xiàn)例外,信息是:”+ex.getMessage());
- ex.printStackTrace();
- }
- %>
- </head>
- <body>
- </body>
- </html>
上面的保證能行,我已經(jīng)測試過了.如有問題可以給我留言.以上的相關(guān)內(nèi)容就是對(duì)java jsp tomcat6 MySQL 連接池配置的介紹,望你能有所收獲。
【編輯推薦】
- 讓MySQL支持中文的實(shí)際操作步驟
- 配置MySQL與卸載MySQL實(shí)操
- MySQL數(shù)據(jù)庫訪問妙招在Linux之下
- 圖解MySQL數(shù)據(jù)庫安裝與實(shí)際操作
- MySQL數(shù)據(jù)庫進(jìn)行備份在Linux異構(gòu)網(wǎng)絡(luò)里