在表中插入记录时,在MySql中查询空异常

时间:2014-08-02 07:10:33

标签: java mysql hibernate

我正在尝试在MySql中使用createQuery()在表中插入记录,但它给了我Query empty()异常

代码:

String queryString="INSERT INTO Mytable (field1,field2,field3,field4,field5) Select 111,1,111,NULL,'2014-03-15 11:04:48' union all   Select 122,3,13,NULL,'2014-03-15 11:10:48'";
Query query=session.createSQLQuery(queryString);
query.executeUpdate();

StackTrace:

Query was empty.()
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
com.mysql.jdbc.Util.getInstance(Util.java:381)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)

com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)

com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate
(NewProxyPreparedStatement.java:175)

org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate
(NativeSQLQueryPlan.java:165)
org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1163)
org.hibernate.impl.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:334)

有人可以告诉我这有什么问题吗?

经过这么多尝试后我找到了解决方案。

解决:

Problem is queryString is passed in method and it generate dynamic query 
so sometimes   it only generate ';' in queryString so in MySql if hibernate found 
';' in session.creareSQLQuery(queryString) then it throws 'Query Was Empty' Exception.

2 个答案:

答案 0 :(得分:0)

使用以下查询

String queryString="INSERT INTO Mytable (field1,field2,field3,field4,field5) Select 111,1,111,NULL,'2014-03-15 11:04:48' union all   Select 122,3,13,NULL,'2014-03-15 11:10:48'";

希望它有所帮助。

答案 1 :(得分:0)

问题是在方法中传递queryString并生成动态查询 所以有时它只会产生&#39 ;;'在queryString中,所以在MySql中如果找到了hibernate &#39 ;;'在session.creareSQLQuery(queryString)中,它抛出“查询为空”'例外