oracle 数据源 java.sql.SQLException:字符串索引超出范围:-1 in spring

oracle datasource java.sql.SQLException: String index out of range: -1 in spring

我有一个 spring 引导 1.5.22 应用程序,我正在尝试使用 tomcat-jdbc 和 ojdbc6 连接到 oracle 10g。
我有连接到我的数据库的数据源 getproperties() 函数:

DataSource dataSourceProperties()  {
     DataSource ds=null;
    try {
    ds = DataSourceBuilder.create()
            .driverClassName("oracle.jdbc.OracleDriver")
            .url("jdbc:oracle:thin:@//host:port/DBname")
            .username("xxx")
            .password("xxx")
            .build();
    }catch (Exception e) {
        System.out.println(e.toString());}

当 url 使用此 SID 格式时:jdbc:oracle:thin:@host:port:DBname 我收到一个 oracle 错误:refused:ROR=(code=12505)(EMFI=4) 当我将其更改为 jdbc:oracle:thin:@host:port/DBname 我得到 java.sql.SQLException: String index out of范围:-1 最后这种格式 jdbc:oracle:thin:@//host:port/DBname 并且仍然得到 java.sql.SQLException: String index超出范围:-1

这就是我在 getConnection 函数中调用该函数的方式:

public Connection getConnection() throws SQLException
    {  
        
        Connection conn=null;
            try {
                this.ds=this.dataSourceProperties();
                conn=ds.getConnection();
            }catch (SQLException e ) {
                System.out.println(e.toString());
                
            }
            return conn;}

这里有一些错误日志:

2021-08-16 18:33:59.926 ERROR 22476 --- [bio-8080-exec-1] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

java.sql.SQLException: String index out of range: -1
        at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:332) ~[tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212) ~[tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:736) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:668) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) [tomcat-jdbc-8.5.43.jar:na]
        at com..web.services.DBConnection.getConnection(DBConnection.java:68) 

[classes/:na]
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
java.sql.SQLException: String index out of range: -1
Mon Aug 16 18:33:59 CEST 2021
java.lang.NullPointerException

这是将 url 修改为正确格式后的新错误日志 jdbc:oracle:thin://xx:1509/xxx

2021-08-17 11:25:27.368 ERROR 15440 --- [bio-8080-exec-5] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

java.sql.SQLException: Connection refused: connect
        at oracle.jdbc.dbaccess.DBError.check_error(DBError.java:223) ~[jdbc-oracle-11.2.0.3.jar:na]
        at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:100) ~[jdbc-oracle-11.2.0.3.jar:na]
        at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:146) ~[jdbc-oracle-11.2.0.3.jar:na]
        at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319) ~[tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212) ~[tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:736) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:668) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) [tomcat-jdbc-8.5.43.jar:na]
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) [tomcat-jdbc-8.5.43.jar:na]


    
    
    

已解决: 这个对我有用:)

jdbc:oracle:thin://host:port/ServiceName

一个例子:

jdbc:oracle:thin://主机名:1509/ServiceName(或数据库名称)