【发布时间】:2013-03-11 15:39:24
【问题描述】:
我正在使用 SPRING 功能,我目前正在尝试在创建新条目(行)时将数组添加到我的数据库中。有问题的列称为“关键字”。所以这是我的 sql 初始化代码(我觉得很好)
CREATE TABLE IF NOT EXISTS Email (
Email_Id INT UNSIGNED NOT NULL AUTO_INCREMENT,
Sender_Email VARCHAR(255) NOT NULL,
Recipient_Email VARCHAR(255) NOT NULL,
Subject VARCHAR(255) NOT NULL,
Body TEXT,
Attachment_Path VARCHAR(255) NOT NULL,
Creation_Date DATETIME NOT NULL,
MaxKeywords INT UNSIGNED NOT NULL,
Keywords VARCHAR(255) NOT NULL,
Primary Key(Email_Id)
);
这是我的 add 函数中崩溃和烧毁的代码:
public boolean add(final MessageDto emailDto){
boolean result = false;
int rowsAffected;
KeyHolder keyHolder = new GeneratedKeyHolder();
final String sql;
sql = "INSERT INTO email (Sender_Email, Recipient_Email, Subject, Body, Attachment_Path, Creation_Date, MaxKeywords, Keywords) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
try{
rowsAffected = getJdbcTemplate().update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement statement =
connection.prepareStatement(sql, new String[] {"emailId"});
String[] foo = {"A","B"};
statement.setString(1, emailDto.getFrom());
statement.setString(2, emailDto.getTo());
statement.setString(3, emailDto.getSubject());
statement.setString(4, emailDto.getBody());
statement.setString(5, emailDto.getAttachmentPath());
statement.setTimestamp(6, new Timestamp(emailDto.getCreationDate().getMillis()));
statement.setInt(7, emailDto.getMaxKeywordCount());
statement.setArray(8, connection.createArrayOf("varchar", foo));
return statement;
}
}, keyHolder);
if(rowsAffected > 0){
emailDto.setEmailId(keyHolder.getKey().intValue());
result = true;
}
} catch (Exception e){
throw new RuntimeException(e);
}
return result;
}
抛出的错误是:
org.springframework.dao.InvalidDataAccessApiUsageException: PreparedStatementCallback; SQL []; null; nested exception is java.sql.SQLFeatureNotSupportedException
帮助?
【问题讨论】:
-
它说类型不正确——它想要一个 java.sql.Array
-
此链接可能对您有所帮助.. PreparedStatement Set Array
-
不要忘恩负义@Smit,但Spring似乎不了解oracle,所以我无法导入或使用它...这不是java/spring的内置部分吗?
-
您使用的是哪个数据库以及什么驱动程序版本?貌似驱动不支持 setArray
-
@Jessermon - 是的,到目前为止我最好的想法是把它变成一个 JSON 字符串,并存储它......但我对此有某种道德上的“必须工作”的感觉设置数组