I am trying to run a sql stored procedure in java that updates an admin's information based on an input "AReg_ID." Currently when I run the code I run into this error:
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)
Here is the stored procedure made in 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*/
Here is the java code trying to run the procedure:
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();
}
}
}
Can you update
CallableStatement stmtadm= conn.prepareCall("{CALL UpdateAdmin('Adm_Fname','Adm_Lname', 'Adm_Address','Adm_Email','Adm_Phone','AReg_ID')}");
to
CallableStatement stmtadm= conn.prepareCall("CALL UpdateAdmin(?,?,?,?,?,?)");
and see if it working or not?