1

我正在尝试在 Hibernate 中执行以下代码以从 mySQL 数据库创建一个 .csv 文件。:

String sql = 
"SELECT * INTO OUTFILE 'table.csv' FIELDS TERMINATED BY ','" +
" OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n' " +
"FROM match INNER JOIN totala ON match_code= match";

.csv 文件已正确创建,但随后出现以下错误:

Exception monitoring the input folder org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2452)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
    at org.hibernate.loader.Loader.list(Loader.java:2187)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1832)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:179)
    at com.salebuild.dao.hibernate.CorporateTitleDAOImpl.dumpTitleCompanyCSV(CorporateTitleDAOImpl.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
    at $Proxy128.dumpTitleCompanyCSV(Unknown Source)
    at com.salebuild.service.CorporateTitleServiceImpl.dumpTitleCompanyCSV(CorporateTitleServiceImpl.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy159.dumpTitleCompanyCSV(Unknown Source)
    at com.salebuild.service.collector.ContactScraperMonitorInputFolder.addNewFile(ContactScraperMonitorInputFolder.java:61)
    at com.salebuild.service.collector.ContactScraperMonitorInputFolder.run(ContactScraperMonitorInputFolder.java:37)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: ResultSet is from UPDATE. No Data.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7021)
    at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:2859)
    at org.hibernate.loader.Loader.doQuery(Loader.java:741)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    ... 29 more

一些谷歌搜索表明这可能是因为查询没有返回任何结果,所以我得到“java.sql.SQLException:ResultSet 来自 UPDATE。没有数据”。但是我该如何克服这个异常。我不希望我的代码中断。

我正在运行如下查询:

public void dumpTitleCompanyCSV(String titleId,String titleName, String dumpFolder) {
        Session session = getSession();
        String hql = "SELECT "+titleId+" ,id, \""+titleName+"\", name " +
                    "FROM company INTO OUTFILE 'd:/contact/input/file.csv' " +
                    "FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'";
        logger.debug("jeets The query is \n "+hql);
        Query query  = session.createSQLQuery(hql);
         query.list();
    }
4

1 回答 1

2

使用executeUpdate()方法运行不返回任何结果的查询。

在您的情况下,由于结果将被转储到外部文件,因此不会有结果集。请注意,这与由于不满足条件而可能返回空结果的查询不同。

编辑 :

由于这似乎也失败了,我认为您可能需要尝试使用纯 JDBC。

做类似下面的事情

session.doWork(new Work() {
    @Override
    public void execute(Connection con) throws SQLException {
        //run your query natively here
    }
});
于 2013-09-10T10:15:29.897 回答