【发布时间】:2011-08-24 13:58:29
【问题描述】:
我正在使用从休眠中获取的 JDBC 连接对象来执行浴更新,我这样做是因为我需要使用 MySql ON DUPLICATE 功能。但是,在尝试插入时,我无法插入说该字符串具有特殊字符,
Session session = sessionFactory.openSession();
PreparedStatement pstm = null;
Connection conn = session.connection();
try
{
IRKeyWordTweet record = null;
conn.setAutoCommit(false);
for (Iterator itrList = statusToInsert.iterator(); itrList.hasNext();)
{
try
{
record = (IRKeyWordTweet) itrList.next();
pstm = (PreparedStatement) conn.prepareStatement("INSERT QUERY");
System.err.println(record.getTwtText());
//tweetId
pstm.setLong(1, record.getTweetId());
Setters For Prepared statement....
pstm.addBatch();
int[] updateCounts = pstm.executeBatch();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
catch (Exception e)
{
log.error("Exception Occured",e);
}
finally
{
try
{
pstm.close();
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
session.close();
}
这可能是什么原因造成的?
【问题讨论】:
-
您省略了最重要的部分之一:SQL 本身。
-
另一件重要的事情:你没有为准备好的语句包括你的设置器。
-
我不知道我投赞成票是因为这是@Jon Skeet 还是因为他有观点:)
-
向我们展示您在哪里逃脱的查询。
-
您没有向我们展示可能包含错误的代码部分:实际查询以及绑定参数的方式。此外,你为什么不简单地使用 session.createSQLQuery()?
标签: java hibernate jdbc prepared-statement