Java 在两个日期之间从我的SQL中搜索数据

Java 在两个日期之间从我的SQL中搜索数据,java,mysql,sql,Java,Mysql,Sql,当我在一个月内的两个日期之间进行搜索时,例如在2013年2月2日和2013年2月9日之间 结果显示从2013年1月3日到2013年1月8日以及从2013年2月3日到2013年2月8日 下面是我的代码: try { dconfig dcf=new dconfig(); java.sql.Connection connection; Class.forName(dcf.driver); connection=(com.mysql.jdbc.Connection) Dr

当我在一个月内的两个日期之间进行搜索时,例如在2013年2月2日和2013年2月9日之间 结果显示从2013年1月3日到2013年1月8日以及从2013年2月3日到2013年2月8日

下面是我的代码:

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();
}

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

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

你说的是日期>-尝试将其更改为>=我尝试使用>=和@user2011577-什么是SS-不熟悉的-你没有将日期存储在datetime字段中?SS表示秒,我将日期时间保存在数据库列中,数据类型为VARCHAR45,当我尝试将其更改为datatype datetime时,错误为:in validvalue@user2011577-如果您没有存储日期,那么您希望如何将其转换为datetime?您需要重新考虑您的数据库结构。它的DD-MM-YYYY HH:MM:SS格式i正在存储。如果数据类型为VARCHAR45是否可以,或者它是否会影响搜索。。。我需要搜索查询,我可以得到两个日期之间的正确结果。。。
if (datetosearch.getText().trim().length()>0) {
    StrQr=StrQr + " and date >= '" + datesearch.getText().toString()+"' ";
    StrQr=StrQr + " and date <= '" + datetosearch.getText().toString()+"' ";
}