Spring JDBC无法连接到PostgreSQL数据库,但普通的JDBC可以连接。

3

我正在尝试使用PostgreSQL设置数据库连接。我以前使用原生的JDBC成功地连接到数据库。

但是,当我使用JdbcTemplate提供相同的连接参数时,无法连接。

请查看我的代码和配置:

<bean  name="dataSource" id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5342/testdbnew" /> 
    <property name="username" value="admin1" />
    <property name="password" value="admin1" />
</bean>    

<bean  id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean> 

<bean name="announcementNewsDAO" id="announcementNewsDAO" class="test.dao.AnnouncementNewsDAOImpl">
</bean>

以下是我通过代码访问的方式:

首先,我创建了一个类来实现ApplicationContextAware。然后,通过该引用调用jdbcTemplate对象。

public class ApplicationContextProvider implements ApplicationContextAware {

private static ApplicationContext context;

 public static ApplicationContext getApplicationContext() {
    return context;
}

@Override
public void setApplicationContext(ApplicationContext ac)
        throws BeansException {
    context = ac;
    System.out.println("Context initialized...");
    JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
    System.out.println("jdbcTemplate initialized..");

}

}

这是 AnnouncementDAOImpl 类的实现:

public class AnnouncementNewsDAOImpl implements AnnouncementNewsDAO {


/**
 * JDBCTemplate object for accessing database
 */
@Autowired
private  JdbcTemplate jdbcTemplate ;


@Override
public void insertAnnouncementNews(AnnouncementNews news) {

    String insertQuery = "<db query for insert>";
    try {

        Object[] args = new Object[]{Integer.valueOf(news.getSlno()), news.getStakeholder_code(), news.getInfo_type(), news.getAnnouncement_news(),null,null};


        int result = jdbcTemplate.update(insertQuery, args);

        if(result!=0){
            System.out.println("Announcement news inserted for the serial number : "+news.getSlno());
        }else{
            System.err.println("Could not insert announcement news for the serial number : "+news.getSlno());
        }


    } catch (ParseException pe) {
        System.err.println("Exception occurred while parsing date : "+pe.getMessage());
    }
}

我遇到了以下错误:

org.springframework.jdbc.CannotGetJdbcConnectionException: 无法获取JDBC连接;嵌套异常为org.postgresql.util.PSQLException:连接被拒绝。请检查主机名和端口是否正确,并且postmaster是否接受TCP/IP连接。 at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:628) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:907) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:968) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:978) at Caused by: org.postgresql.util.PSQLException: 连接被拒绝。请检查主机名和端口是否正确,并且postmaster是否接受TCP/IP连接。 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:207)


1
您的连接已关闭,服务器拒绝了连接,请检查您的防火墙设置。 - Roman C
如果是Windows系统,禁用防火墙。同时希望配置细节正确无误。 - We are Borg
1
这个问题可能应该被关闭,因为它只是一个简单的拼写错误引起的。 - Enzo Mac
1个回答

3
端口号错了吗?
<property name="url" value="jdbc:postgresql://localhost:5342/iitkgpdbnew" /> 

默认的PostgreSQL端口是5432。当然,除非你将其更改为5342 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接