我最近回到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;
}
电话:
cstmt.setObject(column.getName(), entity.getClass().getDeclaredField(column.getName()).get(entity) /**, SQLType targetSqlType ?? */ );
失败时出现一个nullpointer异常,我已经检查过两个值都没问题,但最终它只是做了一个空指针。准备好的声明看起来也不错。我正在尝试使用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");
}
。以下是我的版本:
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
这是最新的最新版本。
如果我使用调试器运行它,那么这给出nullpointer的场景如下所示:
cstmt.setObject("email", "herpderp@gmail.com");
callableStatement中的orginalSql字段如下所示:
create_account(?, ?, ?, ?, ?)
存储过程如下所示:
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
由于
答案 0 :(得分:0)
调用存储过程时,它是这样的:
CallableStatement cstmt = connection.prepareCall("{CALL create_" + table.getName() + "(" + columns.stream().map( column -> "?").collect(Collectors.joining(",")) + ")}");
如果没有CALL,它就没有好处,只是希望错误消息更有帮助,显然是一个完整的脑力计:)