enter image description here我有这个查询,我正在使用它来获取我的数据。
public void loadQuestions() {
try {
String sql = "select * from "
+ hommer.getSelectedItem()
+ "where id= ?";
pst = conn.prepareStatement(sql);
pst.setString(1, getCount());
rs = pst.executeQuery();
if (rs.next()) {
question_space.setText(rs.getString("questions"));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
如果我要使用String sql = "select * from list_of_questions where id =?";
,它肯定会从所选表中提供所需的数据,但我确实希望从用户选择的字符串中获取表名。
以上是我看到的错误,如果我应该调用loadQuestions()
方法。
我一定会欣赏各种姿态。
答案 0 :(得分:1)
我实际做的只是在@JoshH建议的where语句中添加一个空格。
public void loadQuestions() {
try {
String sql = "select * from "
+ hommer.getSelectedItem()
+ " where id= ?";
pst = conn.prepareStatement(sql);
pst.setString(1, getCount());
rs = pst.executeQuery();
if (rs.next()) {
question_space.setText(rs.getString("questions"));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
这有助于得到我真正想要的东西。