
I wanted to know if it is possible to take the fields of a table from a user and then create a table in mysql database......im doing my project in swings netbeans Plz help me ThankYou

Java create table in database and add fields to it
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class CreateTable
{
public static void main(String[] args)
{
JFrame f=new JFrame();
JLabel lab=new JLabel("Enter Table name: ");
final JTextField text=new JTextField(20);
JButton b=new JButton("Create");
f.setLayout(null);
lab.setBounds(10,10,150,20);
text.setBounds(180,10,150,20);
b.setBounds(180,40,100,20);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String st=text.getText();
String num=JOptionPane.showInputDialog(null, "How many fields do you want to add in table "+st);
int no=Integer.parseInt(num);
String field[]=new String[no];
String type[]=new String[no];
for(int i=0;i<no;i++){
field[i]=JOptionPane.showInputDialog(null,"Enter field "+i);
type[i]=JOptionPane.showInputDialog(null,"Enter its datatype");
}
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement stmt=con.createStatement();
String table = "CREATE TABLE "+st+"(id integer)";
stmt.executeUpdate(table);
stmt.close();
Statement sta=con.createStatement();
for(int i=0;i<no;i++){
System.out.println(field[i]+" "+type[i]);
sta.executeUpdate("ALTER TABLE "+st+" ADD "+field[i]+" "+type[i]);
}
JOptionPane.showMessageDialog(null, "Table is created successfully");
}
catch(Exception ex){
System.out.println(ex);
}
}
});
f.add(lab);
f.add(text);
f.add(b);
f.setVisible(true);
f.setSize(350,100);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.