Tomcat6.0连接池配置
配置tomcat下的conf下的context.xml文件,在之间添加连接池配置:
<Resource name="jdbc/sqlserver"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url=" jdbc:sqlserver://localhost:1433;DatabaseName=value" //DatabaseName= 具体的数据库名称
username=" user "//用户名
password="password" /密码
maxActive="100"
maxIdle="30"
maxWait="10000"
/>
配置你的应用下的web.xml中的之间加入:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/sqlserver</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
编写一测试程序MyJsp.jsp
<%
@page
import
=
"
java.sql.*
"
%>
<% @page import = " javax.sql.* " %>
<% @page import = " javax.naming.* " %>
< div align = ' center ' >
<%
DataSource ds = null ;
Connection conn = null ;
Statement stmt = null ;
ResultSet rs = null ;
try
... {
InitialContext ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/sqlserver");
conn = ds.getConnection();
}
catch (Exception e)
... {
e.printStackTrace();
}
%>
<% if ( null != conn)
... {
System.out.println("now have in if function");
try
...{
stmt = conn.createStatement();
rs =stmt.executeQuery("select * from article");
System.out.println("now hava in try function");
}
catch(SQLException e)
...{
e.printStackTrace();
}
}
%>
<% @page import = " javax.sql.* " %>
<% @page import = " javax.naming.* " %>
< div align = ' center ' >
<%
DataSource ds = null ;
Connection conn = null ;
Statement stmt = null ;
ResultSet rs = null ;
try
... {
InitialContext ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/sqlserver");
conn = ds.getConnection();
}
catch (Exception e)
... {
e.printStackTrace();
}
%>
<% if ( null != conn)
... {
System.out.println("now have in if function");
try
...{
stmt = conn.createStatement();
rs =stmt.executeQuery("select * from article");
System.out.println("now hava in try function");
}
catch(SQLException e)
...{
e.printStackTrace();
}
}
%>