I'm editing SQL in JAVA but keep getting ERROR
If I'm understanding it correctly, if state is OK I should get
SELECT name FROM inventura_table where state='O'
when I execute same select by script it works just fine
Connection conn = DriverManager.getConnection(url, user, password);
String SQL = "SELECT name FROM inventura_table where ?";
PreparedStatement pstmt = conn.prepareStatement(SQL);
ResultSet rs;
switch (state) {
case "OK":
pstmt.setString(1, "state='O' ");
break;
case "M":
pstmt.setString(1, "state='M' ");
break;
case "V":
pstmt.setString(1, "state='V' ");
break;
case "removed":
pstmt.setString(1, "out_date IS NOT NULL");
break;
}
Error
ERROR: argument of WHERE must be type boolean, not type character varying
CodePudding user response:
What you are trying is not possible. You can only set values to conditions, not create new conditions.
You could do something like this:
Connection conn = DriverManager.getConnection(url, user, password);
String SQL = "SELECT name FROM inventura_table";
if (state.equals("removed")) {
SQL = " where out_date IS NOT NULL";
}
else {
SQL = " where state = ?";
}
PreparedStatement pstmt = conn.prepareStatement(SQL);
ResultSet rs;
switch (state) {
case "OK":
pstmt.setString(1, "O");
break;
case "M":
pstmt.setString(1, "M");
break;
case "V":
pstmt.setString(1, "V");
break;
}