Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking Database Names and Create Database at run time
    primarykey
    data
    text
    <p>I have one <code>JTextfield</code> for getting the database name to be created and <code>JButton</code> for performing database creation action in my swing form. The problem is when I give the value to the textfield and click the button it needs to check the entire database if the database exists with the same name that we gave to the new database. This means the existing database need to be deleted and the given name to be accepted for the new database. Here is my code.</p> <pre><code>package db1; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.sql.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Main implements ActionListener { JTextField txt; JButton create; JFrame frame; JPanel panel; JOptionPane jop; //Font font = UIManager.getFont("txt.font"); public Main() { frame=new JFrame(); panel=new JPanel(); txt=new JTextField(10); create=new JButton("create"); create.setBounds(20, 200, 50, 40); panel.add(txt); panel.add(create); create.addActionListener(this); frame.add(panel); // n.getContentPane().add(new textf()); frame.setSize(440,310); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { Connection con = null; try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vijay","root","root"); try{ String database=txt.getText(); Statement st = con.createStatement(); String check=null; ResultSet rs = con.getMetaData().getCatalogs(); while (rs.next()) { // System.out.println("" + rs.getString("TABLE_CAT") ); check=rs.getString("TABLE_CAT"); System.out.println(check); if(database.equals(check)) {I NEED CODE HERE} { String query = "DROP DATABASE"+check; st.executeUpdate(query); } else{ st.executeUpdate("CREATE DATABASE "+database); JOptionPane.showMessageDialog(frame,"DATA BASE IS CREATED"); } } } catch (SQLException s) { System.out.println(s); //JOptionPane.showMessageDialog(frame,"DATA BASE IS already exist"); } } catch (Exception ea){ ea.printStackTrace(); } } public static void main(String[] args) { new Main(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload