【问题标题】:Running a stored procedure from Java using Statement Execute?使用 Statement Execute 从 Java 运行存储过程?
【发布时间】:2014-02-12 10:59:06
【问题描述】:

我的 Java 代码去了

Statement statement = connection.createStatement();
String storedProc = "exec stored_proc(" + someVariable + ")";
statement.execute(storedProc);

但是 Java 会抛出 SQLSyntaxException。知道我做错了什么吗?

【问题讨论】:

    标签: java sql stored-procedures


    【解决方案1】:

    试试这个查询:

    在您当前的方法中,您可以使用:

    String storedProc = "{call stored_proc(" + someVariable + ")}";
    

    请注意,我使用了call 而不是exec,并且我用大括号将查询括起来。

    但要避免sql injection,您可以使用参数化查询,例如:

    String storedProc="{call stored_proc(?)}";
    PreparedStatement pstmt=connection.prepareStatement(storedProc);
    pstmt.setString(1,someVariable);
    pstmt.execute(storedProc);
    

    【讨论】:

    • 我怎样才能改变这个'EXEC spGetCustomerDetails 123455789'
    • @R.Anandan 我没明白你的意思,请提出一个单独的问题或在这里详细解释你的问题。
    猜你喜欢
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多