SQL Query works in SQL Developer, but not on the Java side -
i'm trying figure without success. i'm trying find every row in specific table contains value.
for example, let's consider employee table:
| id | first_name | last_name | date_of_birth | car_number | |------------------|------------|------------|---------------|------------| | 10001 | john | washington | 28-aug-43 | 5 | | 10083 | arvid | sharma | 24-nov-54 | null | | 10034 | david | johnson | 12-may-76 | |
i'm building query on java side (this print in log):
select * employee id ? or first_name ? or last_name ? or date_of_birth ? or car_number ?
then use prepared statement that, if search string 'oh' becomes:
select * employee id '%oh%' or first_name '%oh%' or last_name '%oh%' or date_of_birth '%oh%' or car_number '%oh%'
here's corresponding code:
string wantedquery = "select * " + tablename + " where"; preparedstatement preparedstatement; try(resultset rscolumns = columnsfortable(tablename)) { string keyword = keywordfield.gettext(); int limit = 0; while (rscolumns.next()) { wantedquery += " " + rscolumns.getstring(1) + " ? or"; limit++; } wantedquery = wantedquery.substring(0, wantedquery.length()-3); preparedstatement = con.preparestatement(wantedquery); system.out.println(wantedquery); system.out.println("\'%"+keyword+"%\'"); for(int = 1; <= limit; i++) {; preparedstatement.setstring(i, "\'%"+keyword+"%\'"); } } try(resultset rs = preparedstatement.executequery()) { //now results here resultsetmetadata metadata = rs.getmetadata(); while (rs.next()) { system.out.println("fetching row"); ... }
now problem when try results, gives me 0 rows (i never see "fetching row" printed) query works in sql developer. guess error on java side have clue can be. ideas?
when using setstring()
, jdbc knows must string, puts single quotes around string
needed, don't have yourself. because so, different one, expect.
Comments
Post a Comment