Java 尝试调用存储过程时索引超出范围

Java 尝试调用存储过程时索引超出范围,java,sql,sql-server,stored-procedures,resultset,Java,Sql,Sql Server,Stored Procedures,Resultset,我有一个查询,应该得到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 numO

我有一个查询,应该得到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)>=@Y)


GROUP BY T.tressure_No, T.tressure_Name
Having COUNT( DISTINCT U.user_detail_Name)>=@X;
END
当我在2008年SQLEXPRESS服务器R2管理器中测试存储过程时,它工作正常,并且返回结果中的查询符合预期。 但是,当我试图从JAVA GUI调用它时,它会下地狱。。。 以下是java代码(在我的gui中调用此方法以使用ResultSet填充表):

我得到以下异常堆栈跟踪:

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


Tom

在设置值之前,需要执行此行

pst = conn.prepareCall(callStatement);
此外,还必须确保根据microsoft映射规则传递适当的数据类型


2个tinyint参数与传入的两个字节不同。您无法从java传递tinyint,因为java中不存在此类型,但如果使用setByte()方法传递一个字节,它会将该字节的值转换回sql server中的tinyint。这是通过阅读setByte()方法description.Thx得到的,这很有帮助!!
pst = conn.prepareCall(callStatement);
conn = DriverManager.getConnection(LINK);
String callStatement = "{call query3(?,?)}";
pst.setShort(1, numOfUsers);
pst.setShort(2, numberOfLists);