gpt4 book ai didi

java - 无法将 clob 转换为字符串

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:18:20 24 4
gpt4 key购买 nike

我正在尝试将我的 clob 对象转换为字符串以在我的 JSTL 页面上显示它,为此我正在编写以下代码

public String convertClobToString(Clob clob){
String toRet="";
if(clob!=null)
{
try
{
long length=clob.length();
toRet=clob.getSubString(1, (int)length);
}
catch(Exception ex)
{
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
ex.printStackTrace();
}
}
return toRet;
}

但是在使用“long length = clob.length()”计算长度时会抛出以下异常

java.sql.SQLException: Must be logged on to server

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
at oracle.jdbc.dbaccess.DBError.check_error(DBError.java:1160)
at oracle.jdbc.ttc7.TTC7Protocol.assertLoggedIn(TTC7Protocol.java:2196)
at oracle.jdbc.ttc7.TTC7Protocol.lobLength(TTC7Protocol.java:2698)
at oracle.sql.LobDBAccessImpl.length(LobDBAccessImpl.java:468)
at oracle.sql.CLOB.length(CLOB.java:214)
at org.hibernate.lob.SerializableClob.length(SerializableClob.java:33)
at com.starcj.cmt.application.dao.ApplicationDaoImpl.convertClobToString(ApplicationDaoImpl.java:110)
at com.starcj.cmt.termsandcondmgmt.dao.TermsAndCondDaoImpl.getAllActiveTermsandcond(TermsAndCondDaoImpl.java:91)
at com.starcj.cmt.termsandcondmgmt.service.TermsAndCondServiceImpl.getAllActiveTermsAndCond(TermsAndCondServiceImpl.java:43)
at com.starcj.cmt.termsandcondmgmt.controller.TermsAndCondListingCMTController.handleRequest(TermsAndCondListingCMTController.java:66)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

Exceptoin是必须登录到服务器

但我正在从数据库中获取 clob 对象

最佳答案

在您的 hibernate 实体(我们看不到)中声明该属性为 String 类型并使用 @Lob 对其进行注释(同样,我不知道您使用的是注释还是 XML),无需任何手动转换即可使用。

发生这种情况是因为在您尝试解析实体时, session 和底层连接已关闭,因此您无法从数据库中流式传输 CLOB 的值。

上述解决方案的替代方案可能是使用 OpenSessionInView筛选。它会让您在呈现 View (即您的 jsps)时打开 session (和底层数据库连接)

关于java - 无法将 clob 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2278445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com