我需要从Java调用SQL Server存储过程。这是存储过程:
DECLARE @t_certificate v.vt_VidaDollarsCertificates
INSERT INTO @t_certificate VALUES(1,710335,'FFRLODOLAR',363)
INSERT INTO @t_certificate VALUES(1,710335,'FFRLODOLAR05',363)
INSERT INTO @t_certificate VALUES(1,710335,'FFRLODOLAR06',363)
EXEC v.vprdl_CertificateInsert @t_certificate
我所做的是创建一个原始查询,然后使用PreparedStatement添加信息。
我像这样创建原始查询:
StringBuilder query = new StringBuilder();
query.append(
"DECLARE @t_certificate v.vt_VidaDollarsCertificates;"
+ "INSERT @t_certificate VALUES(?,?,?,?)");
for (int i = 0; i < arrCertificates.size() - 1; ++i) {
query.append(",(?,?,?,?) ");
}
query.append("; EXEC v.vprdl_CertificateInsert @t_certificate");
这是形成的原始查询
DECLARE @t_certificate v.vt_VidaDollarsCertificates;INSERT @t_certificate VALUES(?,?,?,?),(?,?,?,?) ,(?,?,?,?) ; EXEC v.vprdl_CertificateInsert @t_certificate
然后我创建了PreparedStatement。
PreparedStatement preparedStmt = con.prepareStatement(query.toString());
for (int i = 0; i < arrCertificates.size(); ++i) {
preparedStmt.setInt(1, arrCertificates.get(i).getCertificateTypeID());
preparedStmt.setInt(2, arrCertificates.get(i).getContratoID());
preparedStmt.setString(3, arrCertificates.get(i).getFolio());
preparedStmt.setInt(4, Integer.parseInt(arrCertificates.get(i).getID()));
}
之后我使用executeUpdate。
preparedStmt.executeUpdate();
ResultSet rs = preparedStmt.getGeneratedKeys();
但它不起作用,它会引发异常
未为参数编号设置值
答案 0 :(得分:2)
您似乎为prepapred语句设置了错误的索引,因为在所有迭代中您都设置了相同的值:
int j = 1;
for (int i = 0; i < arrCertificates.size(); ++i) {
preparedStmt.setInt(j++, arrCertificates.get(i).getCertificateTypeID());
preparedStmt.setInt(j++, arrCertificates.get(i).getContratoID());
preparedStmt.setString(j++, arrCertificates.get(i).getFolio());
preparedStmt.setInt(j++, Integer.parseInt(arrCertificates.get(i).getID()));
}
这似乎不正确,相反你可以使用:
VALUES(?, ?, ?, ?),(?, ?, ?, ?) ,(?, ?, ?, ?)
1 2 3 4 5 6 7 8 9 10 11 12
因此对于3个输入它应该给你:
@Bean
lateinit var test: TestBean