hello.
i created a java app which enables one to create a database table using the name inputted by the user.But the problem i have is that i dont know how to make the column names of the table set to the name prefered by the user i.e the user wants to set the column name when creating the table by inputtin the name in a jtextfield so that when the table has been created,columns will habe the names inputted by the user.

Can somebody just give me an example on how to create a db table with column names all inputted by the user via a textfield?

For this, you have to use Servlets or Struts or Spring (to write controllers) because you have to retrieve the value(column name input by user) from the text box and inside the controller you have to write JDBC code to create table using the retrieved values.

Thanks for replying but the things is this,i dont know how to use struts or spring and as for servlets thats JSP.All i'm doing here is build a desktop app.i just can't decide a column name for the db table being created by the user.i really need help.i have read books but none actually talks about something like this.All they talk about is how to create the table with the name the user inputs into the textfield as table name,no talks about what if he wants to name his columns,will he have to decompile the class file that is if he ever understands java and is allowed to do so without it being a breach of policy.U understand.

i wrote something like this and i get an exception

String tblcol1 = textfield.getText();// table column
String tblcol2 = textfield2.getText();// table column
String tblcol3 = textfield.getText();// table column


String table = "CREATE TABLE" + table + "(tblcol1, tblcol2,tblcol3)";

//this is surrounded with a try and catch block

//the exception says: cannot create table.please check the syntax used near (tblcol1,tblcol2,tblcol3). 
 String tblcol1 = textfield.getText();// table column
 String tblcol2 = textfield2.getText();// table column
 String tblcol3 = textfield.getText();// table column
 String table = "CREATE TABLE" + table + "(tblcol1, tblcol2,tblcol3)";

ow key, the above is really, really weird. you are using the value of table before it actually has a value. and you are using "tblcol1" instead of the value stored in the String tblcol1. let's say you are using the above names to create three cols of integers

try:

String type1 = "Integer";
String table = "UserTable";
String statement = "CREATE TABLE " + table + "(" + tblcol1 +" " + type1 + ", " + tblcol2 +" " + type1 + ", " + tblcol3 + " " + type +")";

@stultuske: Brother, you are simply the best!!.Taking a good look at what you just wrote told me that i was writing the right thing the wrong way.you have just save me some sleep.if this next question won't be such a problem, can you just show me how any text inputted by a user into a textfield will be added to a comboBox as an item and how another comboBox like say ComboBox B will have the same item with comboBox A as items are been added from the textfield?.

Thanks a zillion

You can add an item to a JComboBox by using its addItem method, eg
myComboBox.addItem(myTextField.getText());
You can repeat that for both combos to add the item to both.

@JamesCherrill: let me try that and getback to you.thanks man

Thanks y'all for your help.Now here's comes a question that really messes my brains up.

i wrote this:

String words[] = "select word","hello","hi","bye","goodnight";
textf = new JTextField();
textf2 = new JTextField();
combo = new JComboBox(words);

// disabling textfields

textf.setEnabled(false);
textf2.setEnabled(false);

// setting conditions

String f = (String)combo.getSelectedItem();
if(f.equals("select word")) {
System.out.print("please select the appropriate salutation");
}else if(f.equals("hello")){
 textf.setEnabled(true);
}
if(f.equals("hi")){
textf2.setEnabled(true);
texf.setEnabled(false);
}

the problem here is that though i have issued the enabling command on the textfields at different times,they still remain disabled.As in,the app totally ignored the textf.setEnabled() methods.

i need help and i dont find this funny.

String words[] = "select word","hello","hi","bye","goodnight";

That code looks reasonable, as far as it goes (apart from the invalid literal string array) so the problem may well be in some part of the code that you didn't post.
Did you try putting a debug print after the setEnabled calls to confirm that they awere being executed?

@JamesCherrill: hello again bro.The thing is this,i have a jcombobox with items: "select one..", "select by name" and 'select by alphabet" with 2 textfields; textfieldA and textfieldB which are currently disabled. The user on selecting "select by name" option,textfieldA should be enabled will textfieldB should remain disabled but if he selectes "select by alphabet" option, textfieldB should be enabled and A,disabled. but it aint working.can u show me an example.it sucks i know.

Introduce itemStateChanged listener for jcombobox and put the code(Line no 13 t0 22) inside the listener

@softswing: i'll try it.

@softswing: that method returns an int.should i have to write it like this

public void (ItemEvent e){
int command = e.getItemChange();
// put code line 13 to 20 here
}

if not,pls show me how because i know that one has to assign the e.getItemChange() to an integer.

if not,pls show me.

 combo.addItemListener(new ItemListener(){
  public void itemStateChanged(ItemEvent ie){
String f = (String)combo.getSelectedItem();
if(f.equals("select word")) {
System.out.print("please select the appropriate salutation");
}
else if(f.equals("hello")){
 textf.setEnabled(true);
}
if(f.equals("hi")){
textf2.setEnabled(true);
texf.setEnabled(false);
}
 }
 });

Try this

@softswing: thanks man but i think this can be written in the ame code where the combobox resides.what if i told u that i'm using an MVC model and the C is ..u already know.i have an action class that implements ActionListener in which there is a button that triggers that event when that selection is been made.we all know that buttons use actionPerformed(ActionEvent e).now should i write something like this

public void actionPerformed(ActionEvent t){
 String com = t.getActionCommand();
 if(com.equals("button_name")){

 // the itemStateChanged() with its body here.
 }

because i cant do it like that.pls help me bro.

i have solved it.i decided to create a separate class which should do the selection mode and another class which will have a combobox that should contain my db table names with the txtfields which will contain data to be insert.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.