Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know the syntax to write a create table statement for MySQL where I give the table name there itself instead of it being a string variable. How do I write a variable in that statement?

What I have tried:

The syntax is something like this:

CREATE TABLE table_name ....

Here table_name is a predefined variable where a string is stored.
Posted
Updated 8-Dec-17 23:28pm

1 solution

You have to concatenate the table name into the string in your java Code:
Java
String sqlCommand = "CREATE TABLE " + table_name + " (Id INT, ...
But ... be aware that that leaves your code wide open to SQL Injection, so you need to check, verify, and carefully ensure that the variable contains a single name, and a single name only. If you don't you risk your DB being damaged or destroyed.
 
Share this answer
 
Comments
Member 13225387 9-Dec-17 8:12am    
Thanks, it worked perfectly.
OriginalGriff 9-Dec-17 8:15am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900