SQLException:JZ0S4:无法执行空(零长度)查询。 关于准备好的声明

发布于 2024-07-27 18:06:41 字数 717 浏览 4 评论 0原文

这是一个在连接上执行PreparedStatements 的类。

public class doSomething {
    private PreparedStatement ps;

    public setPS (Connection conn) throws SQLException {
        String sql = "select * from table where id = ?";
        ps = conn.prepareStatement(sql);
    }

    public void runSomething(String var){
        ps.setString(1,var);
        ResultSet rs = ps.executeQuery();
        ...
    }
}

doSomethingInstance.setPS(conn);
doSomethingInstance.runSomething(var);

从另一个类调用,这会抛出异常,异常

 ResultSet rs = ps.executeQuery();

是 SQLException:JZ0S4:无法执行空(零长度)查询。 关于准备好的声明。 我不明白为什么。 有人看到我在这里做错了什么吗?

谢谢!

Heres a class executing PreparedStatements on a Connection.

public class doSomething {
    private PreparedStatement ps;

    public setPS (Connection conn) throws SQLException {
        String sql = "select * from table where id = ?";
        ps = conn.prepareStatement(sql);
    }

    public void runSomething(String var){
        ps.setString(1,var);
        ResultSet rs = ps.executeQuery();
        ...
    }
}

I call

doSomethingInstance.setPS(conn);
doSomethingInstance.runSomething(var);

from another class and this throws and exception at the

 ResultSet rs = ps.executeQuery();

The exception is SQLException: JZ0S4: Cannot execute an empty (zero-length) query. on a Prepared Statement. I cant understand why. Does anyone see what Iam doing wrong here?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不打扰别人 2024-08-03 18:06:41

返回并直接从源文件复制代码,然后编辑您的问题。 您有一个潜在的歧义:您的第一个片段将准备好的语句称为“preparedStatement”,然后更改为“prepareStatement”(不带“d”)。 清晰地查看源代码将使隔离问题变得更加容易。 您有两个变量还是您的示例输入错误?

[之后...]
感谢您更新您的代码。 我没有看到它有明显的问题。 您是否使用调试器(例如,在 Eclipse 中)单步调试它以确保这两个方法按预期被调用?

Go back and copy the code straight from your source file, then edit your question. You have a potential ambiguity: Your first fragment calls the prepared statement "preparedStatement", then you change to "prepareStatement" (without the "d"). Having a clean look at your source code will make isolating the problem easier. Do you have two variables or did you mistype your example?

[later...]
Thanks for updating your code. I'm not seeing an obvious problem with it. Have you stepped through it with a debugger (e.g., in Eclipse) to ensure that the two methods are being called as expected?

唱一曲作罢 2024-08-03 18:06:41

这个问题已经被修复了。 数据库权限有问题。 捕获并打印堆栈跟踪

 ResultSet rs = ps.executeQuery();

表明未找到查询正在访问的表。 我仍然需要弄清楚为什么抛出 SQLException: JZ0S4: Cannotexecute anempty (zero-length) query 。

感谢您的时间和考虑。
罗汉

This issue has been fixed folks. There was a problem with the database permissions. Catching and printing the stacktrace on

 ResultSet rs = ps.executeQuery();

indicated that the table being accessed by the query was not found. I still have to figure out why the SQLException: JZ0S4: Cannot execute an empty (zero-length) query is being thrown.

Thanks for your time and consideration.
Rohan

蓝戈者 2024-08-03 18:06:41

你能在执行查询之前打印sql的内容吗? 我怀疑它正在丢失内容

can you print the contents of sql before executing the query? I suspect it's losing it's contents

梦幻的味道 2024-08-03 18:06:41

var 是否非空? var 的类型是否正确(我认为是 int 而不是 String?)

Is var non null? Is var of the correct type(int instead of String I would think?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文