ITeye博客

有返回值 存储过程 的 调用

hwpok · 2007-12-18 20:03 · Java · Markdown

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

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();
            }
        }
    }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。