简体   繁体   English

在 Java 中调用存储过程的简单方法

[英]Easy way to call stored procedure in Java

When call a stored procedure I use this code:调用存储过程时,我使用以下代码:

    connection = getConnection();
    stmt = connection.prepareCall("{call MPLOGIN (?, ?, ?, ?, ?, ?)}");
    stmt.setString("VUSCD", uscd);
    stmt.setString("VPWD", pwd);
    stmt.setString("VPCSQ", pcsq);
    stmt.setString("VHWID", hwid);
    stmt.registerOutParameter("VLOGID", OracleTypes.VARCHAR);
    stmt.registerOutParameter("VKQ", OracleTypes.VARCHAR);
    stmt.execute();
    String vlogid = stmt.getString("VLOGID");
    String vkq = stmt.getString("VKQ");

write this boring wrapper for few procedure is not problem but if there are hundreds of procedure, it is really a nightmare Is there any easier way to call store procedure than this way?为少数程序编写这个无聊的包装器是没有问题的,但是如果有数百个程序,那真是一场噩梦。有没有比这种方式更简单的方法来调用存储程序? Edit: I think a code generator which use the procedure's parameters from DB is the elagant way but I google for nothing in java编辑:我认为使用数据库中的程序参数的代码生成器是一种优雅的方式,但我在 java 中一无所获

You could, maybe create a generic wrapper, something along these lines: 你可以,也许创建一个通用的包装器,沿着这些方向:

public Map<String, String> SPWrapper(String call, Map<String, String> inParams, Map<String, OracleTypes> outParams)
{
    connection = getConnection();
    try 
    {
        stmt = connection.prepareCall(call);
        for(String inParam : inParams.keys())
        {
            stmt.setString(inParam, inParams.get(inParam));
        }
        for(String outParam : outParams.keys())
        {
            stmt.registerOutParameter(outParam, outParams.get(outParam));
        }

        stmt.execute();

        Map<String,String> results = new HashMap<String, String>();
        for(String outParam : outParams.keys())
        {
            results.put(outParam, stmt.getString(outParam));
        }

        return results;
    }
    catch (Exception e)
    {
        //LOG Exception
        return new HashMap<String, String>();
    }
    finally
    {
        connection.close();   //Do not leave connections open.
    }
}

You would still need to pass in the call and declare the variables, but at least you now have one generic wrapper to handle all your calls. 您仍然需要传递call并声明变量,但至少您现在有一个通用的包装器来处理所有调用。

I like to use the MyBatis data mapper framework for such problems. 我喜欢使用MyBatis数据映射器框架来解决这些问题。 An extensive example for working with MyBatis and stored procedures can be found at http://loianegroner.com/2011/03/ibatis-mybatis-working-with-stored-procedures/ 有关使用MyBatis和存储过程的详细示例,请访问http://loianegroner.com/2011/03/ibatis-mybatis-working-with-stored-procedures/

There isn't any other way. 没有任何其他方式。 Yes, it is boring, but number of procedures is finite. 是的,这很无聊,但程序数量是有限的。 This procedures are like methods in Java, so you should operate with them in prescribed rules. 这个过程就像Java中的方法一样,所以你应该按规定的规则操作它们。 Only one convenient thing you could do - create special class, which will contain wrapped methods for each procedure. 你只能做一件方便的事 - 创建特殊的类,它将包含每个过程的包装方法。 In this case it will be more elegantly to call them in business code, something like this: 在这种情况下,在业务代码中调用它们会更优雅,如下所示:

String[] result = DAO.MPLOGIN(uscd, pwd, pcsq, hwid);

But inside this method you have to copy code, that you mentioned above. 但是在这个方法中你必须复制上面提到的代码。

In database client a stored procedure such as myproc(10,20) is called just by statement select myproc(10,20); 在数据库客户端中,只需通过语句select myproc(10,20)调用存储过程,如myproc(10,20);

So in your JDBC program you can just do : connection = getConnection(); 所以在JDBC程序中你可以这样做:connection = getConnection(); stmt = connection.createStatement(); stmt = connection.createStatement(); stmt.executeQuery("select myproc(10,20)"); stmt.executeQuery(“select myproc(10,20)”);

If procedure is returning something then put it in a ResultSet 如果procedure返回了某些内容,则将其放入ResultSet中

jOOQ includes a code generator that generates stubs for all of your PL/SQL packages, types, procedures, functions . jOOQ 包含一个代码生成器,可为您的所有 PL/SQL 包、类型、过程、函数生成存根 In your case, you could avoid the boring boiler plate code by calling this stub, instead:在您的情况下,您可以通过调用此存根来避免无聊的样板代码,而不是:

Mplogin result = Routines.mplogin(
    configuration, // This contains your JDBC connection
    uscd, pwd, pcsq, hwid
);

String vlogid = result.getVlogid();
String vkq = result.getVkq();

Disclaimer: I work for the company behind jOOQ.免责声明:我在 jOOQ 背后的公司工作。

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

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