简体   繁体   English

从Java中的存储过程获取新的记录id返回值

[英]Getting a new record id return value from a stored procedure in Java

I need to create a new record in a stored procedure and return the ID of the inserted record. 我需要在存储过程中创建一个新记录,并返回插入记录的ID。 I'm sure this is quite simple but I don't know the right way to do this... 我敢肯定这很简单,但我不知道正确的方法...

I've created a simple stored procedure that requires 3 parameters, inserts a record and returns a value of the inserted record: 我创建了一个简单的存储过程,该过程需要3个参数,插入一条记录并返回插入记录的值:

ALTER PROCEDURE [dbo].[BronzeLabsCreateServiceEquipment]
    @companyID int = null,
    @manufacturerID int = null,
    @modelID int = null,
    @serialNumber varchar(255) = '',
    @machine varchar(255) = '',
    @location varchar(255) = ''
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO ServiceEquipment(CompanyID, ManufacturerID, ModelID, SerialNumber, Machine, Location)
    VALUES (@companyID, @manufacturerID, @modelID, @serialNumber, @machine, @location)

    Declare @new_identity int;
    SELECT @new_identity = SCOPE_IDENTITY()
    return @new_identity

END

Then I try to call this in Java 然后我尝试用Java来称呼它

String queryStringAlt = "CALL dbo.BronzeLabsCreateServiceEquipment(?, ?, ?, ?, ?, ?)";
CallableStatement cs = conn.prepareCall(queryStringAlt); 

    cs.setString(1, null);
    cs.setString(2, null);
    cs.setString(3, null);
    cs.setString(4, "new123-1");
    cs.setString(5, "new123-2");
    cs.setString(6, "new123-3");

ResultSet rs = cs.executeQuery();

...however the prepared statement doesn't return a resultset so that last line doesn't work. ...但是,准备好的语句不会返回结果集,因此最后一行不起作用。 What does it return and how do I get it? 它返回什么,如何获得? Or am I doing the wrong thing here - should I be using output parameters (which I tried but had a syntax issue I couldn't get to the bottom of). 还是我在这里做错了-我应该使用输出参数(我曾尝试过,但是遇到语法问题,我无法深入了解)。

Thanks for your help. 谢谢你的帮助。 I hadn't thought of putting the identity into a resultset, So the new code is: 我没有考虑过将身份放入结果集中,因此新代码是:

Stored procedure 存储过程

ALTER PROCEDURE [dbo].[BronzeLabsCreateServiceEquipment]
    @companyID int = null,
    @manufacturerID int = null,
    @modelID int = null,
    @serialNumber varchar(255) = '',
    @machine varchar(255) = '',
    @location varchar(255) = ''
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO ServiceEquipment(CompanyID, ManufacturerID, ModelID, SerialNumber, Machine, Location)
    VALUES (@companyID, @manufacturerID, @modelID, @serialNumber, @machine, @location)

    SELECT SCOPE_IDENTITY() AS equipmentID

END

Java code Java代码

String queryStringAlt = "CALL dbo.BronzeLabsCreateServiceEquipment(?, ?, ?, ?, ?, ?)";
CallableStatement cs = conn.prepareCall(queryStringAlt); 

    cs.setString(1, null);
    cs.setString(2, null);
    cs.setString(3, null);
    cs.setString(4, "new123-1");
    cs.setString(5, "new123-2");
    cs.setString(6, "new123-3");

ResultSet rs = cs.executeQuery();
if(rs.next()) {
    System.out.println("Equipment ID: " + rs.getString("equipmentID"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从存储过程返回值 - How to return value from stored procedure 从Java调用存储类型为Record的参数的存储过程 - Calling a stored procedure from java that accepts parameter of type Record 我如何从sqlserver存储过程的return语句获取值到Java? - how i get a value from a return statement of sqlserver stored procedure to java? 使用 Java 和 Hibernate 从存储过程中获取返回值(标量),而不是结果表 - Get return value (scalar), not result table, from stored procedure using Java and Hibernate 从 Java 方法(在 DAO 层中)调用存储过程(将记录插入 lsa_user_info 表)时出现 PSQLException - Getting PSQLException while calling a Stored Procedure (which inserts record into lsa_user_info table) from Java method ( which is in DAO Layer) 无法通过Java检索存储过程返回值 - Unable to retrieve Stored Procedure Return value through Java 从 java 调用 Postgres 存储过程,要返回的表 - Postgres stored procedure call from java, table to return 如何返回ID插入存储过程+ Mybatis - How to return Id insert stored procedure + mybatis 坚持使用带有返回ID的存储过程 - Persist using stored procedure with return id 使用java从sql server存储过程中检索返回的值 - Retrieve the returned value from sql server stored procedure using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM