Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create table dynamically in JDBC?

 
sudheer yathagiri kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,
i am trying to create table dynamically.
first import requried things.







The above code is work out for a specific fields in create table statement like
create table tablename(sno number(10),sname varchar2(20),sadd varchar2(20));
if the user want to enter String in the place of number and vice versa(number in the place of string ) it may not read by the methods
readLine(),read(),next(),nextLine()...respectively..
here ranges are also specified if user want beyond this range...
please help me to create a record dynamically...

urgent requirment ...
thanks in advance...

[JB: added code tags]
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use PreparedStatements for UPDATE/INSERT/SELECT/DELETE statements, but unfortunately not CREATE (not usually anyway). For those, you just have to construct the SQL yourself.
 
sudheer yathagiri kumar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yah i am using that statements also ...
"create table"+tabname+"(?,?,?)";

here tabname is the tablename which is read dynamically by using scanner class.
here 1st ? ,2nd ?,3rd ? represents sid,sname.sadd respectively for example... we are createing table dynamically na the user dont know the datatype of the field..so it may enter string in place of number and vice versa... how can i solve the problem..

thanks in advance..
 
Sudheer Bhat
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't know the datatype, then always create the columns as VARCHAR2. Later, while processing the data, if you are expecting number from column1 then do a to_number on the column1, if you are expecting date, do a to_date on the varchar2 field and so on..
 
Scott Selikoff
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudheer yathagiri kumar wrote:yah i am using that statements also ...
"create table"+tabname+"(?,?,?)";

here tabname is the tablename which is read dynamically by using scanner class.
here 1st ? ,2nd ?,3rd ? represents sid,sname.sadd respectively for example... we are createing table dynamically na the user dont know the datatype of the field..so it may enter string in place of number and vice versa... how can i solve the problem..

thanks in advance..


As I said, you can't use PreparedStatements, you have to construct the SQL string manually.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic