4
public void searchKlijenta(KlijentiFormEvent klijentiFormEvent) throws SQLException {

    String nazivK = klijentiFormEvent.getNaziv();
    String adresaK = klijentiFormEvent.getAdresa();
    String gradK = klijentiFormEvent.getGrad();
    String drzavaK = klijentiFormEvent.getDrzava();
    String telefonK = klijentiFormEvent.getTelefon();
    String faxK = klijentiFormEvent.getFax();
    String mailK = klijentiFormEvent.getMail();
    String mobitelK = klijentiFormEvent.getMobitel();
    String oibK = klijentiFormEvent.getOib();
    String ugovorK = klijentiFormEvent.getUgovor();
    String osobaK = klijentiFormEvent.getOsoba();

    if (nazivK.length() == 0) 
        nazivK = null;
    if (adresaK.length() == 0) 
        adresaK = null;
    if (gradK.length() == 0) 
        gradK = null;
    if (drzavaK.length() == 0) 
        drzavaK = null;
    if (telefonK.length() == 0) 
        telefonK = null;
    if (faxK.length() == 0) 
        faxK = null;
    if (mailK.length() == 0) 
        mailK = null;
    if (mobitelK.length() == 0) 
        mobitelK = null;
    if (oibK.length() == 0) 
        oibK = null;
    if (ugovorK.length() == 0) 
        ugovorK = null;
    if (osobaK.length() == 0) 
        osobaK = null;


    klijentiSearchModel.clear();

    String sql = "select * from zavrsni.klijenti where naziv like '"+nazivK+"' or adresa like '"+adresaK+"' or grad like '"+gradK+"' or drzava like '"+drzavaK+"' or telefon like '"+telefonK+"' or fax like '"+faxK+"' or mail like '"+mailK+"' or mobitel like '"+mobitelK+"' or oib like '"+oibK+"' or ugovor like '"+ugovorK+"' or osoba like '"+osobaK+"' ";
    Statement selectStmt = con.createStatement();
    ResultSet result = selectStmt.executeQuery(sql);

    while(result.next()) {
        int id = result.getInt("id");
        String naziv = result.getString("naziv");
        String adresa = result.getString("adresa");
        String grad = result.getString("grad");
        int posBr = result.getInt("posBr");
        String drzava = result.getString("drzava");
        String telefon = result.getString("telefon");
        String fax = result.getString("fax");
        String mail = result.getString("mail");
        String mobitel = result.getString("mobitel");
        String oib = result.getString("oib");
        String ugovor = result.getString("ugovor");
        String osoba = result.getString("osoba");

        KlijentiModelSearch klijentSearch = new KlijentiModelSearch(id, naziv, adresa, grad, posBr, drzava, telefon, fax, mail, mobitel, oib, ugovor, osoba);
        klijentiSearchModel.add(klijentSearch);
    }

    result.close();
    selectStmt.close();

}

Can i write this code shorter? I think of "if" statement?

Perhaps through a while loop?

Method that is use for search some client in database. This method work fane but this if-statement i want write shorter.

Thanks

EDIT SOLVED:

public void traziKlijenta(KlijentiFormEvent klijentiFormEvent) throws SQLException {

    String nazivK = returnNullIfEmptys(klijentiFormEvent.getNaziv());
    String adresaK = returnNullIfEmptys(klijentiFormEvent.getAdresa());
    String gradK = returnNullIfEmptys(klijentiFormEvent.getGrad());
    String drzavaK = returnNullIfEmptys(klijentiFormEvent.getDrzava());
    String telefonK = returnNullIfEmptys(klijentiFormEvent.getTelefon());
    String faxK = returnNullIfEmptys(klijentiFormEvent.getFax());
    String mailK = returnNullIfEmptys(klijentiFormEvent.getMail());
    String mobitelK = returnNullIfEmptys(klijentiFormEvent.getMobitel());
    String oibK = returnNullIfEmptys(klijentiFormEvent.getOib());
    String ugovorK = returnNullIfEmptys(klijentiFormEvent.getUgovor());
    String osobaK = returnNullIfEmptys(klijentiFormEvent.getOsoba());

    klijentiSearchModel.clear();

    String sql = "select * from zavrsni.klijenti where naziv like '%"+nazivK+"%' or adresa like '%"+adresaK+"%' or grad like '%"+gradK+"%' or drzava like '%"+drzavaK+"%' or telefon like '%"+telefonK+"%' or fax like '%"+faxK+"%' or mail like '%"+mailK+"%' or mobitel like '%"+mobitelK+"%' or oib like '%"+oibK+"%' or ugovor like '%"+ugovorK+"%' or osoba like '%"+osobaK+"%' ";
    Statement selectStmt = con.createStatement();
    ResultSet result = selectStmt.executeQuery(sql);

    while(result.next()) {
        int id = result.getInt("id");
        String naziv = result.getString("naziv");
        String adresa = result.getString("adresa");
        String grad = result.getString("grad");
        int posBr = result.getInt("posBr");
        String drzava = result.getString("drzava");
        String telefon = result.getString("telefon");       
        String fax = result.getString("fax");
        String mail = result.getString("mail");
        String mobitel = result.getString("mobitel");
        String oib = result.getString("oib");
        String ugovor = result.getString("ugovor");
        String osoba = result.getString("osoba");

        KlijentiModelSearch klijentSearch = new KlijentiModelSearch(id, naziv, adresa, grad, posBr, drzava, telefon, fax, mail, mobitel, oib, ugovor, osoba);
        klijentiSearchModel.add(klijentSearch);
    }

    result.close();
    selectStmt.close();

}
private String returnNullIfEmptys(String value) {
    if (value == null || value.length() == 0) {
        return null;
    }
    return value;
}
9
  • Do the variables have a common type, for instance String? Commented Feb 10, 2017 at 9:05
  • how about nazivK = nazivK.length() == 0 ? null : nazivK and so on? Commented Feb 10, 2017 at 9:06
  • 3
    If your variables were in a list or array instead of separately named, you could do it all in a loop. Alternatively, you could write a method nullIfEmpty and write nazivK = nullIfEmpty(nazivK); etc. Commented Feb 10, 2017 at 9:06
  • Make a collection of variable if they have same datatypes. Iterate the collection and write your logic Commented Feb 10, 2017 at 9:07
  • 1
    And for the record: such code is the symptom of bad design. Dont just look at this code; step back and have some more experienced people review more of your project. What you are showing here is for sure just the tip of the iceberg ;-) Commented Feb 10, 2017 at 9:14

4 Answers 4

4

With your actual code, @khelwood proposition in your comment question is the best approach.
Other solutions have overhead and change your design without bringing a added value .

public static String returnNullIfEmpty(String value){
   if (value == null || value.length() == 0){
      return null;
   }
     return value;
}

Then you can call it in this way :

nazivK = returnNullIfEmpty(nazivK);
adresaK= returnNullIfEmpty(adresaK);

EDIT

With the edit of your question, you could include processing as the time where you retrieve the value from the klijentiFormEvent object :

String nazivK =  returnNullIfEmpty(klijentiFormEvent.getNaziv());
String adresaK = returnNullIfEmpty(klijentiFormEvent.getAdresa());
...
Sign up to request clarification or add additional context in comments.

3 Comments

I my declaration of method "public void searchKlijenta(KlijentiFormEvent klijentiFormEvent)" klijentiFormEvent is data from my field(String naziv = nazivField.getText()..), so that your code dont make what i want..check most be before sql statement..null most be in sql statement if if-statement runs..i have form for search clients i database, if textfield for something is empty i dont want to searh in database emtpy field i.e in search form must be typed some text..
Yes that work now thanks. I have question, what is better? 1. Your solution to call method: telefonK = returnNullIfEmptys(telefonK); or 2. nazivK = (nazivK.length() == 0) ? null : nazivK;
The first one is better because you don't repeat logic. If you have to change it, you have to do it at a single place. Besides in the second solution, you repeat 3 times the nazivK variable. It means that when you add a new control, you have to copy paste an existing instruction and change three time the variable name. It is more error prone.
0

You simply have to put your arrays/lists ... whatever those things are ... into another array or list.

Then you iterate that array/list.

Done.

And hint: your naming could be improved dramatically. Your names should indicate what the "thing" behind the variable actually is.

Comments

0

Also you can use Map<String, List<?>> to store your lists/arrays/strings. for example with List:

    Map<String, List<?>> map = new HashMap<>();
    map.put("nazivK", new ArrayList<>());
    map.put("adresaK", new ArrayList<>());
    //.....
    //replace all lists with null
    map.replaceAll((s, list) -> list.isEmpty() ? null : list);

    //or just remove it
    for(Iterator<Map.Entry<String, List<?>>> it = map.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<String, List<?>> entry = it.next();
        if(entry.getValue().isEmpty()) {
            it.remove();
        }
    }

Comments

0

As it was suggested by GhostCat, put your values into array/list. You can do for example something like this (I suppose those values are Strings):

/* Order in array nazivK, adresaK, gradK, drzavaK, telefonK,
   faxK, mailK, mobitelK, oibK, ugovorK, osobaK */
   String values[] = new String[11];
   for (String val: values) {
       if (val == null || val.length() == 0) {
           val = null;
       }
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.