How can I execute a stored procedure in kotlin? I tried this:
val pStmt = con?.prepareCall("\\{call master.sys.fn_varbintohexsubstring(0,HASHBYTES('SHA1', CONVERT" + "(varchar(255), ${user.clearPassword})+'DEL'+${user.salt}),1,0)\\}")
pStmt?.registerOutParameter("result", Types.VARCHAR)
val cursor = pStmt?.executeQuery()
if (cursor != null) {
while (cursor.next()) {
val result = cursor.getString("result")
}
}
Exception:
W/System.err: java.sql.SQLException: Invalid JDBC call escape at line position 6.
Basically it should work the same as in Java code.
I can guess that user.clearPassword is probably unquoted string value, and one should quote it to pass to CONVERT, like: ...CONVERT(varchar(255), '${user.clearPassword}')...
Well, I’m not an expert in particular SQL dialect. So we have: ...prepareCall("{call master.sys.fn_varbintohexsubstring(0,HASHBYTES('SHA1', CONVERT" + "" + "(varchar(255), '${user.clearPassword}')+'DEL'+'${user.iSalt}'),1,0)}")
Let’s replace all Kotlin expressions with corresponding String values and concat them: