我试图更新一个包含blob数据类型的表单,但是如上所述仍然会出现错误。这是我在ItemDao中更新表单的编码。
public static int update(ItemBean u){
int status=0;
try{
Connection con=ItemDao.getConnection();
PreparedStatement ps=con.prepareStatement("update item set category=?,name=?,brand=?,type=?,style=?,colour=?,itemSize=?,width=?,height=?,price=?,gender=?,images=? where itemId=?");
ps.setString(1,u.getItemId());
ps.setString(2,u.getCategory());
ps.setString(3,u.getName());
ps.setString(4,u.getBrand());
ps.setString(5,u.getType());
ps.setString(6,u.getStyle());
ps.setString(7,u.getColour());
ps.setString(8,u.getItemSize());
ps.setString(9,u.getWidth());
ps.setString(10,u.getHeight());
ps.setString(11,u.getPrice());
ps.setString(12,u.getGender());
ps.setBlob(13,u.getImages());
//FileItem images;
//ps.setBinaryStream(13, (InputStream) u.getImages());
status=ps.executeUpdate();
}catch(Exception ex){ex.printStackTrace();}
return status;
}
这是堆栈跟踪:
java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected - got BLOB
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1046)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3694)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1354)
at b.ItemDao.update(ItemDao.java:78)
at org.apache.jsp.editItem_jsp._jspService(editItem_jsp.java:84)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2403)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2392)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
声明blob数据类型的正确方法是什么?谢谢你的帮助
答案 0 :(得分:0)
编辑正如@Tom所说,你的另一个问题是索引没有正确分配。所有占位符?
从1开始并继续到结尾,这包括where子句中的占位符。
index
update item set
category=?,...........1......ps.setString(1,u.getCategory());
name=?,...............2......ps.setString(2,u.getName());
brand=?,..............3......ps.setString(3,u.getBrand());
type=?,...............4......ps.setString(4,u.getType());
style=?,..............5......ps.setString(5,u.getStyle());
colour=?,.............6......ps.setString(6,u.getColour());
itemSize=?,...........7......ps.setString(7,u.getItemSize());
width=?,..............8......ps.setString(8,u.getWidth());
height=?,.............9......ps.setString(9,u.getHeight());
price=?,.............10......ps.setString(10,u.getPrice());
gender=?,............11......ps.setString(11,u.getGender());
images=?.............12......ps.setBlob(12,u.getImages());
where itemId=?.......13......ps.setString(13,u.getItemId());
更正索引后,您可以更正.getImages()
仍然存在的blob问题,除非它是javax.sql.Blob
类型。
有关如何使用Blob类型,请参阅此question。
1)创建BLOB使用Connection.createBlob
2)将BLOB写入DB使用PreparedStatement.setBlob
3)从DB中读取BLOB使用ResultSet.getBlob