2016-10-16 19 views
0

我有一個查詢應該獲取2個「tinyint」參數,以便運行並將其包裝爲存儲過程。嘗試調用存儲過程時獲取索引超出範圍

在SQL存儲過程中去如下:

Create Procedure query3 
@X tinyint = null, 
@Y tinyint = null 
AS 
BEGIN 
SELECT T.tressure_No as 'Treasure Number', T.tressure_Name as 'Treasure Name' , COUNT(DISTINCT U.user_detail_Name) as numOfUsers 

FROM dbo.tblUser AS U INNER JOIN dbo.tblTressure AS T 
    ON U.country_id_user = T.country_Hidden_Code 
    INNER JOIN dbo.tblListContains AS LC 
    ON T.tressure_No = LC.tressure_No 
    AND LC.user_name_detail = U.user_detail_Name 

WHERE U.user_detail_Name IN(SELECT LC.user_name_detail 
          FROM dbo.tblListContains AS LC 
          WHERE T.tressure_No = LC.tressure_No AND LC.user_name_detail NOT IN (SELECT LF.user_detail_Name 
                            FROM dbo.tblLookingFor AS LF 
                            WHERE LF.tressure_No = T.tressure_No 
                            AND LF.user_detail_Name = U.user_detail_Name) 
          GROUP BY LC.user_name_detail, LC.tressure_No 
          HAVING COUNT(LC.num_Of_List_User)>[email protected]) 


GROUP BY T.tressure_No, T.tressure_Name 
Having COUNT(DISTINCT U.user_detail_Name)>[email protected]; 
END 

當我在2008年SQLEXPRESS服務器R2管理程序測試存儲過程只是正常工作和內側返回的結果的預期查詢。 但是當我試圖從我的JAVA GUI叫它它去地獄...... 這裏是Java代碼(此方法被稱爲我的GUI內,以填補ResultSet中的表):

public ResultSet getQuery3Results(byte numOfUsers, byte numberOfLists){ 
     ResultSet rs = null; 
     try{ 
      Class.forName(server); 
      System.out.println("here1"); 
      conn = DriverManager.getConnection(LINK); 
      String callStatement = "{call query3(?,?)}"; 
      pst.setByte(1, numOfUsers); 
      pst.setByte(2, numberOfLists); 
      System.out.println("here2"); 
      pst = conn.prepareCall(callStatement); 
      rs = pst.executeQuery(); 
      return rs; 
     } 
     catch (SQLException ex){ 
      ex.printStackTrace(); 
      rs = null; 
      return rs; 
     } 
     catch (ClassNotFoundException e){ 
      e.printStackTrace(); 
     } 
     return rs; 
    } 

我得到這個異常堆棧跟蹤:

com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range. 
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source) 
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(Unknown Source) 
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setByte(Unknown Source) 
    at View.ViewLogic.getQuery3Results(ViewLogic.java:1170) 
    at View.ViewLogic.handleGenerateQuery3Button(ViewLogic.java:1641) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.reflect.misc.Trampoline.invoke(Unknown Source) 
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source) 
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source) 
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Node.fireEvent(Unknown Source) 
    at javafx.scene.control.Button.fire(Unknown Source) 
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.process(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source) 
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.notifyMouse(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

我不知道我在做什麼錯,建議的和領導的歡迎:d

湯姆

+0

2 TINYINT參數是不一樣的。 –

+0

不能傳遞從Java原因TINYINT這種類型不存在於Java中,但如果通過使用setByte()方法一個字節,它在sql-server中將字節的值轉換爲tinyint。這來自剛讀取setByte()方法的描述。 – Tom

回答

3

此行需要

pst = conn.prepareCall(callStatement); 

此外,你必須確保你根據微軟的映射規則傳遞適當的數據類型設定值之前發生。爲兩個字節中傳遞https://msdn.microsoft.com/en-us/library/ms378878(v=sql.110).aspx

conn = DriverManager.getConnection(LINK); 
String callStatement = "{call query3(?,?)}"; 
pst.setShort(1, numOfUsers); 
pst.setShort(2, numberOfLists); 
+0

Thx,幫助! – Tom