近日學(xué)習(xí)jsp時,為連接池的問題所困,經(jīng)過一番努力,終于成功了,特此為大家獻(xiàn)上。
1.Tomcat5.5.12中沒有admin模塊,需要讀者自行下載
2.Tomcat中配置如下
JNDI Name: jdbc/mysql
Data Source URL: jdbc:mysql://202.118.133.88/xscj
JDBC Driver Class: org.gjt.mm.mysql.Driver
User Name: root
Password: ********
Max. Active Connections: 4
Max. Idle Connections: 2
Max. Wait for Connection: 500
Validation Query:
注:
1.jdbc/mysql 前綱的jdbc也可以換成其它的, mysql為連池名,可任意起,在下文中注意使用
2.jdbc:mysql://192.168.0.16/xscj xscj為對應(yīng)的數(shù)據(jù)庫名
也可以換成jdbc:mysql://localhost/xscj
3.JDBC Driver Class : com.mysql.jdbc.Driver jdk中自帶
4.User Name:為Mysql中數(shù)據(jù)庫管理員名
3. 下面是最關(guān)鍵的一點
D:\Tomcat 5.5\conf\context.xml
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource
name="mysql/xscj"
type="javax.sql.DataSource"
password="kingsoft88"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://202.118.133.88:3306/xscj"
maxActive="4"/>
</Context>
應(yīng)相應(yīng)的字段真加對了就可以了。
4.在Eclipse中編譯時加入Tomcat 的DBCP和Pool包就不會有問題了.
測試程序如下:
<html>
<head>
<title></title>
<%
out.print("開始測試:"+"<br/>");
DataSource ds = null;
Connection con=null;
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
//這里的數(shù)據(jù)庫前文提及的Data Source URL配置里包含的數(shù)據(jù)庫。
ds = (DataSource)ctx.lookup("jdbc/xscj");
con=ds.getConnection();
Statement stmt = con.createStatement();
String strSql = "select * from xs"; //表中的字段讀者自行添加
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(1)+"<br/>");
}
rs.close();
stmt.close();
con.close();
out.print("我的測試結(jié)束");
}
catch(Exception ex){
out.print("出現(xiàn)例外,信息是:”+ ex.getMessage());
ex.printStackTrace();
}
%>
</head>
<body>
</body>
</html>
總結(jié):以上步驟均十分關(guān)鍵,如果有誤對應(yīng)錯誤如下
1、第一步錯誤,報錯
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
2、第三步錯誤,報錯
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
3、第四步錯誤,報錯
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket
如果有上述錯誤,請檢查對應(yīng)步驟是否正確實施
posted on 2006-02-27 19:44
JavaLife 閱讀(536)
評論(1) 編輯 收藏 所屬分類:
Eclipse 、
Tomcat