2015-04-27 116 views
-1

我想不出什麼似乎是問題與此代碼SQL預處理語句異常

public boolean InsertFile(UserUpload userUpload){ 
    try { 
     con = connectToDB(); 
     stmt = con.prepareStatement("INSERT INTO USERUPLOADS VALUES(?,?,?,?)"); 
     stmt.setInt(1, getUploadId()); 
     stmt.setString(2, userUpload.getFileName()); 
     stmt.setString(3, userUpload.getFilePath()); 
     stmt.setString(4, userUpload.getUserId()); 
     stmt.executeUpdate(); 
     return true; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 

我得到以下異常:

java.sql.SQLException: The column position '2' is out of range. The number of columns for this ResultSet is '0'. 
      at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source) 
      at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source) 
      at org.apache.derby.client.am.ColumnMetaData.getColumnType(Unknown Source) 
      at org.apache.derby.client.am.PreparedStatement.setString(Unknown Source) 
      at DBConnection.DBConnectionManagerClass.InsertFile(DBConnectionManagerClass.java:139) 
      at classes.FileHandlerClass.uploadFile(FileHandlerClass.java:46) 
      at com.fileservice.jigar.FileHandlerImpl.uploadFile(FileHandlerImpl.java:26) 
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
      at java.lang.reflect.Method.invoke(Method.java:483) 
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) 
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
      at java.lang.reflect.Method.invoke(Method.java:483) 
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
      at java.lang.reflect.Method.invoke(Method.java:483) 
      at com.sun.xml.internal.ws.api.server.MethodUtil.invoke(MethodUtil.java:68) 
      at com.sun.xml.internal.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:235) 
      at com.sun.xml.internal.ws.server.InvokerTube$2.invoke(InvokerTube.java:134) 
      at com.sun.xml.internal.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:73) 
      at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:1121) 
      at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:1035) 
      at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:1004) 
      at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:862) 
      at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:404) 
      at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:706) 
      at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:260) 
      at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:98) 
      at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:82) 
      at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79) 
      at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83) 
      at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:82) 
      at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:675) 
      at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79) 
      at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:647) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
      at java.lang.Thread.run(Thread.java:744) 
     Caused by: org.apache.derby.client.am.SqlException: The column position '2' is out of range. The number of columns for this ResultSet is '0'. 
      at org.apache.derby.client.am.ColumnMetaData.checkForValidColumnIndex(Unknown Source) 
      ... 41 more 
+0

您是否嘗試列出列,如「INSERT INTO USERUPLOADS(col1,col2,col3,col4)VALUES(?,?,?,?)」 –

+0

@MarkLeiber yes!我有。 –

+0

你在哪裏聲明你的PreparedStatement對象(stmt)?你是用不同的方法重複使用它嗎? –

回答

0

嘗試使用的prepareCall代替prepareStatement。我一直使用PreparedStatement pstmt = connection.prepareCall(<SQL>),它在很多數據庫上都有效(儘管我還沒有在Derby上嘗試過)。

+0

'prepareCall'用於調用存儲過程,而不是用於純DML語句。 'prepareStatement' **是**正確的API調用。 –