2 Replies - 424 Views - Last Post: 17 December 2011 - 05:29 AM Rate Topic: -----

#1 vijayakumarjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-December 11

DataBase creation mysql using java

Posted 17 December 2011 - 01:40 AM

I need correct IF condition logic in this following code..if i try to create already existing database name I need to perform delete statement query..next create database in same name..


package db1;



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/



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))
{
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();

}


}

Is This A Good Question/Topic? 0
  • +

Replies To: DataBase creation mysql using java

#2 pro_learner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-August 10

Re: DataBase creation mysql using java

Posted 17 December 2011 - 03:20 AM

Did you establish the connection properly?
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,300
  • Joined: 20-September 08

Re: DataBase creation mysql using java

Posted 17 December 2011 - 05:29 AM

Why would you not just use create database if not exists foo; ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1