TA的每日心情 | 郁闷 2018-8-21 10:39 |
---|
签到天数: 16 天 [LV.4]偶尔看看III
|
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:1521rcl
)
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:1521rcl
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
|
|