
im doing my project in netbeans swings...wn a user wants to create a database from the gui...i want to display an error msg if a database with that name already exists and if it does not exist new database should be created

Java Create Database
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: ");
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();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement stmt=con.createStatement();
String sql = "CREATE DATABASE "+st;
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Database is created successfully");
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, "Database already exists!");
}
}
});
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.