有返回值的存储过程的调用方法如下:

public class SenderPrepareCallProcedure
{

public String testP(int statusWillSending, int statusNowSending, int fixedSending)
{
String taskId = null;
Connection connection = null;
CallableStatement proc = null;

try
{
connection = DBPool.getConnection();
proc = connection.prepareCall("{call backtrack_taskId(?,?,?,?)}");
proc.setInt(1, statusWillSending);
proc.setInt(2, statusNowSending);
proc.setInt(3, fixedSending);
proc.registerOutParameter(4, Types.INTEGER);
proc.execute();
taskId = String.valueOf(proc.getInt(4));
//System.out.println("taskId is: " + taskId);

} catch (Exception e)
{
e.printStackTrace();

} finally
{
this.freeSource(proc, connection);
}

if(taskId != null && !taskId.equals("0"))
{
return taskId;
}
return null;
}


private void freeSource(CallableStatement proc, Connection connection)
{

if (proc != null)
{

try
{
proc.close();

} catch (Exception e)
{
e.printStackTrace();
}
}

if (connection != null)
{

try
{
connection.close();

} catch (Exception e)
{
e.printStackTrace();
}
}
}
}

public class SenderPrepareCallProcedure
{
public String testP(int statusWillSending, int statusNowSending, int fixedSending)
{
String taskId = null;
Connection connection = null;
CallableStatement proc = null;
try
{
connection = DBPool.getConnection();
proc = connection.prepareCall("{call backtrack_taskId(?,?,?,?)}");
proc.setInt(1, statusWillSending);
proc.setInt(2, statusNowSending);
proc.setInt(3, fixedSending);
proc.registerOutParameter(4, Types.INTEGER);
proc.execute();
taskId = String.valueOf(proc.getInt(4));
//System.out.println("taskId is: " + taskId);
} catch (Exception e)
{
e.printStackTrace();
} finally
{
this.freeSource(proc, connection);
}
if(taskId != null && !taskId.equals("0"))
{
return taskId;
}
return null;
}

private void freeSource(CallableStatement proc, Connection connection)
{
if (proc != null)
{
try
{
proc.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
if (connection != null)
{
try
{
connection.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
本文介绍了一个Java类,该类用于调用带有返回值的存储过程。通过实例展示了如何使用`CallableStatement`准备并执行存储过程,同时处理输入参数及获取输出结果。
1160

被折叠的 条评论
为什么被折叠?



