2013-10-27 70 views
1

以下是我運行Java類時正在生成的代碼,頁面正確顯示信息但日誌文件中存在錯誤。Java,SQLSERVER語法

java.sql.SQLException:[FMWGEN] [SQLServer JDBC Driver] [SQLServer]'''附近語法不正確。

Java Code: 

    sql2 = " SELECT COUNT(*) CNT FROM (SELECT UPPER(SOURCECODE) as SRCCODE 
      FROM T1 WHERE (PRODUCTTYPE LIKE '%card%') 
      AND ORDER_DATE BETWEEN CONVERT(DATETIME,'"+ddet.getStartDate()+"') AND 
      CONVERT(DATETIME,'"+ddet.getEndDate()+"') 
      AND (UPPER(SOURCECODE) IS NOT NULL) GROUP BY    
      UPPER(SOURCECODE)) AS T"; 
SQL Generated: 
    SELECT COUNT(*) CNT FROM ( 
    SELECT UPPER(SOURCECODE) as SRCCODE FROM T1 WHERE 
    (PRODUCTTYPE LIKE '%card%') AND 
    ORDER_DATE BETWEEN CONVERT(DATETIME,'12/01/2012') AND CONVERT(DATETIME,'10/27/2013') 
    AND (UPPER(SOURCECODE) IS NOT NULL) 
GROUP BY UPPER(SOURCECODE) 
) AS T 
+0

如果不知道查詢的目的不容易b ut我最好的猜測是你的代碼中不需要'AS T' –

+0

什麼是T1?該表是否存在於您的數據庫中? –

回答

0

將您的查詢中使用預先製備聲明

Connection cnx = DriverManager.getConnection(sql_auth,login,password); 


sql2 = " SELECT COUNT(*) CNT FROM (SELECT UPPER(SOURCECODE) as SRCCODE 
     FROM T1 WHERE (PRODUCTTYPE LIKE '%card%') 
     AND ORDER_DATE BETWEEN CONVERT(DATETIME,?) AND 
     CONVERT(DATETIME,?) 
     AND (UPPER(SOURCECODE) IS NOT NULL) GROUP BY    
     UPPER(SOURCECODE)) AS T"; 

PreparedStatement ps = cnx.prepareStatement(sql2); 

ps.setDate(1, ddet.getStartDate()); 
ps.setDate(2, ddet.getEndDate()); 

檢查您的查詢的toString如果你想

ps.toString(); 

執行查詢

 ResultSet rs = ps.executeQuery(); 
+0

上述解決方案適用於我.Thnx,Java Main。 – SMITH