【问题标题】:ora-00933:SQL command not properly ended for update sql statementora-00933:更新 sql 语句的 SQL 命令未正确结束
【发布时间】:2012-05-29 15:21:40
【问题描述】:

点击下面的这一行时,我发现 sql 命令没有正确结束。

stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);    
String updateQ = "update ANI_999 set First_Name = '"+d.getName()+"', HouseNo = '"+d.getAddr1()+"', Indicator_Sourcefile_iCARE3 = Indicator_Sourcefile_iCARE2, Indicator_Sourcefile_iCARE2 = Indicator_Sourcefile_iCARE1, Indicator_Sourcefile_iCARE1='"+currentFile+"' where CALLER_ID = '"+msisdn+"' ";

int result = stmt.executeUpdate(updateQ);
conn.commit();
conn.close();`

我不断收到 ORA-00933:SQL 命令未正确结束。

这是updateQ 语句的样子:

update ANI_999 set First_Name = 'ZAHARAH BINTI ABDUL RAHMAN', HouseNo = 'No. JKR6357,', Indicator_Sourcefile_iCARE3 = Indicator_Sourcefile_iCARE2, Indicator_Sourcefile_iCARE2 = Indicator_Sourcefile_iCARE1, Indicator_Sourcefile_iCARE1='ICAREP_ANI_SVCPROF_20120402_002.DAT' where CALLER_ID = '058011726' 

这里是完整的功能:- 请参考这个符号“

public void updateRecord(icData d, String msisdn) {
   Connection conn = null;
   Statement stmt = null;
   int recCtr = 0;

try {
   conn = ds.getConnection();

       stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); 
       String updateQ = "update ANI_999 set First_Name = '"+d.getName()+"', HouseNo = '"+d.getAddr1()+"', Indicator_Sourcefile_iCARE3 = Indicator_Sourcefile_iCARE2, Indicator_Sourcefile_iCARE2 = Indicator_Sourcefile_iCARE1, Indicator_Sourcefile_iCARE1='"+currentFile+"' where CALLER_ID = '"+msisdn+"' ";


   int result = stmt.executeUpdate(updateQ);
   conn.commit();
   conn.close();
}
catch(SQLException ex) {

    logger.error("iCARE:Error : " + ex.getMessage()); <<this line show me that error>>

}
finally {
    try {if (stmt != null) stmt.close();} catch (SQLException e) {}
        try {if (conn != null) conn.close();} catch (SQLException e) {}
}
}

【问题讨论】:

  • 您是否尝试过打印 sql 并在 SQL 提示符下运行。如果缺少引号,很容易找到。
  • currentFile 中的值是多少。这可能包含一些导致问题的斜线
  • 对不起,打印 sql 是什么意思?..我已经检查了“结果”值,它显示数字,表示 sql 工作正常但我不知道为什么我不断收到此错误。ORA-00933: SQL 命令未正确结束。
  • 如果您构建更新查询的任何变量包含单引号,则会导致此错误。更严重的是,这种直接从用户输入构建语句的技术容易受到“SQL 注入”(Google it)的攻击,并且是一个永远不应出现在生产代码中的巨大安全漏洞。
  • @SitiHaslinaMohdZulkafli - 嗯。不仅是错误消息,还有 statcktrace。检查我的回答,如果有帮助的话。

标签: java sql oracle ora-00933


【解决方案1】:

你应该使用 PreparedStatement:

String updateQ = "update ANI_999 set First_Name = ?, HouseNo = ?, " +
       "Indicator_Sourcefile_iCARE3 = Indicator_Sourcefile_iCARE2, " +
       "Indicator_Sourcefile_iCARE2 = Indicator_Sourcefile_iCARE1, " +
       "Indicator_Sourcefile_iCARE1=? where CALLER_ID = ? ";
PreparedStatement prep =  conn.prepareStatement(updateQ, 
    ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); 
prep.setString(1, ...);
prep.setString(2, ...);
prep.setString(3, ...);
int result = prep.executeUpdate(updateQ);

【讨论】:

  • 谢谢您..您的建议非常有用。我真的很感激..谢谢..:)
【解决方案2】:

错误:ORA-00933:SQL 命令未正确结束。
CAUSE: 你试图用不适当的子句执行 SQL 语句。

您应该在 catch 块中捕获堆栈跟踪,而不是仅仅捕获错误消息。这为您提供了具有根本原因的语句执行的行号。

改变

logger.error("iCARE:Error : " + ex.getMessage()); // <<this line show me that error>>

ex.printStackTrace(); // <<this line show me that error>>

或者,您可以尝试以下代码更改,看看它是否适合您。

您对更新语句的输入有可能包含一些未转义的字符,从而导致错误。将您的 Statement 对象更改为 PreparedStatement 并查看它是否已解决。

try {  
  ...
  String updateQ = "update ANI_999"  
    + " set First_Name = ?, HouseNo = ?,"  
    + " Indicator_Sourcefile_iCARE3 = Indicator_Sourcefile_iCARE2,"  
    + " Indicator_Sourcefile_iCARE2 = Indicator_Sourcefile_iCARE1,"  
    + " Indicator_Sourcefile_iCARE1=?"   
    + " where CALLER_ID = ?";  

  PreparedStatement pstmt = conn
   .createStatement( updateQ, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY );  
  pstmt.setString( 1, d.getName() );  
  pstmt.setString( 2, d.getAddr1() );  
  pstmt.setString( 3, currentFile );  
  pstmt.setString( 4, msisdn );  

  // print what the query actually holds. Not sure if all drivers support this.
  System.out.println( "DEBUG: query: " + pstmt.toString() );

  int result = pstmt.executeUpdate( updateQ );  
  System.out.println( "DEBUG: Update Result: " + result );
  ...  
} catch ( Exception ex ) {  
  // logger.error( ...  
  ex.printStackTrace(); // keep this until debugged  
}  
...

【讨论】:

  • 谢谢你,Ravinder,当我尝试这个时它的工作,但需要一些修改。无论如何,感谢这个想法。真的很感激。耶耶!!..:)
  • @SitiHaslinaMohdZulkafli - 你跟踪错误堆栈了吗?如果是,请将其发布在您的查询中。
  • 好的,我会更新的。我不能“投票”,因为我的声望只有 11。我需要将声望提高到 15 才能做到这一点。对不起..:)
【解决方案3】:

如果您将变量字符串插入到命令字符串中,例如

,您可以获得 ORA-00933
string inputName = "Rose";
string sqlCmd = "SELECT * FROM mytable WHERE brand_name = '" + inputName +"'";

以上工作正常 - 但如果:

string inputName = "Rose's";

生成的 SQL 是 SELECT * FROM mytable WHERE brand_name = 'Rose's' 会抛出 ORA-00933,所以请记住转义单引号!

如果您使用的是LIKE 子句,那么您可能不得不开始考虑转义% 的问题。人们建议使用准备好的语句的原因之一是您不必担心转义这些东西。

【讨论】:

    猜你喜欢
    • 2016-02-04
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 2010-09-09
    • 2010-11-22
    • 1970-01-01
    相关资源
    最近更新 更多