
In my project,i want the user to specify the name of the database he wants to create in mysql and then also the name of the table along with the column names he wants in that database....Im doing my project in netbeans...swings.......If it is possible kindly help me Thank You...

Java Create database and table using Java Swing
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class CreateDatabase
{
public static void main(String[] args)
{
JFrame f=new JFrame();
JLabel lab=new JLabel("Enter Database name: ");
JLabel lab1=new JLabel("Enter Table name: ");
final JTextField text=new JTextField(20);
final JTextField text1=new JTextField(20);
JButton b=new JButton("Create");
f.setLayout(null);
lab.setBounds(10,10,150,20);
text.setBounds(180,10,150,20);
lab1.setBounds(10,40,150,20);
text1.setBounds(180,40,150,20);
b.setBounds(180,70,100,20);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String st=text.getText();
String st1=text1.getText();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "root");
Statement stmt=con.createStatement();
String sql = "CREATE DATABASE "+st;
stmt.executeUpdate(sql);
stmt.close();
con.close();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+st, "root", "root");
Statement sta = conn.createStatement();
String table = "CREATE TABLE "+st1+"(id integer, name varchar(10))";
sta.executeUpdate(table);
JOptionPane.showMessageDialog(null, "Database and table are created successfully");
}
catch(Exception ex){
System.out.println(ex);
}
}
});
f.add(lab);
f.add(text);
f.add(lab1);
f.add(text1);
f.add(b);
f.setVisible(true);
f.setSize(350,180);
}
}
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.