Can't use a variable for a column name?

From: Chris <cmattmiller(at)gmail(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Can't use a variable for a column name?
Date: 2008-04-24 17:25:16
Message-ID: eac7b0710804241025k75e9aea3mef7f02c95c5a9c78@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

A user enters a name into a textfield and clicks on a "Find" button.
Depending on which text field the user entered the data, the appropriate
column name in the table is used for fieldName and the entered text is
passName. However, the fieldName doesn't return anything. But if I replace
fieldName with the column name ("WHERE first_name='"), the program returns
values. Can't we use variables for column names or do I have to just put it
all in an if/else statement?

Here is my code:

result = fe.executeQuery("SELECT first_name, last_name,
emp_nbr, emp_type_code, emp_status_code, emp_work_center " +
"FROM employee " +
"WHERE
'"+fieldName+"'='"+passName+"'");

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Oliver Jowett 2008-04-24 21:19:52 Re: Expressiveness of SQLException
Previous Message Daniel Migowski 2008-04-24 15:06:37 Re: Expressiveness of SQLException