1.Configure conf/context.xml in root directory of Tomcat 5.5
配置Tomcat根目录下conf/context.xml文件
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="accp" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=restrant"/>
2.Configure web.xml
配置web.xml
<web-app>
<resource-ref>
<description></description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
3.Make a mothod to get resultset
编写使用连接池获得结果集的方法
public ResultSet getRsByPool(String sql){
try {
Context ic=new InitialContext();
DataSource source=(DataSource)ic.lookup("java:comp/env/jdbc/mydb");
con=source.getConnection();
psta=con.prepareStatement(sql);
rs=psta.executeQuery();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
配置Tomcat根目录下conf/context.xml文件
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="accp" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=restrant"/>
2.Configure web.xml
配置web.xml
<web-app>
<resource-ref>
<description></description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
3.Make a mothod to get resultset
编写使用连接池获得结果集的方法
public ResultSet getRsByPool(String sql){
try {
Context ic=new InitialContext();
DataSource source=(DataSource)ic.lookup("java:comp/env/jdbc/mydb");
con=source.getConnection();
psta=con.prepareStatement(sql);
rs=psta.executeQuery();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}