2014-09-27 98 views
0

我正尝试使用Eclipse从我的Web应用程序连接Oracle数据库。 但我得到了错误:org.springframework.jdbc.CannotGetJdbcConnectionException:无法获得JDBC连接;在localhost上连接Oracle时出错:无法获得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来创建使用上面的设置和成功的连接。 你知道什么是错的吗?

感谢和问候, 大

+0

那为什么你有2行代码*之后*说的行:*抛出此行后的异常*?hmm,尝试交换行顺序?'isConnected = true; initSystemPros(); jdbcTemplate = new JdbcTemplate(ds);' – Coffee 2014-09-27 15:52:20

+3

您正在隐藏异常消息抛出如何完全异常栈。 – OldProgrammer 2014-09-27 15:54:03

+0

@OldProgrammer - 你的意思是'如何完全异常栈.'咋做? – Coffee 2014-09-27 15:56:22

回答

0

确保xe是你Service 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

+0

:是,XE是我的服务名称,因为我可以使用SQL开发人员创建连接 – taibc 2014-10-11 16:21:13