2013-11-22 116 views
0

經與下面的代碼有很多問題:SQL準備語句無效列索引?

public static ArrayList getKundeNrToOrder (String s_date, String e_date) throwsSQLException 
{ 
    ResultSet rs = null; 

    Connection con = null; 
    int tal   = 0 ; 

    try 
    { 
     Class.forName(DB.driver); 
     con = DriverManager.getConnection(DB.URL, DB.ID, DB.PW); 
     String query = "SELECT ORDRE_NR FROM ORDRE_INFO WHERE S_DATE BETWEEN TIMESTAMP '? 00:00:00 CET' AND TIMESTAMP '? 00:00:00 CET' or E_DATE BETWEEN TIMESTAMP '? 00:00:00 CET' AND TIMESTAMP '? 00:00:00 CET' OR S_DATE <= TIMESTAMP '? 00:00:00 CET' AND E_DATE >= TIMESTAMP '? 00:00:00 CET';"; 

     PreparedStatement prest = con.prepareStatement(query);   

     String S_DATE = s_date; 
     String E_DATE = e_date; 
     System.out.println("jens"); 

     prest.setString(1,S_DATE); 
     prest.setString(2,E_DATE); 
     prest.setString(3,S_DATE); 
     prest.setString(4,E_DATE); 
     prest.setString(5,S_DATE);  
     prest.setString(6,E_DATE); 

     rs = prest.executeQuery(); 

它崩潰,並給我一個錯誤「無效的列索引......」

任何人有一個想法,爲什麼失敗?我試圖給它這個查詢,而不是它的作品完美。

String query = "SELECT ORDRE_NR FROM ORDRE_INFO WHERE 
S_DATE BETWEEN TIMESTAMP '2007-10-10 00:00:00 CET' 
AND TIMESTAMP '2009-10-10 00:00:00 CET' 
or E_DATE BETWEEN TIMESTAMP '2007-10-10 00:00:00 CET' 
AND TIMESTAMP '2009-10-10 00:00:00 CET'OR S_DATE <= TIMESTAMP '2007-10-10 00:00:00 CET' 
AND E_DATE >= TIMESTAMP '2009-10-10 00:00:00 CET'"; 
+0

你確定你連接到正確的數據庫和架構? –

+0

是的:)有連接 – HOervald

回答

0

您的語法不正確。這個?不應該在引號中。考慮設置

S_DATE=s_date+" 00:00:00 CET" 

而不是在聲明本身。

然後你的聲明將是:

String query = "SELECT ORDRE_NR FROM ORDRE_INFO WHERE S_DATE BETWEEN TIMESTAMP ? AND TIMESTAMP ? ...." 
+0

然後我得到一個關鍵字錯誤...任何想法壽這然後:) – HOervald

+0

使用setTimestamp而不是setString並從查詢中刪除TIMESTAMP – jny

+0

謝謝:)現在得到了一個全新的問題世界但我認爲這解決了一些:) – HOervald