快速提问,为什么我在使用以下代码时会遇到以下异常;
请注意,这是使用Java使用存储过程调用MS SQL Server 2008 R2。
Connection con = getDataSource().getConnection();
CallableStatement cs = con.prepareCall("countrySelect(?)");
cs.setObject(1, "GB");
cs.execute();
结果;
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:322)
这个“@ P0”来自哪里?这是Java内存参考吗?为什么它没有正确转换为SQL类型?
如果可以,我会得到同样的结果;
cs.setString(1, "GB");
存储的Proc代码如下(不是我写的);
DECLARE @dbid INTEGER;
DECLARE @dbName NVARCHAR(125);
DECLARE @prcName NVARCHAR(125);
SET @dbid = DB_ID();
SET @dbName = DB_NAME();
SET @prcName = 'usp_countrySelect'
IF dbo.ufn_Admin_countryExists(@countryCode) = 1
SELECT countryCode
, countryName
, nationalVerificationPrice
, vatCharged
, VATPercentage
, sortingPrecedence
FROM dbo.country
WHERE countryCode = @countryCode ;
ELSE
RAISERROR ( N'ERROR: A country with countryCode %s does not exist - proc Name:%s, database ID:%i, the database name is: %s.'
, 16 -- Severity
, 1 -- State
, @countryCode -- First subbed argument
, @prcName -- Second subbed argument
, @dbid -- Third subbed argument
, @dbName -- Fourth subbed argument
);
END
答案 0 :(得分:0)
想出来,如果你问我,那么对于一个不显眼的问题就是错误的错误;
CallableStatement cs = con.prepareCall("countrySelect(?)");
不起作用,而是使用;
CallableStatement cs = con.prepareCall("{call countrySelect(?)}");
区别在于“call”和括号{}括号。