populate JTable with the value selected in Jcombobox

好久不见. 提交于 2019-12-02 13:07:14
String n=(String)comboBox.getSelectedItem();
String sql="SELECT .... WHERE student.first_name=n)";

That code doesn't do anything with the variable "n". You can't just include "n" as part of the String because all the have is the character "n", not the value of the variable.

You were given a link to a tutorial on using a PreparedStatement in your last question (Populate JTable with the value in JCombobox).

So where do you use the "?" which indicates you want to provide a dynamic value to the SQL statement?

Your basic code should be:

String sql="SELECT ..... where student.first_name = ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, n);
ResultSet rs = ps.executeQuery();

Now the value of the variable "n" will replace the "?" in the SQL string.

Assuming the rest of your SQL is correct then it should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!