我在ZKoss上转换datebox格式时遇到了一些问题。日志输出如下:
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()返回2002年1月4日时,我无法转换日期格式。 我希望格式化(yyyy-MM-dd)。
感谢您帮助我:)
答案 0 :(得分: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();