求解决 提示是数据库问题
package util;import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.dbcp.BasicDataSource;
/**
* 该类引入了连接池来管理连接,
* 连接池代替了DriverManager。
* 它是DBTool的升级版。
*/
public class DBUtil {
private static BasicDataSource ds;
static {
//1.只读取一次连接参数
Properties p = new Properties();
try {
p.load(DBUtil.class.getClassLoader()
.getResourceAsStream("db.properties"));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("读取配置文件失败",e);
}
String driver = p.getProperty("driver");
String url = p.getProperty("url");
String user = p.getProperty("user");
String pwd = p.getProperty("pwd");
String initSize = p.getProperty("initSize");
String maxSize = p.getProperty("maxSize");
//2.只创建一个连接池
ds = new BasicDataSource();
//3.将连接参数设置给连接池
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(user);
ds.setPassword(pwd);
ds.setInitialSize(new Integer(initSize));
ds.setMaxActive(new Integer(maxSize));
}
public static Connection getConnection()
throws SQLException {
return ds.getConnection();
}
/**
* 由连接池创建的连接,其close()被连接池改为
* 归还的作用,而不是真正关闭连接。并且归还时,
* 该连接内的数据被清空,状态重置为空闲态。
*/
public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("归还连接失败",e);
}
}
}
public static void main(String[] args)
throws SQLException {
Connection conn =
DBUtil.getConnection();
System.out.println(conn);
DBUtil.close(conn);
}
}
以下是报错求解决 提示是说数据库连接不上
Exception in thread "main" 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.createPoolableConnectionFactory(BasicDataSource.java:1549)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at util.DBUtil.getConnection(DBUtil.java:48)
at util.DBUtil.main(DBUtil.java:70)
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:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:261)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:441)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
... 4 more
sid无法识别,默认的数据库是orcl,你链接的数据库是orcl吗?如果不是,修改下你数据库的名称,找到以下目录,/oracle/product/10.2.0/db_1/network/admin,修改listener.ora,在里面的sid括号里面修改,或者直接添加一个sid,希望可以帮到你 yun 发表于 2016-11-22 22:54 static/image/common/back.gif
sid无法识别,默认的数据库是orcl,你链接的数据库是orcl吗?如果不是,修改下你数据库的名称,找到以下目录 ...
好的 谢谢大神 我试下
页:
[1]