tomcat5.5 mysql接続プール
2411 ワード
1.serverを変更します.xml
2.webを修正する.xml
3.jsp表示
<Context docBase="Bean" path="/Bean" privileged="true">
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="2"
maxWait="5000"
username="root"
password="lingting"
url="jdbc:mysql://localhost:3306/test"
maxActive="4" />
</Context>
2.webを修正する.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
3.jsp表示
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.sql.*,javax.sql.DataSource,javax.naming.*"%>
<html>
<head><title>test.jsp</title></head>
<body bgcolor="#ffffff">
<h1>test Tomcat</h1>
<%
try
{
Context initCtx=new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/mysql");
Connection conn=ds.getConnection();
out.println("data from database:<br>");
Statement stmt=conn.createStatement();
ResultSet rs =stmt.executeQuery("select * from cheng");
while(rs.next())
{
out.println(rs.getInt("id"));
out.println(rs.getString("name"));
out.println(rs.getString("msg"));
}
rs.close();
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>