java 什么是 JDBC 中的有序绑定和命名绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16924560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:24:33  来源:igfitidea点击:

What is Ordinal binding and Named binding in JDBC

javajdbc

提问by Maruti Prasad

What is Ordinal binding and Named binding in JDBC? while calling a PL/SQL procedure i am getting an exception

什么是 JDBC 中的顺序绑定和命名绑定?在调用 PL/SQL 过程时出现异常

java.sql.SQLException: operation not allowed: Ordinal binding and Named binding cannot be combined!

java.sql.SQLException: operation not allowed: 顺序绑定和命名绑定不能组合!

回答by Eric Jablow

In Oracle, for example, you can write a stored procedure with PL/SQL code like:

例如,在 Oracle 中,您可以使用 PL/SQL 代码编写存储过程,例如:

CREATE PROCEDURE remove_emp (employee_id NUMBER) AS
    tot_emps NUMBER;
    BEGIN
    DELETE FROM employees
    WHERE employees.employee_id = remove_emp.employee_id;
    tot_emps := tot_emps - 1;
END;
/

When you call this from Java, you use code like:

当你从 Java 调用它时,你使用如下代码:

CallableStatement cs = conn.prepareCall("{call remove_emp(employee_id)}");
cs.setInt(1, 42);             // ordinal binding, or
cs.setInt("employee_id", 42); // named binding

With only one argument, the choice doesn't matter. However, you can't mix techniques.

只有一个论点,选择并不重要。但是,您不能混合使用技术。

CallableStatement cs = conn.prepareCall("{call xyzzy(plugh, bedquilt)]");
cs.setInt(1, 42);         // ordinal binding, and
cs.setInt("plugh", 1729); // named binding
cs.executeQuery();        // throws

回答by ykaganovich

Ordinal means by index. Named means by name.

序数表示索引。命名是指名称。

Google searchreveals that this could come up in a number of ways to misuse the API, so it's impossible to guess which one you fell into without seeing your code.

Google 搜索显示,这可能会以多种方式滥用 API,因此无法在不查看代码的情况下猜测您陷入了哪一种。