gpt4 book ai didi

postgresql - Apache Tomcat 连接到 PostgreSQL 数据库(通过 JDBC 驱动程序)

转载 作者:行者123 更新时间:2023-11-28 23:22:51 24 4
gpt4 key购买 nike

我已经安装了 PostgreSQL 9.5.2 (rhel 6.7 x64) 和 Apache Tomcat 7.0.73(x64 rhel 6.7) 之后我下载了 postgresql-9.4.1212.jar jar 文件并粘贴到 Catalina_home/Lib 路径并添加这个跟随content.xml文件

<Resource auth="Container" name="jdbc/postgres" type="javax.sql.DataSource" user="sarkhan" password="1"
driverClassName="org.postgresql.Driver" url="jdbc:postgresql://172.29.92.162:5432" maxActive="150"
schema="test" maxIdle="4"/>

将以下内容添加到 web.xml 文件

<resource-ref>
<description>postgreSQL Datasource</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

那么我如何测试 Apache Tomcat 服务器是否已连接到 PostgreSQL 数据库服务器?

最佳答案

Tomcat 给出了JNDI Datasource HOW-TO 的引导,具体来说:

  1. Accessing the datasource

    When accessing the datasource programmatically, remember to prepend java:/comp/env to your JNDI lookup, as in the following snippet of code. Note also that "jdbc/postgres" can be replaced with any value you prefer, provided you change it in the above resource definition file as well.

InitialContext cxt = new InitialContext();
if ( cxt == null ) {
throw new Exception("Uh oh -- no context!");
}

DataSource ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/postgres" );

if ( ds == null ) {
throw new Exception("Data source not found!");
}

关于postgresql - Apache Tomcat 连接到 PostgreSQL 数据库(通过 JDBC 驱动程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40997191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com