
Im doing project in netbeans swings......you helped me to create a table with user defined column names.....i wanted to know if it is possible to let the user insert his data into that table.....Plz help me if it is possible

Java JDBC create table
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]);
}
int id=Integer.parseInt(JOptionPane.showInputDialog(null, "Enter value for field1"));
String name=JOptionPane.showInputDialog(null, "Enter value for field2");
String address=JOptionPane.showInputDialog(null, "Enter value for field3");
Statement statement=con.createStatement();
sta.executeUpdate("insert into "+st+" values("+id+",'"+name+"','"+address+"'");
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);
}
}

Java JDBC create table
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]);
}
int id=Integer.parseInt(JOptionPane.showInputDialog(null, "Enter value for field1"));
String name=JOptionPane.showInputDialog(null, "Enter value for field2");
String address=JOptionPane.showInputDialog(null, "Enter value for field3");
Statement statement=con.createStatement();
statement.executeUpdate("insert into "+st+" values("+id+",'"+name+"','"+address+"'");
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.