收到“ com.microsoft.sqlserver.jdbc.SQLServerException:索引1超出范围。”尝试执行存储过程时

时间:2019-04-11 01:27:37

标签: java sql-server stored-procedures jdbc callable-statement

我试图在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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以更新

CallableStatement stmtadm= conn.prepareCall("{CALL UpdateAdmin('Adm_Fname','Adm_Lname', 'Adm_Address','Adm_Email','Adm_Phone','AReg_ID')}");

CallableStatement stmtadm= conn.prepareCall("CALL UpdateAdmin(?,?,?,?,?,?)");

看看它是否有效?