4
我正在使用SPRING famework,並且當前正在嘗試在創建新條目(行)時將數組添加到數據庫中。有問題的列被稱爲「關鍵字」。因此,這裏是我的SQL初始化代碼(這看起來好像沒什麼問題)添加數組時出現PreparedStatment錯誤
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)
);
這裏是代碼是崩潰和燃燒我的附加功能:
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 – JMH 2013-03-21 22:22:38
此鏈接可以幫助你.. [PreparedStatement的SET數組(http://www.java2s.com/Code/Java /Database-SQL-JDBC/PreparedStatementSetArray.htm) – Smit 2013-03-21 22:30:19
不是忘恩負義@Smit,但Spring似乎並不知道oracle,所以我不能導入或使用它...這不是java/spring的內置部分? – JMH 2013-03-21 22:38:23