Getting column name instead of values in prepared statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Getting column name instead of values in prepared statement

I am getting column name instead of values in prepared statement. here is my piece of code: string q="select ? from EMPLOYEE where salary>?"; prepared pst=connectionobject.preparedstatement(q); pst.setstring(1,"FIRST_NAME"); pst.setint(2,10000); please help

24th Jan 2017, 3:57 PM
Bhuvanesh Desai
Bhuvanesh Desai - avatar
12 Answers
+ 1
I want to set values, I figured out a way using string concatenation but I wonder why set doesn't work properly....
31st Jan 2017, 6:22 PM
Bhuvanesh Desai
Bhuvanesh Desai - avatar
+ 1
sorry I meant get...
31st Jan 2017, 6:25 PM
Bhuvanesh Desai
Bhuvanesh Desai - avatar
+ 1
I don't want to get the name from resultset containing other columns instead I want the resultset to contain only the name without any other column,... if that makes sense
1st Feb 2017, 10:00 AM
Bhuvanesh Desai
Bhuvanesh Desai - avatar
+ 1
just tell me how to set first "?" value correctly(that is select ? from employee where salary>?) I am saying this because actually in my program I set the column name dynamically from jcombobox
1st Feb 2017, 10:05 AM
Bhuvanesh Desai
Bhuvanesh Desai - avatar
+ 1
thanks for the reply. by the way that's what I did string concatenation... so you are saying there is no way we can set ? using setxxx() method
1st Feb 2017, 10:29 AM
Bhuvanesh Desai
Bhuvanesh Desai - avatar
0
do you want to get or set the values? I would do it ways different
31st Jan 2017, 8:09 AM
raz0rblade
raz0rblade - avatar
0
you need 'pst.executeUpdate()' in the end, and maybe a commit
31st Jan 2017, 6:24 PM
raz0rblade
raz0rblade - avatar
0
statement st= st.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM EMPLOYEE WHERE salary>10000"); while(rs.hasnext()) { string name = rs.getString("name"); ..... } kind of this, you are trying to set the values in your code..
31st Jan 2017, 6:32 PM
raz0rblade
raz0rblade - avatar
0
working now?
31st Jan 2017, 10:07 PM
raz0rblade
raz0rblade - avatar
0
yeah Just change the select statement to: SELECT name FROM ..then you get just the name, but you still should use the while loop
1st Feb 2017, 10:02 AM
raz0rblade
raz0rblade - avatar
0
easiest way is: st.executeQuery("SELECT " +column name+ " FROM EMPLOYEE WHERE salary > " + salary);
1st Feb 2017, 10:14 AM
raz0rblade
raz0rblade - avatar
0
yes you can set values with the set method but you need to execute the set commands with executeUpdate() method in the end, but then you set the value in the database
1st Feb 2017, 10:34 AM
raz0rblade
raz0rblade - avatar