1

how to create user defined table (table name is in a string ) oracle using java

String Tablename="student";
stmt=con.prepareStatement("create table"+Tablename+"(name varchar(12),class varchar(12))");
stmt.executeUpdate();

with out string i.e user defined table name it works fine:

stmt=con.prepareStatement("create table student (name varchar(12),class varchar(12))");

but the above listed versions which takes string as user name shows following error

java.sql.SQLException: ORA-00901: invalid CREATE command

2 Answers 2

3

Leave spaces before and after the table name, please.

Your original code resulted in this SQL command getting executed:

create tableMyTablename(name varchar(12),class varchar(12))

And in that, the tableMyTablename was not comprehensible for the DB...

Correctly:

stmt=con.prepareStatement("create table "+Tablename+" (name varchar(12),class varchar(12))");

Also, a few notes:

  • Java naming: variable names start with lowercase letters
  • this (preparing the statement) doesn't really make sense. It is not reusable to have a statement that can only create the same structure for a table.
  • it is wise to avoid the + to concatenate Strings, and other objects' string representations... It may have very unexpected results, even compile errors...
  • logging: in such situations, it is always wise to log the created SQL commands. Even a (yuck!) System.out.println(); is better than nothing...
Sign up to request clarification or add additional context in comments.

Comments

2

Put a space after "create table" i.e.

"create table "

Your statement was being evaluated as create tablestudent...

Comments

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.