SQL in java returns invalid data

I have a java method like below:

public String qE (String query, String selector) throws QSLException,    IOException{

//I get my sqlQuery from properties
String sqlQuery = properties.getPRoperty(query);
//sqlQuery = SELECT count(?) FROM employees WHERE ? is not null

PreparedStatement ps = conn.preparedStatement(sqlQuery);
ps.setFetchSize(100);
ps.setString(1,selector);
ps.setString(2,selector);

ResultSet rs = ps.executeQuery();

String rs = "";

while(rs.next()){
queryValue = rs.getString(1);
}

return queryValue;
}

When I run it with qe parameters (employees, second_name), then this request should be executed:

SELECT count(second_name)
FROM employees
WHERE second_name is not null

The problem is that not the employees have a middle name, and I should get 0, and the whole method should return 0, but I always get a great number greater than zero.

Can someone tell me why this doesn't return 0, but always a great number, like 2399 for example?

+4
source share
1 answer

A ?represents a value not the name of an object, so it is equivalent to using

SELECT count('second_name')
FROM employees
WHERE 'second_name' is not null

. , employees.

. , ( ). , SQL-, (, ).

+8

Source: https://habr.com/ru/post/1695059/


All Articles