(存储过程在MySQL Workbench中完美运行并且还检索行)
这是我的代码:
ConnectionManager cm = new ConnectionManager();
java.sql.CallableStatement cstmt = null;
try
{
connect = cm.getConnection();
connect.createStatement();
String SQL = "{call getReportDetails ('"+ emailId +"','"+password+"')}";
cstmt = connect.prepareCall(SQL);
rs = cstmt.executeQuery(SQL);
int i = 0;
while(rs.next())
{
String element1 = rs.getString("description");
// -- some code --
}
}
catch(Exception e)
{
e.printStackTrace();
}
答案 0 :(得分:1)
删除createStatement
行。
并且(错误)不使用SQL作为executeQuery的参数。
(带有sql参数的重载版本是针对即时的,未准备好的语句版本。)
进一步关闭陈述和结果集; try-with-resources将在这里做得很好。
try (CallableStatement cstmt =
connect.prepareCall"{call getReportDetails (?, ?)}")) {
cstmt.setString(1, emailId);
cstmt.setString(2, password);
try (ResultSet rs = cstmt.executeQuery()) {
int i = 0;
while (rs.next())
{
String element1 = rs.getString("description");
// -- some code --
}
}
}
在存储函数的一般情况下,您可能需要自己指定结果:
cstmt.registerOutParameter(1, Types.INTEGER);
cstmt.registerOutParameter(2, Types.VARCHAR);
rs.getString(2);
但是你有一个ResultSet。
答案 1 :(得分:0)
CallableStatement cs = null;
try {
cs = connectionObject.prepareCall("{call getReportDetails (?, ?)}");
cs.setString(1, emailId);
cs.setString(2, password);
cs.execute();
ResultSet rs = cs.getResultSet();
while (rs.next()) {
System.out.println(rs.getString("columnLabel"));
}
}
catch (SQLException e) {
e.printStackTrace();
}
finally{
//close statements
}