请问jdbc数据库连接池要怎样配置以及代码编写,希望大家详细解答,多谢先
环境:postgresql8.0+Tomcat 5.5
环境:postgresql8.0+Tomcat 5.5
解决方案 »
- session过期的原因
- struts2遇到了一个难以解决的问题
- java 取sqlserver的text等大字段值,怎么取?
- js的变量付给给当前jsp页面,有办法吗?
- application生存期问题
- 做JSP练习的时候,迅雷总是自动出现下载框,浏览器就打不开JSP页面了
- struts中validator的奇怪的问题
- html转为pdf用itext,在jsp页面点击按钮保存pdf文件,怎么写?
- MSSQL2000打过SP4补丁还是提示空指针错误 跪求各位帮忙 顶的也有分
- 一个简单的STRUTS问题,但是我不会。
- 在JSP里 我做了模糊查询后,想对模糊查询所的的结果进行分页显示,如何做`?
- JSP页面在没有提交的时候怎么验证用户名是否已经存在?
context.xml放到main-info文件夹下面就可以了
<Resource
auth="Container"
name="jdbc/postgresql"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
password="<PASSWORD>"
maxIdle="5"
maxWait="5000"
username="<USERNAME>"
url="jdbc:postgresql://localhost/<DATABASE_NAME>"
maxActive="5"/>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>在web.xml中引用一下:
<resource-ref>
<description>PostgreSQL Database Source</description>
<res-ref-name>jdbc/postgresql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>再写个类ConnectionFactory用于获得Connection,在使用时直接用:
Connection con = ConnectionFactory.getConnection();
就可以了。下面的代码可以参考一下import java.sql.Connection;
import java.sql.SQLException;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;public class ConnectionFactory {
static {
load();
} public static Connection getConnection() {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e) {
LOG.error("xxxx");
}
return con;
}
private static void load() {
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/postgresql");
} catch (NamingException e) {
LOG.error("xxxx");
}
} private ConnectionFactory(){
} private static DataSource ds = null;
private static Log LOG = LogFactory.getLog(ConnectionFactory.class);
}
有事请教!