[英]JDBC DatabaseMetaData says that my mysql server has no support for namedParameters and accordingly throws a null pointer exception
Im back on java recently, and i am struggling making the following method work: 我最近回到java,我正在努力使以下方法工作:
@Override
public T createEntity(T entity) throws SQLException{
DatabaseMetaData dbMeta = connection.getMetaData();
boolean okay = dbMeta.supportsNamedParameters();
if(okay) {
System.out.println("Supports named arguments");
} else {
System.out.println("Does not support named arguments");
}
CallableStatement cstmt = connection.prepareCall("create_" + table.getName() + "(" + columns.stream().map( column -> "?").collect(Collectors.joining(",")) + ")");
final ThrowingConsumer<DatabaseColumn> throwingConsumer = column -> {
try {
String alright = "herpderp";
cstmt.setObject(column.getName(), entity.getClass().getDeclaredField(column.getName()).get(entity) /**, SQLType targetSqlType ?? */ );
ParameterMetaData meta = cstmt.getParameterMetaData();
} catch(Exception e) {
e.printStackTrace();
}
};
columns.stream().forEach(throwingConsumer);
cstmt.execute();
return null;
}
The call: 电话:
cstmt.setObject(column.getName(), entity.getClass().getDeclaredField(column.getName()).get(entity) /**, SQLType targetSqlType ?? */ );
Fails with a nullpointer exception, i have checked that both of the values goes in are just fine, but eventually it just does a null pointer. 失败了一个nullpointer异常,我已经检查过两个值都很好,但最终它只是一个空指针。 The prepared statement also looks just fine. 准备好的声明看起来也不错。 I am trying to use namedParameters so that is what i think is the culprit, i tried to check if my setup was even compatible with namedParameters, and it turns out that DatabaseMetaData doesn't think so, in the following code: 我正在尝试使用namedParameters,这就是我认为的罪魁祸首,我试图检查我的设置是否与namedParameters兼容,并且事实证明DatabaseMetaData不这么认为,在以下代码中:
DatabaseMetaData dbMeta = connection.getMetaData();
boolean okay = dbMeta.supportsNamedParameters();
if(okay) {
System.out.println("Supports named arguments");
} else {
System.out.println("Does not support named arguments");
}
. 。 The following is my versions: 以下是我的版本:
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.3'
mysql: mysql Ver 14.14 Distrib 5.7.13, for Linux (x86_64) using EditLine wrapper
so pretty much the newest of the newest. 这是最新的最新版本。
If i run through it with the debugger, a scenario where this gives a nullpointer looks like this: 如果我使用调试器运行它,这给出nullpointer的场景如下所示:
cstmt.setObject("email", "herpderp@gmail.com");
Where the orginalSql field in the callableStatement looks like this: 其中callableStatement中的orginalSql字段如下所示:
create_account(?, ?, ?, ?, ?)
The stored procedure looks like this: 存储过程如下所示:
CREATE DEFINER=`root`@`localhost` PROCEDURE `create_account`(email VARCHAR(45), username VARCHAR (45), pw VARCHAR (45), guid VARCHAR(45), verified tinyint(4))
BEGIN
insert into account (`email`, `username`, `password`, `guid`, `verified`) values (email, username, pw, guid, verified);
END
Thanks 谢谢
When calling a stored procedure, it goes like this: 调用存储过程时,它是这样的:
CallableStatement cstmt = connection.prepareCall("{CALL create_" + table.getName() + "(" + columns.stream().map( column -> "?").collect(Collectors.joining(",")) + ")}");
Without the CALL, its no good, wouldve just wished the error messages was more helpful, obviously a complete brainfart :) 没有CALL,它没有好处,只是希望错误消息更有帮助,显然是一个完整的脑筋:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.