This is not possible, with your way so to solve your problem.
Change your code like this :
String att = "FIRST_NAME";
string q="select " + att + " from EMPLOYEE where salary>?";
Preparedstatement pst=connectionobject.preparedstatement(q);
pst.setint(1,10000);
IF YOU AFRAID THAT THE USER CAN MAKE AN SQL Injection than use this solution
You should to check if your column exist in your table or not :
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND TABLE_NAME = 'table_name'
AND COLUMN_NAME = 'column_name'
Like so :
public boolean column_exist(String att) {
boolean succes = false;
CreerConnection con = new CreerConnection();
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultat = null;
try {
connection = con.getConnection();
statement = connection.prepareStatement("SELECT * \n"
+ "FROM information_schema.COLUMNS "
+ " WHERE"
+ " TABLE_SCHEMA = 'db_name'"
+ " AND TABLE_NAME = 'table_name'"
+ " AND COLUMN_NAME = ?");
statement.setString(1, att);
resultat = statement.executeQuery();
if (resultat.next()) {
succes = true;
}
} catch (SQLException e) {
System.out.println("Exception = " + e);
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException ex) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException ex) {
}
}
}
return succes;
}
If the column exist then you can continue like so:
if(column_exist(att)){
string q="select " + att + " from EMPLOYEE where salary>?";
Preparedstatement pst=connectionobject.preparedstatement(q);
pst.setint(1,10000);
}
Learn more here :
MySQL, Check if a column exists in a table with SQL
Hope this can help you.