2013-12-09 90 views
1

我有一個HQL展示間(TO_DATE)根據我的數據,但我得到Could not locate named parameter錯誤無法找到名爲參數

這是我的代碼

SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); 
    Date date1 = Calendar.getInstance().getTime(); 
    String tgl1 = sdf1.format(date1); 
    String Tanggal_awal = tgl1+" 00:00:00"; 
    System.out.println(Tanggal_awal); 

    SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy"); 
    Date date2 = Calendar.getInstance().getTime(); 
    String tgl2 = sdf2.format(date2); 
    String Tanggal_akhir = tgl2+" 23:59:59"; 
    System.out.println(Tanggal_akhir); 

    Query LoadSource = session_source.createQuery("select CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE,COUNT(*) FROM SwitcherServiceSource" + 
      " where TIMESTAMP between to_date(':awal','dd-MM-yyyy HH24:MI:SS') and to_date(':akhir','dd-MM-yyyy HH24:MI:SS')" + 
      " and PROVIDER_CODE is not null group by CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE order by CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE"); 
    LoadSource.setParameter("awal", Tanggal_awal); 
    LoadSource.setParameter("akhir", Tanggal_akhir); 

任何幫助將是快樂:)

回答

1

您不需要引用您的參數。即:

而不必where TIMESTAMP between to_date(':awal','dd-MM-yyyy HH24:MI:SS') and to_date(':akhir','dd-MM-yyyy HH24:MI:SS')的,你應該寫where TIMESTAMP between to_date(:awal,'dd-MM-yyyy HH24:MI:SS') and to_date(:akhir,'dd-MM-yyyy HH24:MI:SS')


更妙的是,你可以(也應該)甚至避免結合一個字符串,該參數。您可以綁定一個日期/時間標記,以便查詢,能夠更有效和可讀性:where TIMESTAMP between :awal and :akhir,並做類似這樣的東西在你的代碼:

Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.HOUR_OF_DAY, 0); 
cal.set(Calendar.MINUTE, 0); 
cal.set(Calendar.SECOND, 0); 
cal.set(Calendar.MILLISECOND, 0); 

Date fromDate = cal.getTime(); 

cal.set(Calendar.HOUR_OF_DAY, 23); 
cal.set(Calendar.MINUTE, 59); 
cal.set(Calendar.SECOND, 59); 

Date toDate = cal.getTime(); 

Query loadSource = session_source.createQuery("select blablabla FROM SwitcherServiceSource" + 
     " where TIMESTAMP between :awal and :akhir " + 
     " and blablabla group by blabla order by blablabla"); 
loadSource.setParameter("awal", fromDate); 
loadSource.setParameter("akhir", toDate); 
+0

whoaaahhhh解決了,感謝花花公子;)) –

+0

不,精湛的意見..... mega謝謝你,夥計,我會試試它:) –

相關問題