2

is it possible to set parameters in the SELECT clause in a prepared statement in Java? Something like this:

PreparedStatement prepStat = conn.prepareStatement("SELECT DISTINCT ? 'Typ' FROM employee");

    prepStat.setString(1, typ);
    ResultSet rs = prepStat.executeQuery();

    while (rs.next())
    {
        String strFilter = rs.getString("Typ");
        System.out.println("strFilter: "+strFilter);
    }

When I execute those lines, I only get the header of the column and not the values. When I execute the query without the question mark in SQL Server, it works fine.

coki1405
  • 21
  • 2

1 Answers1

0

Bind variables can only be used to bind values, not syntactic constructs or object names. The value you are binding is interpreted as a string literal, and since a literal only has one value, applying distinct to it will return a single line.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Ok, thank you. The problem is that with "normal" statements the application is really slow. Do you have a suggestion to make it faster? – coki1405 Feb 12 '16 at 08:16