81,094
社区成员
发帖
与我相关
我的任务
分享
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
password="root"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="10000"
validationQuery="SELECT '1'"
username="root"
url="jdbc:mysql://localhost:3306/dbname?autoReconnect=true"
maxActive="200"/>
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:/Andy/workspace/appName" reloadable="true">
<Resource
name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
password="root"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="20"
maxWait="10000"
validationQuery="SELECT 1"
username="root"
url="jdbc:mysql://localhost:3306/dbname?autoReconnect=true"
maxActive="200"/>
<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSource" />
</Context>
<Context path="/appName" docBase="appName"
debug="5" crossContext="true" reloadable="false"
cachingAllowed="true" cacheMaxSize="20480"
cacheTTL="10000">
<Resource name="jdbc/mysql" auth="Container" removeAbandoned="true"
removeAbandonedTimeout="60" logAbandoned="true"
type="javax.sql.DataSource" maxActive="50" maxIdle="10"
maxWait="10000" username="root" password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbname?autoReconnect=true" />
</Context>
<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>
<%@page language="java" import="java.util.*,java.sql.*,javax.naming.*,javax.sql.*" pageEncoding="GB2312"%>
<%@page import="com.mchange.v2.c3p0.*"%>
<%@page import="java.sql.Connection"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'mysqlTest.jsp' starting page</title>
</head>
<body>
Mysql数据库测试<br><br><br>
<%
Connection conn=null;
try
{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
conn=ds.getConnection();
}
catch(NamingException ex)
{
ex.printStackTrace();
}
String sql="select * from jforum_posts";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
%>
字段1:<%=rs.getString(1)%> 字段2:<%=rs.getString(2)%><br>
<%
}
if(rs!=null)
{
rs.close();
rs=null;
}
if(ps!=null)
{
ps.close();
ps=null;
}
if(conn!=null)
{
conn.close();
conn=null;
}
%>
</body>
</html>