2012-01-15 97 views
0

我在ZKoss上转换datebox格式有些问题。日志输出如下:ZK错误选择日期

input? 
Fri Jan 04 00:00:00 UTC 2002choosing date 
test1 
org.postgresql.util.PSQLException: ERROR: syntax error at or near "Jan" 
    Position: 49 
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2096) 
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1829) 
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:510) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:386) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271) 
    at controller.jurnal.jurnalDAO.getLKeuangan(jurnalDAO.java:46) 

//error ommited 

这是我的控制器类

public class JournalController extends GenericForwardComposer { 

    private Listbox listlk; 
    private Datebox datebox; 
    private koneksi k; 
    private Connection c; 
    private SimpleDateFormat sdf; 
    private Button pilih; 

    public JournalController() { 
    } 

    @Override 
    public void doAfterCompose(Component comp) throws Exception { 
     super.doAfterCompose(comp); 

    } 

    public void onClick$pilih() throws Exception, SQLException { 
     try { 
      getListlk(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void getListlk() { 
     try { 

      Date dates = datebox.getValue(); 

      System.out.println("input?"); 
      System.out.println(dates + "choosing date"); 

      koneksi k = new koneksi(); 

      jurnalDAO jd = new jurnalDAO(k.getConnection(), dates); 
      List<Jurnal_tbl> ltsu = jd.getLKeuangan(); 
      listlk.setModel(new ListModelList(ltsu, true)); 

      listlk.setItemRenderer(new ListitemRenderer() { 

       public void render(Listitem lstm, Object o) throws Exception { 
        Jurnal_tbl jt = (Jurnal_tbl) o; 
        SimpleDateFormat sdf; 
        sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        String tanggal = sdf.format(jt.getTanggal()); 

        new Listcell(jt.getKode()).setParent(lstm); 
        new Listcell(jt.getTransaksi()).setParent(lstm); 
        new Listcell(tanggal); 
        new Listcell(jt.getAkun()).setParent(lstm); 
        new Listcell(jt.getDeskripsi()).setParent(lstm); 
        new Listcell(jt.getDC()).setParent(lstm); 
        new Listcell(Double.toString(jt.getAmount())).setParent(lstm); 
        new Listcell(jt.getItem()).setParent(lstm); 

        //  System.out.println("test keluar" + tanggal + jt); 
       } 
      }); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      k.closeConnection(); 
     } 
    } 
} 

我DataAccessObject类:

public class jurnalDAO { 

    private Connection conn; 
    private Date tanggal; 

    // private String pilihbulan; 
// private String pilihtahun; 
// JournalController lkc = new JournalController(); 
    public jurnalDAO(Connection conn, Date dates) { 
     this.conn = conn; 
     this.tanggal = dates; 
    } 

    public List<Jurnal_tbl> getLKeuangan() throws SQLException, Exception { 
     PreparedStatement ps = null; 

     //  JournalController lkc = new JournalController(); 

     try { 
      List<Jurnal_tbl> llk = new ArrayList<Jurnal_tbl>(); 
      System.out.println("test1"); 
      String sql = "select * from public.jurnal where tanggal = " + tanggal; 
      ps = conn.prepareStatement(sql); 
      ResultSet rs = ps.executeQuery(); 
      while (rs.next()) { 
       llk.add(new Jurnal_tbl(rs.getString("kode"), rs.getString("transaksi"), rs.getDate("tanggal"), rs.getString("akun"), rs.getString("Desc"), rs.getString("dc"), rs.getDouble("amt"), rs.getString("item"))); 
      } 

      rs.close(); 
      return llk; 
     } finally{ 
     } 
    } 
} 

而我最后我ZUL的页面:

<window title="Untitled Journal" width="auto" height="auto" border="" apply="controller.jurnal.JournalController"> 

         <grid> 
          <rows> 
           <row> 
            <hlayout> 
             <div> 
              <datebox id="datebox" cols="16" format="yyyy-MM-dd" mold="rounded"/> 
              <button id="pilih" label="pilih"/> 
              <button id="cetak" label="Cetak" image="images/Print.png"/> 
             </div> 
            </hlayout> 
           </row> 
          </rows> 
         </grid> 
         <listbox id="Listlk" > 
          <listhead> 
           <listheader label="Kode Jurnal"/> 
           <listheader label="No Transaksi"/> 
           <listheader label="Tanggal"/> 
           <listheader label="Account Number"/> 
           <listheader label="Deskripsi"/> 
           <listheader label="Debit/Credit"/> 
           <listheader label="Amount"/> 
           <listheader label="Item No"/> 
          </listhead> 
         </listbox> 
        </window> 

当datebox.getValue()返回jan 04 2002时,我不能转换日期格式。我希望格式为(yyyy-MM-dd)。

感谢您的帮助:)

+0

请帮我T.T – aulia 2012-01-15 17:03:49

+0

你能提供你的表模式,以及如何保存呢? 我敢打赌,这是您的数据保存问题,而不是您的数据加载。 – TonyQ 2012-01-16 05:04:12

回答

0

这不是ZK问题。 此代码是你的问题:

String sql = "select * from public.jurnal where tanggal = " + tang gal; 

你让java.util.Date#的toString(),它给你,你有个约会的默认日期显示格式的转换。

相反,你必须把一个参数在SQL语句中,然后将它的值:

String sql = "select * from public.jurnal where tanggal = :myDate" 
preparedCall = conn.prepareCall(sql); 
preparedCall.setDate(":myDate", tanggal); 
ResultSet rs = preparedCall.executeQuery();