2 u014063105 u014063105 于 2014.08.21 13:00 提问

Hibernate使用sql语句操作数据库时出现的异常could not execute query

代码如下:

HibernateUtil:

public static int createSqlQuery(String sql,
Serializable... serializables) {
Session session = null;
Integer result = null;
try {
session = getSession();
session.beginTransaction();
Query query = session.createSQLQuery(sql);
if (serializables != null) {
for (int i = 0; i < serializables.length; i++) {
query.setParameter(i, serializables[i]);
}
}
result = (Integer) query.uniqueResult();
session.getTransaction().commit();
} catch (HibernateException e) {
if (null != session) {
session.getTransaction().rollback();
}
e.printStackTrace();
}
return result;
}

Test:
public class Test {
public static void main(String[] args) {
String sql = "insert into t_user values(2,'Nero','123','18628840518','devilmaycryn@qq.com')";
System.out.println(HibernateUtil.createSqlQuery(sql));
}
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!