我正在尝试连接到oracle数据库并将结果映射到POJO。但是我收到了错误
java.lang.IllegalStateException: Connection factory returned null from createConnection
我有其他春季项目,从未遇到过这个问题所以不知道如何解决它。我在下面包含了我的配置和课程。
Servlet xml代码段
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${mnp.driverClassName}" />
<property name="url" value="${mnp.url}" />
<property name="username" value="${mnp.username}" />
<property name="password" value="${mnp.password}" />
</bean>
DAO实施
package cdjh.ops.mnpui.daos;
import java.util.ArrayList;
import javax.annotation.Resource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import cdjh.ops.mnpui.pojos.PacEntry;
import cdjh.ops.mnpui.pojos.PortEntry;
import cdjh.ops.mnpui.pojos.PortRecord;
@Repository
public class QueryDAO extends JdbcTemplate implements IQueryDAO{
@Resource BasicDataSource dataSource;
@Override
public PortRecord findPortRecord(String msisdn, String pac){
String sql = "select * from tbl_entry where msisdn = ? and pac_code = ?";
PortRecord portRecord = queryForObject(sql, new Object[]{msisdn, pac}, new BeanPropertyRowMapper<PortRecord>(PortRecord.class));
return portRecord;
}
public void setDataSource(BasicDataSource dataSource) {
this.dataSource = dataSource;
}
public BasicDataSource getDataSource() {
return dataSource;
}
}
栈跟踪
java.lang.IllegalStateException: Connection factory returned null from createConnection
org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:584)
org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666)
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674)
org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:729)
cdjh.ops.mnpui.daos.QueryDAO.findPortRecord(QueryDAO.java:41)
cdjh.ops.mnpui.services.QueryService.findPortRecord(QueryService.java:33)
cdjh.ops.mnpui.controllers.QueryController.showPortRecord(QueryController.java:58)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:439)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:427)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
更新 我已经对我构建的mysql db进行了测试,它运行正常,但似乎并不适用于oracle。唯一改变的是jdbc.properties文件。
MySQL的
mnp.driverClassName=com.mysql.jdbc.Driver
mnp.url=jdbc:mysql://db4free.net:3306/mnpui
mnp.username=XXX
mnp.password=XXX
的Oracle
mnp.driverClassName=oracle.jdbc.OracleDriver
mnp.url=jdbc:oracle:thin:mnpui@172.31.125.37:1523
mnp.username=XXX
mnp.password=XXX
我是否需要与oracle做一些不同的事情才能让它发挥作用?
答案 0 :(得分:1)
(将原始评论作为答案重新发布,因为它似乎解决了问题)。
您用于连接Oracle的classes12.zip
JDBC驱动程序非常陈旧,适用于Java 1.2客户端。 Spring或Commons DBCP可能依赖于更现代的JDBC版本,因此您应该使用更现代的odbc6.jar
或ojdbc7.jar
(分别用于Java 6和7,尽管这种关联有点松散而且它们在很大程度上是可以互换的。相反。
答案 1 :(得分:0)
JdbcTemplate
封装在你的dao中JdbcTemplate
的数据源?DataSource
而不是BasicDataSource
进行编程。
package cdjh.ops.mnpui.daos;
import java.util.ArrayList;
import javax.annotation.Resource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import cdjh.ops.mnpui.pojos.PacEntry;
import cdjh.ops.mnpui.pojos.PortEntry;
import cdjh.ops.mnpui.pojos.PortRecord;
@Repository
public class QueryDAO implements IQueryDAO{
private final JdbcTemplate jdbcTemplate;
@Autowired
public QueryDao(DataSource dataSource) {
this.jdbcTemplate=new JdbcTemplate(dataSource);
}
@Override
public PortRecord findPortRecord(String msisdn, String pac){
String sql = "select * from tbl_entry where msisdn = ? and pac_code = ?";
PortRecord portRecord = this.jdbcTemplate.queryForObject(sql, new Object[]{msisdn, pac}, new BeanPropertyRowMapper(PortRecord.class));
return portRecord;
}
}
还要确保您实际使用的是弹簧配置中配置的数据源。您的帖子中的信息太少,而您的初始信息也很难确定。