tomcat4.1.24+Mysql连接池配法 find99

阅读更多
1.修改CATALINA_HOME\conf\server.xml






prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>

auth="Container"
type="javax.sql.DataSource"/>



factory
org.apache.commons.dbcp.BasicDataSourceFactory




maxActive
100




maxIdle
30




maxWait
10000




username
sa


password
111111




driverClassName
org.gjt.mm.mysql.Driver




url
jdbc:mysql://localhost:3306/test?useUnicode=true &characterEnco ding=GBK











2.修改webapps/ROOT/INF-WEB/web.xml


"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

MySQL Test App

DB Connection
jdbc/MysqlDB
javax.sql.DataSource
Container



3.测试用的testdb.jsp内容:

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import= "java.sql.* " %>
<%@ page import= "javax.naming.* "%>
<%
try{

Context initCtx = new InitialContext();

Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象

Object obj = (Object) ctx.lookup("jdbc/MysqlDB");

//类型转换

javax.sql.DataSource ds = (javax.sql.DataSource)obj;

Connection conn = ds.getConnection();

Statement stmt = conn.createStatement();

String strSql = " insert into test(id,name) values('007','hello,中国!') ";

stmt.executeUpdate(strSql);

strSql = " select id,name from test ";

ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){

out.println(rs.getString(1));

out.println(rs.getString(2)+"
");

}
conn.close();

}catch(Exception ex){

ex.printStackTrace();
throw new SQLException("cannot get Connection pool.");
}


%>





你可能感兴趣的:(MySQL,JDBC,SQL,Web,SQL,Server)