java - Java 错误调用 Sql Server

标签 java sql-server

我在 Java 类中有以下代码:

CallableStatement stmt = con.prepareCall({ call myInsert( ?, ?, ?)});
stmt.setString(1, myTO.getField1());
stmt.setString(2, myTO.getField2());
stmt.setString(3, myTO.getField3());
stmt.executeUpdate();

数据库中的myInsert定义为:

IF OBJECT_ID ( 'myInsert', 'P' ) IS NOT NULL   
DROP PROCEDURE myInsert;  

GO 

CREATE PROCEDURE [myInsert]
@Field1 as uniqueidentifier,
@Field2 as varchar(120),
@Field3 as varchar(120)
@myId UNIQUEIDENTIFIER OUTPUT

AS

SET @myId = NEWID()

INSERT INTO myTable
     (myID, Field1, Field2, Field3) 
    VALUES ( @myID, @Field1, @Field2, @Field3 )

但是,调用失败并出现以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Procedure or function 'myInsert' expects parameter '@myId', which was not supplied.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:348)
    at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)
    at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)

我做错了什么?我该如何解决这个问题?

感谢您的阅读!

最佳答案

我认为您缺少输出参数。

CallableStatement stmt = con.prepareCall("{ call myInsert(?, ?, ?, ?)}");
stmt.setString(1, myTO.getField1());
stmt.setString(2, myTO.getField2());
stmt.setString(3, myTO.getField3());
stmt.registerOutParameter(4, java.sql.Types.VARCHAR);
stmt.executeUpdate();
System.out.println(stmt.getString(4)); // <-- print @myId

关于java - Java 错误调用 Sql Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44274214/

相关文章:

java - Java 中出现很多错误,对角矩阵问题

抽象类的 Java 反射错误

java计算两个时间戳之间的时间

sql-server - 为什么触发器在使用 'inserted' 表中的字段时尝试插入 NULL 值?

sql - 使用子句 IF 和 IN 获取值

sql-server - 英文和中文文本的文件编码

java - 无法将字符串值从一个 fragment 传递到另一个 fragment

java - 有没有办法使用 Matcher 来 peek() 或返回

javascript - SQL Server 实时推送通知到 Node.JS

c# - FirebirdSql (C#) 比在 FlameRobin 中执行脚本慢