SQL准备语句无效列索引?

时间:2013-11-22 15:13:02

标签: 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'";

1 个答案:

答案 0 :(得分:0)

您的语法不正确。的?不应该在引号中。考虑设置

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

而不是在声明本身。

然后你的陈述是:

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