Question

I am trying to create a generalized UPDATE-statement like this, where only the table-name is fixed.

updateValueQuery = conn.prepareStatement("UPDATE TABLENAME SET (?)=(?)");

That fails with an SQLException complaining about syntax. As soon as I specify the column-name like this:

updateValueQuery = conn.prepareStatement("UPDATE TABLENAME SET COL_NAME=(?)");

SQL happily compiles. Is there any way to make the columnname anonymous as well?

I am using Apache derby.

Was it helpful?

Solution

No, PreparedStatement has holders for values only. I resolved similar problem in following way:

    private final String FIND_STRING = "select * from TABLENANE where {0} = ?";
       .
       .
       .
    private final Map<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>();

    private PreparedStatement prepareStatement(String field, String toFind) throws SQLException{
       PreparedStatement statement = null;
       if (statements.contains(field)){
          statement = statements.get(field);
       }else{
          String findInHelpDefinition = MessageFormat.format(FIND_STRING, field));
          statement = connection.prepareStatement(findInHelpDefinition);
          statemnts.put(field, statement);
       }
       statement.setString(1, toFind);
       return statement;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top