简体   繁体   English

从Java Servlet调用Firebird存储过程

[英]Calling Firebird Stored procedure from Java Servlet

I have a Firebird Database with some stored procedure, and I am developing a Java web/database application. 我有一个带有某些存储过程的Firebird数据库,并且正在开发Java Web /数据库应用程序。 I want to know if it is possible and how to call those stored procedures from a java Class or a Servlet. 我想知道是否有可能,以及如何从Java类或Servlet调用那些存储过程。

example: 例:

  1. Stored procedure deletep , used to delete a row from database 存储过程deletep ,用于从数据库中删除行
  2. my webapp Jsp/servlets 我的webapp Jsp / servlet
  3. I want to create a link that execute my delete stored procedure 我想创建一个执行我的删除存储过程的链接

To execute a Firebird stored procedure in Java, there are several ways. 要用Java执行Firebird存储过程,有几种方法。 The JDBC-default method would be something like: JDBC-default方法类似于:

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

If the Firebird stored procedure was selectable (that is: contains SUSPEND and can return multiple rows), you would need to do something like: 如果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 ...
        }
    }
}

This is sufficient if the stored procedure is executable and doesn't return any values. 如果存储过程是可执行的并且不返回任何值,这就足够了。 If the stored procedure is executable with (multiple) return columns, you'd need to something like: 如果存储过程是可执行的(带有(多个)返回列),则需要执行以下操作:

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
}

Alternatively, you could use the Firebird specific syntax EXECUTE PROCEDURE yourProcedure(?, ?, ?) for executable stored procedures and SELECT * FROM yourProcedure(?, ?, ?) . 或者,您可以使用Firebird特定的语法EXECUTE PROCEDURE yourProcedure(?, ?, ?)用于可执行存储过程,并使用SELECT * FROM yourProcedure(?, ?, ?)

This is possible using Jaybird, the Firebird JDBC driver. 使用Firebird JDBC驱动程序Jaybird可以做到这一点。

Three steps: 三个步骤:

  1. Create a DBUtil java class to stock the query : 创建一个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. Create the delete servlet 创建删除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. Create the delete link in the jsp file 在jsp文件中创建删除链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM