Derin S
Derin S

Reputation: 55

Misuse of query

enter image description hereI have this query which i am making use of to fetch my data.

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);
        }
    }

if i am to make use of String sql = "select * from list_of_questions where id =?"; it will definitely give out the required data from the selected table, but i do like to get the table name from a string selected by the user.

Above is the error i am seeing if i should call the loadQuestions() method. I will definitely appreciate every kind gesture.

Upvotes: 0

Views: 61

Answers (1)

Derin S
Derin S

Reputation: 55

What i actually did was just to add a space to the where statement as suggested by @JoshH.

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);
        }
    }

This helped to get what i actually wanted.

Upvotes: 1

Related Questions