0

I keep getting this error when trying to connect to the database.

This is my prepared statement

String SQL = "SELECT * FROM `?` WHERE `HomeTeam` = '?'";
        PreparedStatement prepst;            

        prepst =  con.prepareStatement(SQL);
        prepst.setString(1,box1.getSelectedItem().toString());
        prepst.setString(2,box1.getSelectedItem().toString());
        rs = prepst.executeQuery();

Anyone know why I get this error?

0

2 Answers 2

1

I think that your problem is in ' and ``` symbols. You should fix the sql as follwing:

String SQL = "SELECT * FROM ? WHERE HomeTeam = ?";

However I am not sure that parameter placeholder ? is supported after from. So, propbably you will have to put it yourself, e.g.:

String table = box1.getSelectedItem().toString();
String SQL = "SELECT * FROM " + table + " WHERE HomeTeam = ?";
Sign up to request clarification or add additional context in comments.

Comments

0

Use

String SQL = "SELECT * FROM ? WHERE HomeTeam = ?";

Don't use ` to nest parameters, use ' to nest values you're comparing against if you're hard coding them.

You can't use ? to specify a table name.

2 Comments

Ok,I've tried that and now I get a MySQL syntax error although the SQL statement works when I tried it manually on the database
You can't use ? to specify a table name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.