michaalis
michaalis

Reputation: 131

I have stuck with the simplest SQL query in java

I can't find the correct syntax of the following query in java,please help me.

String st = "SELECT COUNT('"+id+"') FROM '"+selected_table+"' ";

String st = "SELECT COUNT('"+id+"') FROM '"+selected_table+"'";

I think that the mistake is how to end the query...

Since I got the error Check the manual that corresponds to your MySQL server version for the right syntax to use near ''Customer'' at line 1

when I choose Customer table

Upvotes: 1

Views: 197

Answers (4)

Jer In Chicago
Jer In Chicago

Reputation: 828

Looks from your query that you are enclosing your id and selected_table in single quotes... For example, SELECT COUNT('ID') FROM 'CUSTOMER' which is wrong. should be in backtics `` or nothing...

Upvotes: 0

Thom Smith
Thom Smith

Reputation: 14086

What are the values of id and selected_table? What is the actual query string that is sent to the database?

Also, it's rarely a good idea to manually build a query like this using string concatenation. This makes it very easy for a bug to result in a gaping security hole, and it's a lot more difficult (and risky) to try to secure this approach than it is to just do it right.

Upvotes: 0

mellamokb
mellamokb

Reputation: 56769

Table names should be surrounded by tick marks (`), not single quotes (')

String st = "SELECT COUNT('"+id+"') FROM `"+selected_table+"`";
                                         ^ use tick marks   ^

Upvotes: 0

Joe Stefanelli
Joe Stefanelli

Reputation: 135739

You want to use backticks instead of single quotes around your object names.

String st = "SELECT COUNT(`"+id+"`) FROM `"+selected_table+"` ";

Upvotes: 2

Related Questions