java - Prepared Statement with Null Parameters -
i'm working on little database programme witch create easy select querys.
the programme utilize gui can come in search parameters. if don't come in parameters completed results of query. tried check wether parameter textbox empty/null , set placeholder "*".
but when tried run. programmed placeholders give me sqlexception syntax error.
i post shortend version of whole code witch technaly same.
preparedstatement statement = connection.preparestatement("select * t_person user_a = ? "+ "and dasatz_a = ? " + "and user_g = ? "); if (parameter.get(0) == null) { parameter.set(0, "*") }; statement.setstring(1, parameter.get(0)); and on.
parameter arraylist parameters textboxes.
simply don't filter column in way if there no parameter specified "filtered" column. dynamically prepare query inlude parameters specified.
map<integer, string> statementparams = new hashmap<>(); string query = "select * t_person"; boolean firstcondition = true; if (parameter.get(0) != null) { if (firstcondition) { query += " "; firstcondition = false; } else { query += " , "; } query += "user_a = ?"; statementparams.put(1, parameter.get(0)); } // other params preparedstatement statement = connection.preparestatement(query); (mapentry<integer, string> entry : statementparams.entryset()) { statement.setstring(entry.getkey(), entry.getvalue()); } java sql prepared-statement
No comments:
Post a Comment