2017-03-24 7 views
0

私はデータベーステーブルを持っている列は複数の文字列を保持しています。彼らはライセンスプレート番号を保持しています。私が1 ilを検索すると、最初の登録を取得します。私は完全な文字列を入力する必要があるようにそれを設定したいと私はそれが見つからないと言う必要がありません。ここに私のコードです。私は、これを変更する必要があるSQLコマンドだけだと確信しています。特定のSQLのためのSQLの検索

public Car getCar(String searchLicense) { 
    Car foundCar = new Car(); 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = DriverManager.getConnection(url + dbName, userName, password); 
     statement = conn.createStatement(); 
     resultSet = statement 
       .executeQuery("select * from eflow.registration where cLicense like '%" + searchLicense + "%'"); 

     while (resultSet.next()) { 
      foundCar = new Car(resultSet.getInt("cID"), resultSet.getString("cLicense"), 
        resultSet.getInt("cJourneys"), resultSet.getString("cUsername"), 
        resultSet.getString("cPassword").toString()); 
     } 
     conn.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return foundCar; 
} 

答えて

1

正確な文字列が必要な場合は、LIKEフィルタで '%'を削除する必要があります。 あなたのコードは次のようになります。

cLicense like '" + searchLicense + "' 

「%」ワイルドカードの前または後に何であるかのチェックをせずに、前に入れて、あなたが持っているパラメータの後、途中でsearchLicense値を含む任意の文字列を検索することができますその文字列。

関連する問題