0

When I tried to execute this statement, I got the error. I have created an array mem[] which contains table names, and in each table there are two columns with names amt(float(9,2)) and comb(varchar(5)).

I used this statement inside a for loop :

PreparedStatement pst =con.prepareStatement("select sum(amt) from ? where comb = ?");
pst.setString(1,mem[i]);
pst.setString(2, "all");

Error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''akhil' where comb = 'all'' at line 1

In the error description near akhil and all one side it is showing single quotation and other side double quotation. Why it is showing like that? What exactly is the error?

Eran
  • 387,369
  • 54
  • 702
  • 768
BHARATH RAJ
  • 69
  • 1
  • 10

1 Answers1

1

The parameters in a PreparedStatement can't replace table names or column names. They can only replace values. You'll need a separate PreparedStatement for each table.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • I have to access 6 tables.is there any way to write code shortly instead of writing it separately for each table – BHARATH RAJ Nov 22 '15 at 10:44