我想用Tomcat在PostgreSQL中上传一个文件:
@Resource(name = "jdbc/DefaultDB")
private DataSource ds;
Connection conn = ds.getConnection();
我试着用这种方式投射
conn
对象:PGConnection pgCon = ((org.apache.commons.dbcp.DelegatingConnection) conn).getInnermostDelegate();
我明白了
Caused by: java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to org.postgresql.PGConnection
我也试过:
Connection unwrap = conn.unwrap(Connection.class);
connSec = (org.postgresql.PGConnection) unwrap;
我明白了
java.sql.SQLException: Cannot unwrap to org.postgresql.PGConnection
LargeObjectManager pgCon = pgCon.getLargeObjectAPI();
实现代码的正确方法是什么?
附笔
我试过这个:
PGConnection pgConnection = null;
if (conn.isWrapperFor(PGConnection.class))
{
pgConnection = conn.unwrap(PGConnection.class);
}
LargeObjectManager lobj = pgConnection.getLargeObjectAPI();
但我在这一行得到NPE
LargeObjectManager lobj = pgConnection.getLargeObjectAPI();
最佳答案
if (dataSource.getConnection().isWrapperFor(PGConnection.class)) {
PGConnection pgConnection = dataSource.getConnection().unwrap(PGConnection.class);
}
关于java - 将java.sql.Connection转换为PGConnection,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36986653/