我试图使用
java代码向postgres插入一个数组,但我总是得到这个错误:
SEVERE [http-nio-8080-exec-2]org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service()
for servlet [] in context with path [/] threw exception
[Servlet execution threw an exception] with root cause
java.lang.AbstractMethodError:
com.mchange.v2.c3p0.impl.NewProxyConnection.createArrayOf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/sql/Array;
使用的代码
pst = getConnection().prepareStatement(INSERT_QUERY,PreparedStatement.RETURN_GENERATED_KEYS);
pst.setString(1, t.getname());
pst.setString(2, t.getEmail());
Array itemIds = conn.createArrayOf("bigint", t.getItemIds());
pst.setArray(3, itemIds);
如果我通过主类运行该函数它工作正常,但在部署到tomcat服务器后,http调用失败并出现上述错误.
>使用DB – Postgres
> JDBC驱动程序postgres-9.1-901-1.jdbc4
> c3p0-0.9.5-pre10
> tomcat-8.0.24
根据我的研究,createArrayOf()应该与jdbc4和c3p0-0.9.5一起使用.
使用它工作正常,但我不认为这是正确的方法
if (conn instanceof C3P0ProxyConnection) {
C3P0ProxyConnection proxy = (C3P0ProxyConnection) conn;
try {
Method m = Connection.class.getMethod("createArrayOf", String.class, Object[].class);
Object[] args = { "bigint", t.getItemIds() };
itemIds = (Array) proxy.rawConnectionOperation(m, C3P0ProxyConnection.RAW_CONNECTION, args);
} catch (IllegalArgumentException e) {
throw new SQLException(e);
}
} else {
itemIds = conn.createArrayOf("bigint", t.getItemIds());
}
需要帮忙 .
谢谢
最佳答案 我强烈怀疑你的应用程序的有效CLASSPATH中有一个旧版本的c3p0.我已经从Maven Central上的c3p0-0.9.5-pre10.jar和c3p0-0.9.5.1.jar下载并验证了com.mchange.v2.c3p0.impl.NewProxyConnection确实包含createArrayOf方法.
% javap -sysinfo -cp ./c3p0-0.9.5.1.jar com.mchange.v2.c3p0.impl.NewProxyConnection
Classfile jar:file:/Users/swaldman/tmp/c3p0jars/c3p0-0.9.5.1.jar!/com/mchange/v2/c3p0/impl/NewProxyConnection.class
Last modified Jun 16, 2015; size 27098 bytes
MD5 checksum c1ff36b87219ddc84c92fb6c1445a2d1
Compiled from "NewProxyConnection.java"
public final class com.mchange.v2.c3p0.impl.NewProxyConnection implements java.sql.Connection,com.mchange.v2.c3p0.C3P0ProxyConnection {
//...
public synchronized java.sql.Array createArrayOf(java.lang.String, java.lang.Object[]) throws java.sql.SQLException;
//...
}
虽然这不太可能是您的问题的原因,但我建议您升级到c3p0-0.9.5.1而不是使用预发布版本.
要查看应用程序实际使用的c3p0版本,请在日志文件中查看初始化c3p0库时打印的横幅的INFO.它应该看起来像这样:
INFO: Initializing c3p0-0.9.5.1 [built 16-June-2015 00:06:36 -0700; debug? true; trace: 10]
我怀疑你会看到一个较旧的版本,在某个地方,也许在传递依赖项中,你正在引入该库的0.9.1.x或0.9.2.x版本.
祝好运!