2013-02-15 58 views
-1

当我做两个日期之间一个月(间02-02-201309-02-2013例如) 搜索结果之间的SQL数据是从03-01-201308-01-201303-02-201308-02-2013搜索从我的两个日期

下面是我的代码:

try { 
    dconfig dcf=new dconfig(); 
    java.sql.Connection connection; 
    Class.forName(dcf.driver); 
    connection=(com.mysql.jdbc.Connection) DriverManager.getConnection(dcf.StrUrl,dcf.StrUid,dcf.StrPwd); 
    ResultSet rs = null; 
    ResultSet rscount; 
    String StrQr=""; 

    if (jobnamesearch.getText().trim().length()>0) { 
     StrQr=StrQr + " and job_name like '%" + jobnamesearch.getText().trim() +"%' "; 
    } 

    if (datetosearch.getText().trim().length()>0) { 
     StrQr=StrQr + " and date > '" + datesearch.getText().toString()+"' "; 
     StrQr=StrQr + " and date < '" + datetosearch.getText().toString()+"' "; 
    } 

    if (datesearch.getText().trim().length()>0 && datetosearch.getText().trim().length()==0) { 
     StrQr=StrQr + " and date like '%" + datesearch.getText().trim().toString()+ "%' "; 
    } 

    if (dwsearch.getText().trim().length()>0) { 
     StrQr=StrQr + " and dw_no like '%" + dwsearch.getText().trim() + "%' "; 
    } 

    if (remsearch.getText().trim().length()>0) { 
     StrQr=StrQr + " and rem_no like '%" + remsearch.getText().trim() + "%' "; 
    } 

    if (typesearch.getSelectedItem().toString().length()<11) { 
     StrQr=StrQr + " and type like '%" + typesearch.getSelectedItem().toString() + "%' "; 
    } 

    if (usersearch.getSelectedItem().toString().length()>0) { 
     StrQr=StrQr + " and user like '%" + usersearch.getSelectedItem().toString() + "%' "; 
    } 

    java.sql.PreparedStatement stmt=connection.prepareStatement("SELECT ID,job_name,details,type,dw_no,rem_no,user,date FROM tracker where 1=1 " + StrQr +" order by ID DESC"); 
    java.sql.PreparedStatement stmtcount=connection.prepareStatement("SELECT Count(*) FROM tracker where 1=1 " + StrQr +""); 
    rs = stmt.executeQuery(); 

    rscount = stmtcount.executeQuery(); 
} 

回答

0

如果我猜的话,我会说这是在这里:

if (datetosearch.getText().trim().length()>0) { 
    StrQr=StrQr + " and date > '" + datesearch.getText().toString()+"' "; 
    StrQr=StrQr + " and date < '" + datetosearch.getText().toString()+"' "; 
} 

你说日期> - 尝试改变,为> =和< =。

if (datetosearch.getText().trim().length()>0) { 
    StrQr=StrQr + " and date >= '" + datesearch.getText().toString()+"' "; 
    StrQr=StrQr + " and date <= '" + datetosearch.getText().toString()+"' "; 
} 

此外,如果在你的数据库中的日期是日期时间,那么< =不会工作。您需要为其添加一天或追加23:59,因为它将在午夜的当天搜索。

希望这会有所帮助。

顺便说一句 - 这是非常容易受到SQL注入。改为使用参数化查询。

+0

我试过使用> =和<=但不工作显示相同的结果数据库中的日期格式是SS-MM-YYYY HH:MM:SS – user2011577 2013-02-15 23:09:39

+0

@ user2011577 - 什么是SS--不熟悉的 - 你没有在日期时间字段中存储日期? – sgeddes 2013-02-15 23:56:33

+0

ss意味着秒我保存日期时间在数据库列数据类型为VARCHAR(45)当我尝试将其更改为数据类型DATETIME它给出的错误为:在有效值 – user2011577 2013-02-16 00:03:03