【问题标题】:Java SQL exception command not properly endedJava SQL 异常命令未正确结束
【发布时间】:2015-08-23 04:56:55
【问题描述】:

请帮我解决以下问题。使用下面的代码

 if (con == null || con.isClosed()) con = DBBase.getNewConnection();
    sb = con.createStatement();

    resSet = sb.executeQuery("select * from  USERSAGGRS  where userid='" +user.getUserid()+ "'  AND  id='" +aggid+ "'  ");

加载 jsp 页面时出现此错误:

java.sql.SQLException: ORA-00933: SQL 命令未正确结束

【问题讨论】:

  • 请显示您的代码的总部分。以便了解您的程序

标签: java oracle jdbc


【解决方案1】:

user.getUserid() 和/或aggid 的值很可能是问题所在。您可以通过打印查询来测试它并在任何 Oracle DBMS 客户端中进行测试。

System.out.println("select * from  USERSAGGRS  where userid='" +user.getUserid()+ "'  AND  id='" +aggid+ "'  ")

奥特

正确的做法是使用PreparedStatement

if (con == null || con.isClosed()) con = DBBase.getNewConnection();
    String SQL = "select * from  USERSAGGRS  where userid=?  AND  id=?"
    sb = con.prepareStatement(SQL);
    sb.setString(1,user.getUserid());
    sb.setString(2,aggid);
    resSet = sb.executeQuery();

通过使用 PreparedStatement 你也可以避免SQLInjections

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    相关资源
    最近更新 更多