1

code:

String myTblN = fileN[0];

PreparedStatement creatTableStmt = 
    conn.prepareStatement("create table " + myTblN +" like stocktradtbl");
System.out.println(creatTableStmt);
creatTableStmt.execute();

error:

com.mysql.cj.jdbc.ClientPreparedStatement: create table 00002 like stocktradtbl java.sql.SQLSyntaxErrorException: 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 00002 like stocktradtbl' at line 1

How to use value of variable myTblN in java as table name of mysql?

1
  • 1
    00002 is not a valid identifier, since identifiers must start with a letter. Commented Feb 28, 2019 at 2:03

2 Answers 2

2

check the manual that corresponds to your MySQL server version for the right syntax to use near 00002 like stocktradtbl' at line 1

It looks like you are trying to create a table whose name contains only digits. AS explained in the documentation, MySQL by default does not allow digits at the beginning of the table name.

One quick solution would be to add a fixed alphabetic character at the beginning of the table name :

PreparedStatement creatTableStmt = 
    conn.prepareStatement("create table t" + myTblN +" like stocktradtbl");

Or you need to quote the identifier (but then be prepare to quote the table name in every subsequent query, which can be painful):

PreparedStatement creatTableStmt = 
    conn.prepareStatement("create table `" + myTblN +"` like stocktradtbl");
Sign up to request clarification or add additional context in comments.

Comments

2

Your values need to be quoted with backticks or double-quotes

"create table `" + myTblN +"` like `stocktradtbl`"

Also as per @kartik

Also, you may want to add the reason that 00002 consists only of numbers, and mysql allows number-only identifiers only if they are quoted.

2 Comments

I think it should be quoted with backticks, not apostrophes
Also, you may want to add the reason that 00002 consists only of numbers, and mysql allows number-only identifiers only if they are quoted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.