Java SQL异常命令未正确结束

时间:2015-08-23 04:56:55

标签: java oracle jdbc

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

 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命令未正确结束

1 个答案:

答案 0 :(得分:1)

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

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

OTOH

正确的方法是使用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