UnsignedByte is defined to be 0-255. .getValue() returns Short.
pts1, pts2, pts3 are ALL null or ALL not null (input checks beforehand guarantee).
addlevel(...)'s CallableStatement WORKS for these scenarios:
- (gameNo, levelNo, null, null, null, null)
- (gameNo, levelNo, pts1, pts2, pts3, null)
- (gameNo, levelNo, pts1, pts2, pts3, typeNo)
but throws
com.microsoft.sqlserver.jdbc.SQLServerException: The index 6 is out of
range.
on:
(gameNo, levelNo, null, null, null, typeNo)
the right statement called is marked with a comment in java code segment.
.......................................................................................................................................
SP:
CREATE PROCEDURE addLevel
@gameNo int,
@levelNo int,
@pts1 int = NULL,
@pts2 int = NULL,
@pts3 int = NULL,
@typeNo tinyint = NULL
AS
BEGIN
SET NOCOUNT ON
INSERT INTO tblLevel (gameNo, levelNo, pointsForStar1, pointsForStar2, pointsForStar3, typeNo)
VALUES (@gameNo, @levelNo, @pts1, @pts2, @pts3, @typeNo)
END
GO
java:
public boolean addLevel(int gameNo, int levelNo, Integer pts1star, Integer pts2star, Integer pts3star, UnsignedByte typeNo){
try{
CallableStatement cstmt;
if (pts1star != null){
if (typeNo != null)
cstmt = database.getConnection().prepareCall("{call dbo.addLevel(?,?,?,?,?,?)}");
else
cstmt = database.getConnection().prepareCall("{call dbo.addLevel(?,?,?,?,?)}");
cstmt.setInt("pts1", pts1star);
cstmt.setInt("pts2", pts2star);
cstmt.setInt("pts3", pts3star);
}
else{
if (typeNo != null)
cstmt = database.getConnection().prepareCall("{call dbo.addLevel(?,?,?)}"); //<<<<<--------------- THIS ONE
else
cstmt = database.getConnection().prepareCall("{call dbo.addLevel(?,?)}");
}
cstmt.setInt("gameNo", gameNo);
cstmt.setInt("levelNo", levelNo);
if (typeNo != null)
cstmt.setShort("typeNo", typeNo.getValue());
cstmt.executeUpdate();
return true;
}
catch(SQLException e){
e.printStackTrace();
return false;
}
}
See Question&Answers more detail:
os