我正在尝试从我的存储过程中获取结果,我得到了ERROR: Incorrect syntax near '{'.
. 我看到很多例子对 a 使用相同的东西,sql string
但我不知道为什么会出现错误..我有相同数量的参数...
当我像这样执行我的数据库的存储过程时
exec rptGetAssetbyLocation '2122
' 我得到了我的 5 列
如何使用 Callable Statement 返回结果?我做错了registerOutParameter
什么?
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver:...";
String username = "username";
String password = "password";
sb.append("<table id=\"examplee\" border=1 >"
+ "<tr><td>ID</td><td>TAG</td><td>NAME</td>"
+ "<td>MODEL</td><td>SERIAL NUMBER</td></tr>");
String query = "{exec rptGetAssetbyLocation(?,?,?,?,?)}";
Connection conn = DriverManager.getConnection(url, username, password);
CallableStatement stmt = conn.prepareCall(query);
stmt.registerOutParameter(1, java.sql.Types.INTEGER);
stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
stmt.registerOutParameter(3, java.sql.Types.VARCHAR);
stmt.registerOutParameter(4, java.sql.Types.VARCHAR);
stmt.registerOutParameter(5, java.sql.Types.VARCHAR);
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
int id = rs.getInt("ID");
String tag = rs.getString("tag");
String name = rs.getString("name");
String model = rs.getString("model");
String serNum = rs.getString("serialNumber");
sb.append("<tr><td>" + id + "</td>"
+ "<td>" + tag + "</td>"
+ "<td>" + name + "</td>" +
"<td>" + model + "</td>" +
"<td>" + serNum + "</td></tr>");
}
sb.append("</table>");
}
catch(Exception e)
{
sb.append("<h1>ERROR: " + e.getMessage() + "</h1>");
}
sb.append("</body></html>");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(sb);
}