如何从preparestatement执行storedprocedure

时间:2016-08-03 16:38:41

标签: android sql-server stored-procedures

我如何执行我的存储过程。 当我直接从sql执行它时,该过程是有效的,但是当我从平板电脑上执行它时它不起作用

String itemshelf = DBshelf;// edittext for putting in values on set
String itemcard = DBcard;// edittext for putting in values on where
PreparedStatement preparedStatement = con.prepareStatement("EXEC [dbo].[spUpd_Location]");
                        preparedStatement.executeUpdate();

我的存储过程

ALTER PROCEDURE [dbo].[spUpd_Location]
   @itemshelf nvarchar(1000)=NULL
 , @itemcard nvarchar(1000)=NULL
 AS
SET NOCOUNT ON
UPDATE PS
SET [ShelfNumber]=@itemshelf
FROM [file].[ItemPart] PS
JOIN [file].[Item] P ON P.[id] = PS.[id]
where [ItemNumber]=@itemcard

1 个答案:

答案 0 :(得分:1)

您的存储过程需要两个参数,您需要为准备好的语句提供这些参数。您的SQL需要占用参数的占位符,并且需要在PreparedStatement对象上设置参数。您的代码看起来像是:

String itemshelf = DBshelf;// edittext for putting in values on set
String itemcard = DBcard;// edittext for putting in values on where
PreparedStatement preparedStatement = con.prepareStatement("EXEC [dbo].[spUpd_Location] ?, ?;");
preparedStatement.setString(1, itemshelf);
preparedStatement.setString(2, itemcard);
preparedStatement.executeUpdate();