File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Adding Columns dynamically through UI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Adding Columns dynamically through UI" Watch "Adding Columns dynamically through UI" New topic

Adding Columns dynamically through UI

hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 64
How to add columns dynamically through UI?..

I am just doing simple exercises on Servlets and JSP's.

I have a doubt:

Created One table called "Product" and added few columns like "ID, Name etc"..

Now i want to add new columns through UI Itself, can we achieve tat?..

For ex: create a page called "Add column" , In that we have to provide

Column Name(Price) and Data type(Integer) for the new column
.. once click on create button, It should create a new column in our existing table..
Rob Spoor

Joined: Oct 27, 2005
Posts: 20275

You'll need to execute an "ALTER TABLE" query. The exact syntax depends on the database server used, but in general it's something like this:
That last NULL indicates the column can have NULL values. Use "NOT NULL" instead to indicate it cannot have NULL values, but that's only possible if you have no rows yet, unless you specify a default value:
Final part of the lesson: you can also change existing columns, to change the type, allow/disallow NULL values or change the default:

How To Ask Questions How To Answer Questions
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 64
I want to achieve this through User Interface Itself..

not like through query.. i want to do some logic in JDBC Code to add column dynamically..

Like I created one JSP

Column Name: ***
Data Type : ***

and add one button, when we click on tat button, i should add new column dynamically..

am i clear?
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

Yes. That is what Rob is suggesting.
The UI can grab what the user enters. Then, your code uses JDBC to send an ALTER TABLE... statement to your database.

OCUP UML fundamental and ITIL foundation
hariharan jayaprakash
Ranch Hand

Joined: Sep 09, 2010
Posts: 64
oh okay thanks .. i ll start tryin to implement..
I agree. Here's the link:
subject: Adding Columns dynamically through UI
jQuery in Action, 3rd edition