在Java -MySql中处理多个过滤器

时间:2014-10-01 10:39:16

标签: java mysql sql filter

有三个过滤器Class,Section和Date。现在有七种可能性:

1-用户输入类

2-)用户输入部分

3-)用户输入日期

4-)用户输入日期和班级

依旧......

我编写了以下代码来处理java中的这些可能性:

if( mySection.equals("0") && myClass.equals("0") && ( FDate.trim().compareTo("")==0 || TDate.trim().compareTo("")==0    ) )
        {
        out.println("Note: Please provide valid filteration attributes. Atleast one filter is required.");
        return;
        }



        if( !mySection.equals("0") && myClass.equals("0") && ( FDate.trim().compareTo("")==0 || TDate.trim().compareTo("")==0    ) )
        {
            Query = "select st.rollno, concat(st.firstname,' ',st.midname,' ',st.lastname), st.level, st.section, st.rfidtag, sc.schoolname, sa.time, date_format(sa.time,'%T') from AAV.studentattendence sa, AAV.Students st, AAV.Schools sc where sa.studentindexptr=st.indexptr and sc.indexptr=st.schoolindexptr and st.section='"+mySection+"'";
        }

        if( !mySection.equals("0") && !myClass.equals("0") && ( FDate.trim().compareTo("")==0 || TDate.trim().compareTo("")==0    ) )
        {
            Query = "select st.rollno, concat(st.firstname,' ',st.midname,' ',st.lastname), st.level, st.section, st.rfidtag, sc.schoolname, sa.time, date_format(sa.time,'%T') from AAV.studentattendence sa, AAV.Students st, AAV.Schools sc where sa.studentindexptr=st.indexptr and sc.indexptr=st.schoolindexptr and st.section='"+mySection+"' and st.level='"+myClass+"' ";
        }


        if( mySection.equals("0") && !myClass.equals("0") && ( FDate.trim().compareTo("")==0 || TDate.trim().compareTo("")==0    ) )
        {
            Query = "select st.rollno, concat(st.firstname,' ',st.midname,' ',st.lastname), st.level, st.section, st.rfidtag, sc.schoolname, sa.time, date_format(sa.time,'%T') from AAV.studentattendence sa, AAV.Students st, AAV.Schools sc where sa.studentindexptr=st.indexptr and sc.indexptr=st.schoolindexptr and st.level='"+myClass+"' ";
        }


        if( mySection.equals("0") && myClass.equals("0") && ( !(FDate.trim().compareTo("")==0) && !(TDate.trim().compareTo("")==0)    ) )
        {
            Query = "select st.rollno, concat(st.firstname,' ',st.midname,' ',st.lastname), st.level, st.section, st.rfidtag, sc.schoolname, sa.time, date_format(sa.time,'%T') from AAV.studentattendence sa, AAV.Students st, AAV.Schools sc where sa.studentindexptr=st.indexptr and sc.indexptr=st.schoolindexptr and date_format(time,'%Y-%m-%d %T') between '"+FromDate+"'  and '"+ToDate+"'";
        }


        if( mySection.equals("0") && !myClass.equals("0") && ( !(FDate.trim().compareTo("")==0) && !(TDate.trim().compareTo("")==0)    ) )
        {
            Query = "select st.rollno, concat(st.firstname,' ',st.midname,' ',st.lastname), st.level, st.section, st.rfidtag, sc.schoolname, sa.time, date_format(sa.time,'%T') from AAV.studentattendence sa, AAV.Students st, AAV.Schools sc where sa.studentindexptr=st.indexptr and sc.indexptr=st.schoolindexptr and date_format(time,'%Y-%m-%d %T') between '"+FromDate+"'  and '"+ToDate+"' and st.level='"+myClass+"'";
        }


        if( !mySection.equals("0") && myClass.equals("0") && ( !(FDate.trim().compareTo("")==0) && !(TDate.trim().compareTo("")==0)    ) )
        {
            Query = "select st.rollno, concat(st.firstname,' ',st.midname,' ',st.lastname), st.level, st.section, st.rfidtag, sc.schoolname, sa.time, date_format(sa.time,'%T') from AAV.studentattendence sa, AAV.Students st, AAV.Schools sc where sa.studentindexptr=st.indexptr and sc.indexptr=st.schoolindexptr and date_format(time,'%Y-%m-%d %T') between '"+FromDate+"'  and '"+ToDate+"' st.section='"+mySection+"'";
        }


        if( !mySection.equals("0") && !myClass.equals("0") && ( !(FDate.trim().compareTo("")==0) && !(TDate.trim().compareTo("")==0)    ) )
        {
            Query = "select st.rollno, concat(st.firstname,' ',st.midname,' ',st.lastname), st.level, st.section, st.rfidtag, sc.schoolname, sa.time, date_format(sa.time,'%T') from AAV.studentattendence sa, AAV.Students st, AAV.Schools sc where sa.studentindexptr=st.indexptr and sc.indexptr=st.schoolindexptr and date_format(time,'%Y-%m-%d %T') between '"+FromDate+"'  and '"+ToDate+"' and st.section='"+mySection+"' and st.level='"+myClass+"'";
        }

这是问题,在我添加另一个过滤器的那一刻,这些可能性大幅增加,因此查询和处理更多。 为了通过JAVA或MYSQL有效地处理这个问题,必须有办法。

这种情况的通用sql语句?

JAVA中用于处理多个输入查询的库?

某种东西......

使用尽可能少的代码处理这种情况可能会做些什么(考虑过滤器可能会添加)?

1 个答案:

答案 0 :(得分:2)

解决了,在我找到一个解决方案之后想了一些。以下是解决此问题的通用单一查询:

 if( mySection.equals("0") && myClass.equals("0") && ( FDate.trim().compareTo("")==0 || TDate.trim().compareTo("")==0    ) )
        {
        out.println("Note: Please provide valid filteration attributes. Atleast one filter is required.");
        return;
        }


        Query = "select st.rollno, concat(st.firstname,' ',st.midname,' ',st.lastname), st.level, st.section, st.rfidtag, sc.schoolname, sa.time, date_format(sa.time,'%T') from AAV.studentattendence sa, AAV.Students st, AAV.Schools sc where sa.studentindexptr=st.indexptr and sc.indexptr=st.schoolindexptr and ( '0'='"+mySection+"' or st.section='"+mySection+"') and ('0'='"+myClass+"' or st.level='"+myClass+"') and (  (' 00:00:00'='"+FromDate+"' and ' 23:59:59'='"+ToDate+"')  or (date_format(time,'%Y-%m-%d %T') between '"+FromDate+"'  and '"+ToDate+"' )  )";


executeQuery(Query);

当作为表达式处理时,在我的情况下,在默认值中解析为true,即'0',查询变为通用,我们可以根据需要添加尽可能多的过滤子句。