Spring JDBC 无法连接到 postgres 数据库,但普通 JDBC 可以连接
Spring JDBC can not connect to postgres database , but plain JDBC is able to connect
我正在尝试使用 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>
这是我通过代码访问的方式:
首先,我正在创建一个 class 来实现 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 的 class:
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: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
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: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:207)
端口有错字?
<property name="url" value="jdbc:postgresql://localhost:5342/iitkgpdbnew" />
默认的 PostgreSQL 端口是 5432。当然除非你把它改成 5342:)
我正在尝试使用 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>
这是我通过代码访问的方式:
首先,我正在创建一个 class 来实现 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 的 class:
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: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. 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: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:207)
端口有错字?
<property name="url" value="jdbc:postgresql://localhost:5342/iitkgpdbnew" />
默认的 PostgreSQL 端口是 5432。当然除非你把它改成 5342:)