从Java Servlet

时间:2018-03-24 20:16:36

标签: java stored-procedures jdbc firebird jaybird

我有一个带有一些存储过程的Firebird数据库,我正在开发一个Java Web /数据库应用程序。我想知道是否可能以及如何从java类或Servlet调用这些存储过程。

示例:

  1. 存储过程deletep,用于从数据库中删除行
  2. 我的webapp Jsp / servlets
  3. 我想创建一个执行删除存储过程的链接

2 个答案:

答案 0 :(得分:1)

要在Java中执行Firebird存储过程,有几种方法。 JDBC默认方法类似于:

try (CallableStatement cstmt = connection.prepareStatement(
        "{call yourProcedure(?, ?, ?)}")) {
    cstmt.setString(1, "value1");
    cstmt.setString(2, "value2");
    cstmt.setString(3, "value3");
    cstmt.execute();
}

如果Firebird存储过程是可选择的(即:包含SUSPEND并且可以返回多行),则需要执行以下操作:

try (CallableStatement cstmt = connection.prepareStatement(
        "{call yourProcedure(?, ?, ?)}")) {
    cstmt.setString(1, "value1");
    cstmt.setString(2, "value2");
    cstmt.setString(3, "value3");
    try (ResultSet rs = cstmt.executeQuery()) {
        while(rs.next()) {
            // do something with result set rows ...
        }
    }
}

如果存储过程是可执行的并且不返回任何值,则这就足够了。如果存储过程可以使用(多个)返回列执行,则需要执行以下操作:

try (CallableStatement cstmt = connection.prepareStatement(
        "{call yourProcedure(?, ?, ?, ?, ?)}")) {
    cstmt.setString(1, "value1");
    cstmt.setString(2, "value2");
    cstmt.setString(3, "value3");
    cstmt.registerOutParameter(4, Types.VARCHAR);
    cstmt.registerOutParameter(5, Types.VARCHAR)
    cstmt.execute();

    String out1 = cstmt.getString(4);
    String out2 = cstmt.getString(5);
    // do something with result
}

或者,您可以将Firebird特定语法EXECUTE PROCEDURE yourProcedure(?, ?, ?)用于可执行存储过程和SELECT * FROM yourProcedure(?, ?, ?)

答案 1 :(得分:0)

使用Firebird JDBC驱动程序Jaybird可以实现这一点。

三个步骤:

  1. 创建一个DBUtil java类来存储查询:

    public static void deletePlan(Connection conn, CallableStatement 
            statement, String code, String rev, String mention) throws SQLException {  
    
        statement.setString(1, code);
        statement.setString(2, rev);
        statement.setString(3, mention);
        statement.execute();
    }
    
  2. 创建删除servlet

    protected void doGet(HttpServletRequest request, 
            HttpServletResponse response) throws ServletException, IOException {
        Connection conn = MyUtils.getStoredConnection(request);
    
        String code = request.getParameter("code");
        String rev = request.getParameter("revision");
        String mention = request.getParameter("mention");
        CallableStatement statement = null;
        try {
            statement = conn.prepareCall("execute procedure 
        DELETEP(?,?,?)");
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    
        String errorString = null;
    
        try {
            DBUtils.deletePlan(conn, statement, code, rev, mention);
        } catch (SQLException e) {
            e.printStackTrace();
            errorString = e.getMessage();
        } 
        .........
    
  3. 在jsp文件中创建删除链接

    <a href="${pageContext.request.contextPath}/deletePlan code=${plan.code}&revision=${plan.revision}&mention=${plan.mention}">Delete</a>