java jsp tomcat6 MySQL 連接池的正確配置
以下的文章主要講述的是java jsp tomcat6 MySQL 連接池的正確配置,有很多人都是用tomcat5 對java jsp tomcat6 MySQL 連接池進行配置,但是我個人認為用tomcat5 太費時間也很麻煩,現在分享如下:
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>之間添加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" />
 
上面的參數不用我說了吧,這些都知道是什么意思吧.
3.配置你的應用下的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里面配置了,下面就可以編寫測試程序了,這個網上就很多了,主要的就上面,當然要把連接驅動程序都放到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必須是數據庫已有的表, 
這里的數據庫前文提及的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>
 
上面的保證能行,我已經測試過了.如有問題可以給我留言.以上的相關內容就是對java jsp tomcat6 MySQL 連接池配置的介紹,望你能有所收獲。
【編輯推薦】
 















 
 
 
 
 
 
 