在localhost上连接Oracle时出错:无法获取JDBC连接?

时间:2014-09-27 15:49:14

标签: java oracle jdbc jdbctemplate

我正在尝试使用Eclipse从我的Web应用程序连接Oracle数据库。 但是我得到了错误:org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接;

我使用下面的代码:

public static void initDB(){
    try {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        ds.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
        ds.setUsername("mybank");
        ds.setPassword("123456");

        jdbcTemplate  = new JdbcTemplate(ds); // throw exceptiong after this line
        isConnected = true;
        initSystemPros();
    } catch (Exception e) {
        System.err.println("Cannot connect to DataBase");
        isConnected = false;
    }
}

例外:

INFO: Server startup in 11564 ms 
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Io exception: The Network Adapter could not establish the connection) at 
org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtil‌​s.java:80) at
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382) at 
org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)

我尝试使用SQL Developer使用上述设置创建连接并成功。 你知道什么是错的吗?

谢谢和问候, 泰

1 个答案:

答案 0 :(得分:0)

确保xeService Name

如果连接成功,但您有此例外SQLNestedException: Cannot create PoolableConnectionFactory (Io exception: The Network Adapter could not establish the connection) at "

您必须拥有不兼容的JDBC版本,请安装正确的版本: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html