import java.sql.*;
import javax.swing.*;
public class SQLCreateTable {
  public SQLCreateTable(String tname) {
    Connection c;    SQLConnection MyCon;
    Statement stmt;  String SQL="";
    ResultSet rs;    DatabaseMetaData md;
    boolean found=false; String msg="";
    try {
      MyCon = new SQLConnection();
      c = MyCon.getConnection("JAVA");
      md = c.getMetaData();
      rs = md.getTables(null, null, "%", null);
      while (rs.next()) {
        if (rs.getString(3).equals(tname)) {
          found = true;
          break;
        }
      }
      stmt = c.createStatement();
      if(!found) {
        if(tname.equals("psale"))
          SQL = "create table psale (pno int NOT NULL AUTO_INCREMENT,  pid "+ 
                 "varchar(5),pqty int(11), ptotal float, primary key (pno))";
        stmt.executeUpdate(SQL);
        md = c.getMetaData();
        rs = md.getTables(null, null, "%", null);
        msg = "Table >>> ";
        while (rs.next()) {
          if (rs.getString(3).equals(tname)) {
              msg = msg+rs.getString(3).toUpperCase();
              break;
          }
        }
        msg = msg+" is Created";
        JOptionPane.showMessageDialog(null, msg);            
      }
      else {    
        msg = msg+"Table >>> " + tname.toUpperCase() + " is already Created";
        JOptionPane.showMessageDialog(null, msg);
      }
    }
    catch (SQLException ex) {
      System.out.println(ex);
    }
  }
}
