How can I execute this statement and process it java. Is it possible to get a return value and then the next resultset which is the select to employee in the way I'm doing it?
I cannot find the accurate way on Google to perform what I want because all of the examples are results with single SELECTs and cannot find a query with RETURN from DB. But according to this question, it is possible to manage multiple result sets from DB (java) as .NET can do.
I'm using postgresql 9.4 and I don't want to use a stored proc (function) to do what I'm trying to do.
This is the code that I've been trying to test, but I get an exception that there is a syntax error in 'IF' line 1
public Employee getEmployee(Connection con, String code) {
Employee employee = new Employee();
try {
String query =
"IF EXISTS(SELECT * FROM employee WHERE code = ?) THEN "
+ "RETURN 1; "
+ "ELSE "
+ "RETURN 2; "
+ "END IF; "
+ "SELECT EmployeeID, FirstName FROM employee where code = ?; ";
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, code);
stmt.setString(2, code);
boolean hasResults = stmt.execute();
int returnValue = 0;
while(hasResults){
ResultSet rs = stmt.getResultSet();
returnValue = rs.getInt(1);
if(returnValue == 1){
hasResults = stmt.getMoreResults();
while(hasResults){
employee.setId(rs.getInt("EmployeeID"));
employee.setFirstName(rs.getString("FirstName"));
}
}
}
return employee;
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}