尝试执行存储过程时收到“com.microsoft.sqlserver.jdbc.SQLServerException:索引1超出范围。”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试执行存储过程时收到“com.microsoft.sqlserver.jdbc.SQLServerException:索引1超出范围。”相关的知识,希望对你有一定的参考价值。
我试图在java中运行一个sql存储过程,它根据输入“AReg_ID”更新管理员的信息。目前,当我运行代码时,我遇到了这个错误:
com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:228)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:1124)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:1138)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1683)
at update_admin.main(update_admin.java:21)
这是MSSQL中的存储过程:
CREATE Procedure UpdateAdmin
@Adm_Fname varchar(255),
@Adm_Lname varchar(255),
@Adm_Address varchar(255),
@Adm_Email varchar(255),
@Adm_Phone varchar(255),
@AReg_ID int
AS
UPDATE Admin
SET Adm_Fname = @Adm_FName, Adm_LName = @Adm_LName, Adm_Address = @Adm_Address, Adm_Email = @Adm_Email, Adm_Phone = @Adm_Phone
FROM Admin A
INNER JOIN Admin_Registration_Form ARF
ON ARF.Adm_ID=A.Adm_ID
WHERE ARF.AReg_ID= @AReg_ID/* Reg_ID value for the admin being updated*/
这是尝试运行该过程的java代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.sql.Statement;
public class update_admin {
public static void main(String[] args){
Connection conn=null;
Statement stmt=null;
String connectionURL=
"jdbc:sqlserver://localhost:1433;"
+"database=test_2;"
+"user=user;"
+"password=password;";
try {
conn = DriverManager.getConnection(connectionURL);
CallableStatement stmtadm= conn.prepareCall("{CALL UpdateAdmin('Adm_Fname','Adm_Lname', 'Adm_Address','Adm_Email','Adm_Phone','AReg_ID')}");
stmtadm.setString(1, "First Name");
stmtadm.setString(2, "Last Name");
stmtadm.setString(3, "1324 Address Street");
stmtadm.setString(4, "admin@admin.com");
stmtadm.setString(5,"123-456-789");
stmtadm.setInt(6,4 );
}
catch (SQLException e){
System.out.println("unable to make connection with db");
e.printStackTrace();
}
}
}
答案
你能更新吗?
CallableStatement stmtadm= conn.prepareCall("{CALL UpdateAdmin('Adm_Fname','Adm_Lname', 'Adm_Address','Adm_Email','Adm_Phone','AReg_ID')}");
至
CallableStatement stmtadm= conn.prepareCall("CALL UpdateAdmin(?,?,?,?,?,?)");
看看它是否有效?
以上是关于尝试执行存储过程时收到“com.microsoft.sqlserver.jdbc.SQLServerException:索引1超出范围。”的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在从 Java 批量执行 PostgreSQL 存储过程时收到错误消息,提示“未预期结果”?