我在下面点击这一行时没有正确结束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) {}
}
}
答案 0 :(得分: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);
答案 1 :(得分:1)
错误:ORA-00933:SQL命令未正确结束 原因:您尝试使用不合适的子句执行SQL语句。
您应该捕获catch块中的堆栈跟踪,而不仅仅是捕获错误消息。这将为您提供具有根本原因的语句执行行号。
更改
logger.error("iCARE:Error : " + ex.getMessage()); // <<this line show me that error>>
到
ex.printStackTrace(); // <<this line show me that error>>
或者,您可以尝试以下代码更改,看看它是否适合您。
您对update语句的输入可能有一些未转义的字符,从而导致错误。将您的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
}
...
答案 2 :(得分:0)
如果要将变量字符串插入命令字符串,则可以获得ORA-00933。
string inputName = "Rose";
string sqlCmd = "SELECT * FROM mytable WHERE brand_name = '" + inputName +"'";
以上工作正常 - 但如果:
string inputName = "Rose's";
生成的SQL是
抛出ORA-00933的SELECT * FROM mytable WHERE brand_name = 'Rose's'
,所以记得要逃避你的单引号!
如果您使用LIKE
条款,则可能必须开始考虑转义%
。人们建议使用预准备语句的原因之一是您不必担心逃避这些事情。