Frage

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?

War es hilfreich?

Lösung

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 = ?";

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top