简体   繁体   中英

java sql SQLException: Parameter index out of range (1 > number of parameters, which is 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?

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, eg:

String table = box1.getSelectedItem().toString();
String SQL = "SELECT * FROM " + table + " WHERE HomeTeam = ?";

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM