-1

I am writing a program, where the user will be receiving instructions in different languages. I have the tables structures by language, so based on the user's language user settings, the corresponding language table will be selected. However, I keep getting errors from the following code.

            String query2 = "select * from ? where instruction_id = ?";
            PreparedStatement pst2 = connection.prepareStatement(query);
            pst2.setString(1, user_config.language);
            pst2.setString(2, instruction_id);
            ResultSet rs2 = pst2.executeQuery();

Can someone explain why the above code is not working?

drake
  • 259
  • 4
  • 17
  • define "errors", define "not working" Please read http://stackoverflow.com/help/how-to-ask – reto Jun 22 '15 at 15:22
  • 1
    It's not possible to have table names parametrized, see http://stackoverflow.com/questions/7541761/table-name-as-parameter-using-pdo-mysql-prepared-statement – JP Moresmau Jun 22 '15 at 15:23
  • I would not recommend using a database for internalization like this - instead, I'd go with this approach: https://docs.oracle.com/javase/tutorial/i18n/resbundle/index.html – Emile Pels Jun 22 '15 at 15:27
  • This post was more about the actual syntax that I did. I've done the above format several times and I've had no issues except for this one, where the table name is a variable. Why would the table name variable be treated differently? – drake Jun 22 '15 at 15:29
  • because it is wrapped in '' – nano_nano Jun 22 '15 at 15:30
  • I changed query to query2 in the PreparedStatement declaration. Now I am getting this error: [SQLITE_ERROR] SQL error or missing database (near "?": syntax error) – drake Jun 22 '15 at 15:35

1 Answers1

0

I ended up using string concatenation for the table name.

      String query2 = "select * from " + user_config.language + " where instruction_id = ?";
      PreparedStatement pst2 = connection.prepareStatement(query2);
drake
  • 259
  • 4
  • 17