-1

1 か月の 2 つの日付間 (たとえば02-02-2013との間09-02-2013) で検索すると、結果は from 03-01-2013to08-01-201303-02-2013to に表示され08-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();
}
4

1 に答える 1

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 インジェクションに対して非常に脆弱です。代わりに、パラメーター化されたクエリの使用を検討してください。

于 2013-02-15T23:07:18.560 に答える