views:

621

answers:

2

Im trying to use prepared statements to set a table name to select data from but i keep getting an error when i execute the query.

The error and sample code is displayed below.

[Microsoft][ODBC Microsoft Access Driver] Parameter 'Pa_RaM000' specified where a table name is required.



private String query1 = "SELECT plantID, edrman, plant, vaxnode FROM [?]"; //?=date
public Execute(String reportDate){
 try {

  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  Connection conn = DriverManager.getConnection(Display.DB_MERC);
  PreparedStatement st = conn.prepareStatement(query1);
  st.setString(1, reportDate);
  ResultSet rs = st.executeQuery();

Any Thoughts on what might be causeing this?

+3  A: 

I believe the table name can't be used as a parameter. It must be hard coded. So you can do something like:

private String query1 = "SELECT plantID, edrman, plant, vaxnode FROM [" + reportDate + "?]";
camickr
Ok thanks, guess ill just use a string replacementAccepted your response as answerThanks!
Brandon
A: 

I'm not sure you can use a PreparedStatement to specify the name of the table, just the value of some fields. Anyway, you could try the same query but, without the brackets:

"SELECT plantID, edrman, plant, vaxnode FROM ?"
Pierre
You need brackets to escape "/" in queries...or it might be be for dates. I looked it up last summer
Brandon