对于一个应用程序,我正在检查表是否已填满,以便我可以从中检索它;否则我会从另一张桌子上回来。为此,我使用select并检查isnull()是否具有空值。如果是,我执行一个查询;否则,我执行一个不同的查询。我遇到的问题是它为null时,它不执行查询但返回空值! 你能告诉我我做错了什么或更好的解决方案吗?
这是我的代码:
String reqEx = "SELECT PYE_DATEDEBUT,PYE_DATEFIN FROM PGEXCEPTPRESENCE WHERE PYE_SALARIE='"+chaine+"' ";
ResultSet rs = stmt.executeQuery(reqEx);
if (rs.wasNull()) {
req = " select jour.PJO_HORDEBPLAGE1,jour.PJO_HORFINPLAGE1,jour.PJO_HORDEBPLAGE2,jour.PJO_HORFINPLAGE2"
+ " from profilpressalarie propressal join profilpresence propres on propressal.ppz_profilpres = propres.ppq_profilpres "
+ "join modelecycle modcyc on propres.PPQ_CYCLEAFFECT = modcyc.PMO_MODELECYCLE join JOURNEETYPE jour " +
" on modcyc.PMO_JOURNEETYPE= jour.PJO_JOURNEETYPE where modcyc.PMO_ORDREJOUR='"+orderJ+"' " +
" and propressal.PPZ_salarie= '"+chaine+"'";
}
else{
while(rs.next()){
req = " select jour.PJO_HORDEBPLAGE1,jour.PJO_HORFINPLAGE1,jour.PJO_HORDEBPLAGE2,jour.PJO_HORFINPLAGE2"
+ " from PGEXCEPTPRESENCE exc join profilpresence propres on exc.PYE_CYCLEAFFECT = propres.ppq_profilpres join modelecycle modcyc " +
"on propres.PPQ_CYCLEAFFECT = modcyc.PMO_MODELECYCLE join JOURNEETYPE jour " +
"on modcyc.PMO_JOURNEETYPE= jour.PJO_JOURNEETYPE where modcyc.PMO_ORDREJOUR='"+orderJ+"' " +
"and exc.PYE_SALARIE= '"+chaine+"' ";
}}
答案 0 :(得分:0)
rs.wasNull()用于验证最后一个读取列是否为NULL。如果表的第一列是像int这样的原始数据类型,则它不会返回NULL。因此,在这种情况下,您需要验证查询是否返回任何行。为此,请使用以下条件:
if (!rs.next()) {
req = " select jour.PJO_HORDEBPLAGE1,jour.PJO_HORFINPLAGE1,jour.PJO_HORDEBPLAGE2,jour.PJO_HORFINPLAGE2"
+ " from profilpressalarie propressal join profilpresence propres on propressal.ppz_profilpres = propres.ppq_profilpres "
+ "join modelecycle modcyc on propres.PPQ_CYCLEAFFECT = modcyc.PMO_MODELECYCLE join JOURNEETYPE jour " +
" on modcyc.PMO_JOURNEETYPE= jour.PJO_JOURNEETYPE where modcyc.PMO_ORDREJOUR='"+orderJ+"' " +
" and propressal.PPZ_salarie= '"+chaine+"'";
}