我试图获取结果集的结果,我将第二个参数注册为INTEGER
。根据我的阅读,我应该先使用结果集检索对象。
代码
try (Connection myConn = DBUtil.connect();
CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
{
myFirstCs.setString(1, searchSection);
myFirstCs.registerOutParameter(2, Types.INTEGER);
myFirstCs.executeUpdate();
String name = null;
System.out.println(myFirstCs.getObject(2));
ResultSet myRs = null;
myRs = (java.sql.ResultSet)myFirstCs.getObject(2);
while (myRs.next()) // Retrieve result set rows
{
name=myRs.getString(1);
System.out.print("Section Name: "+name);
}
myRs.close();
这是检索结果集的正确方法吗?我尝试使用try with resource尝试此方法。我在这里错过了一些事情ResultSet myRs = myFirstCs.getObject(2, type)
我不知道我会在第二个参数中添加什么内容?
仅限样本
try (ResultSet myRs = myFirstCs.getObject(2, type))//What should I put here?
{
while (myRs.next())
{
name = myRs.getString(1);
System.out.print("Section Name: "+name);
}//end of while
}
随意发表评论。任何帮助将不胜感激。谢谢。
答案 0 :(得分:0)
@Mia以下两个答案,我认为其中一个应该有效 -
try (Connection myConn = DBUtil.connect();
CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
{
myFirstCs.setString(1, searchSection);
myFirstCs.registerOutParameter(2, Types.INTEGER);
myFirstCs.executeUpdate();
String name = myFirstCs.getObject(2);
System.out.print("Section Name: "+name);
}
OR
try (Connection myConn = DBUtil.connect();
CallableStatement myFirstCs = myConn.prepareCall("{call getSECTION_NAME(?,?)}"))
{
myFirstCs.setString(1, searchSection);
myFirstCs.registerOutParameter(2, Types.INTEGER);
myFirstCs.executeUpdate();
String name = null;
System.out.println(myFirstCs.getObject(2));
ResultSet myRs = null;
myRs = myFirstCs.getResultSet();
while (myRs.next()) // Retrieve result set rows
{
name=myRs.getString(1);
System.out.print("Section Name: "+name);
}
myRs.close();