现在尝试着用这个来做下数据库连接
dbcp.xml配置文件如下:
<dbcp>
<datasource>
<name>HZMOBILE</name>
<driverClassName>oracle.jdbc.driver.OracleDriver</driverClassName>
<url>jdbc:oracle:thin:10.10.10.10:1521:HZMOBILE</url>
<username>HZMOBILE</username>
<password>HZMOBILE</password>
<minIdle>1</minIdle>
<maxIdle>3</maxIdle>
<initialSize>1</initialSize>
<maxActive>12</maxActive>
<maxWait>3000</maxWait>
<testWhileIdle>true</testWhileIdle>
<removeAbandoned>true</removeAbandoned>
<removeAbandonedTimeout>60</removeAbandonedTimeout>
<logAbandoned>true</logAbandoned>
<timeBetweenEvictionRunsMillis>60000</timeBetweenEvictionRunsMillis>
<minEvictableIdleTimeMillis>60000</minEvictableIdleTimeMillis>
</datasource>
</dbcp>
有个POJO类,用来给XML文件的节点提供get(),set()方法获取连接的类中是这样写的:
private void init(){
try {
datasources=new HashMap<String,BasicDataSource>();
XStream xstream = new XStream(new DomDriver());
xstream.alias("dbcp", ArrayList.class);
xstream.alias("datasource", DataSourceVO.class);
ArrayList l = (ArrayList) xstream.fromXML(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("dbcp.xml")));
for (Iterator i = l.iterator(); i.hasNext();) {
DataSourceVO m = (DataSourceVO)i.next(); BasicDataSource s=new BasicDataSource();
s.setDriverClassName(m.getDriverClassName());
s.setUrl(m.getUrl());
s.setUsername(m.getUsername());
s.setPassword(m.getPassword());
s.setMinIdle(m.getMinIdle());
s.setMaxIdle(m.getMaxIdle());
s.setInitialSize(m.getInitialSize());
s.setMaxActive(m.getMaxActive());
s.setMaxWait(m.getMaxWait());
s.setTestWhileIdle(m.isTestWhileIdle());
s.setTimeBetweenEvictionRunsMillis(m.getTimeBetweenEvictionRunsMillis());
s.setRemoveAbandoned(m.isLogAbandoned());
s.setRemoveAbandonedTimeout(m.getRemoveAbandonedTimeout());
s.setLogAbandoned(m.isLogAbandoned());
s.setValidationQuery("SELECT 1");
s.setTestOnBorrow(true);
s.setMinEvictableIdleTimeMillis(m.getMinEvictableIdleTimeMillis());
datasources.put(m.getName(),s);
logger.info("DataSource "+m.getName()+" Initialized!");
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} }
启动程序时:日志有有记录(DBCPFactory.java:67) - DataSource HZMOBILE Initialized!
但在获取连接时报以下错误:
INFO [main] - DataSource hzmobile Initialized!
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool@f9f9d8)
LogAbandoned: true
RemoveAbandoned: true
RemoveAbandonedTimeout: 60
ERROR [main] - DBCP: get dbcp connection error: Cannot create PoolableConnectionFactory (Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:orcl
)
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:orcl
)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at com.mobtec.hzmobileback.common.db.DbUtil.getDbcpConn(DbUtil.java:27)
at com.mobtec.hzmobileback.common.db.DbUtil.getConnection(DbUtil.java:16)
at com.mobtec.hzmobileback.common.db.DbUtil.main(DbUtil.java:85)
Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:orcl at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:279)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:327)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:360)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:150)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:31)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:595)
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
... 4 more请知道的各位帮帮手解决。谢谢!
dbcp.xml配置文件如下:
<dbcp>
<datasource>
<name>HZMOBILE</name>
<driverClassName>oracle.jdbc.driver.OracleDriver</driverClassName>
<url>jdbc:oracle:thin:10.10.10.10:1521:HZMOBILE</url>
<username>HZMOBILE</username>
<password>HZMOBILE</password>
<minIdle>1</minIdle>
<maxIdle>3</maxIdle>
<initialSize>1</initialSize>
<maxActive>12</maxActive>
<maxWait>3000</maxWait>
<testWhileIdle>true</testWhileIdle>
<removeAbandoned>true</removeAbandoned>
<removeAbandonedTimeout>60</removeAbandonedTimeout>
<logAbandoned>true</logAbandoned>
<timeBetweenEvictionRunsMillis>60000</timeBetweenEvictionRunsMillis>
<minEvictableIdleTimeMillis>60000</minEvictableIdleTimeMillis>
</datasource>
</dbcp>
有个POJO类,用来给XML文件的节点提供get(),set()方法获取连接的类中是这样写的:
private void init(){
try {
datasources=new HashMap<String,BasicDataSource>();
XStream xstream = new XStream(new DomDriver());
xstream.alias("dbcp", ArrayList.class);
xstream.alias("datasource", DataSourceVO.class);
ArrayList l = (ArrayList) xstream.fromXML(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("dbcp.xml")));
for (Iterator i = l.iterator(); i.hasNext();) {
DataSourceVO m = (DataSourceVO)i.next(); BasicDataSource s=new BasicDataSource();
s.setDriverClassName(m.getDriverClassName());
s.setUrl(m.getUrl());
s.setUsername(m.getUsername());
s.setPassword(m.getPassword());
s.setMinIdle(m.getMinIdle());
s.setMaxIdle(m.getMaxIdle());
s.setInitialSize(m.getInitialSize());
s.setMaxActive(m.getMaxActive());
s.setMaxWait(m.getMaxWait());
s.setTestWhileIdle(m.isTestWhileIdle());
s.setTimeBetweenEvictionRunsMillis(m.getTimeBetweenEvictionRunsMillis());
s.setRemoveAbandoned(m.isLogAbandoned());
s.setRemoveAbandonedTimeout(m.getRemoveAbandonedTimeout());
s.setLogAbandoned(m.isLogAbandoned());
s.setValidationQuery("SELECT 1");
s.setTestOnBorrow(true);
s.setMinEvictableIdleTimeMillis(m.getMinEvictableIdleTimeMillis());
datasources.put(m.getName(),s);
logger.info("DataSource "+m.getName()+" Initialized!");
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} }
启动程序时:日志有有记录(DBCPFactory.java:67) - DataSource HZMOBILE Initialized!
但在获取连接时报以下错误:
INFO [main] - DataSource hzmobile Initialized!
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool@f9f9d8)
LogAbandoned: true
RemoveAbandoned: true
RemoveAbandonedTimeout: 60
ERROR [main] - DBCP: get dbcp connection error: Cannot create PoolableConnectionFactory (Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:orcl
)
org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:orcl
)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1225)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at com.mobtec.hzmobileback.common.db.DbUtil.getDbcpConn(DbUtil.java:27)
at com.mobtec.hzmobileback.common.db.DbUtil.getConnection(DbUtil.java:16)
at com.mobtec.hzmobileback.common.db.DbUtil.main(DbUtil.java:85)
Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:orcl at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:279)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:327)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:360)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:150)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:31)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:595)
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294)
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221)
... 4 more请知道的各位帮帮手解决。谢谢!
The Connection descriptor used by the client was:
localhost:1521:orcl 看提示,说server name错误,确认实例名是HZMOBILE 吗,还是orcl
应该就是一楼说的那样,你应该是弄错了
<url>jdbc:oracle:thin:10.10.10.10:1521:HZMOBILE </url>
红色的地方应该是填写数据库实例名
<url>jdbc:oracle:thin:10.10.10.10:1521:orcl </url>
HZMOBILE是数据源名,大概是指oracle服务器
数据源下有多个数据库,orcl是你要连接的那个库
Caused by: java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:orcl
以上错误所描述的是不是监听器拒绝连接是由于描述的连接信息已经被占用了,还是什么?
对于数据库库,我本地是客户端访问来的。在服务器上数据库名是:HZMOBILE,在本地配置了一个HZMOBILE_150,通过pl/sql连接。。
我该写哪个名称呢?
[/size]
jdbc:oracle:thin:@10.10.10.10:1521:HZMOBILE