Advertisement
JKaysss

CustomCommand Sec

Oct 2nd, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.81 KB | None | 0 0
  1. package MYSQL_GUI;
  2. import static MYSQL_GUI.Frame1.j;
  3. import static MYSQL_GUI.Frame1.k;
  4. import com.mysql.jdbc.Connection;
  5. import com.mysql.jdbc.Statement;
  6. import java.awt.Dimension;
  7. import java.awt.Toolkit;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.util.Scanner;
  11. import javax.swing.JOptionPane;
  12.  
  13. /**
  14.  *
  15.  * @author Jaskaran
  16.  */
  17. public class CustComm extends javax.swing.JFrame {
  18.  
  19.     /**
  20.      * Creates new form CustComm
  21.      */
  22.     public CustComm() {
  23.         initComponents();
  24.         Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  25.         this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
  26. }
  27.  
  28.     /**
  29.      * This method is called from within the constructor to initialize the form.
  30.      * WARNING: Do NOT modify this code. The content of this method is always
  31.      * regenerated by the Form Editor.
  32.      */
  33.     @SuppressWarnings("unchecked")
  34.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  35.     private void initComponents() {
  36.  
  37.         jLabel1 = new javax.swing.JLabel();
  38.         jScrollPane1 = new javax.swing.JScrollPane();
  39.         jTextArea1 = new javax.swing.JTextArea();
  40.         jButton1 = new javax.swing.JButton();
  41.         jButton2 = new javax.swing.JButton();
  42.         jScrollPane2 = new javax.swing.JScrollPane();
  43.         jTextArea2 = new javax.swing.JTextArea();
  44.         jLabel2 = new javax.swing.JLabel();
  45.         jScrollPane3 = new javax.swing.JScrollPane();
  46.         jTextArea3 = new javax.swing.JTextArea();
  47.         jLabel3 = new javax.swing.JLabel();
  48.  
  49.         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  50.         setTitle("MySQL Custom Command Editor");
  51.  
  52.         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
  53.         jLabel1.setText("Enter Custom Commands Below");
  54.  
  55.         jTextArea1.setColumns(20);
  56.         jTextArea1.setRows(5);
  57.         jScrollPane1.setViewportView(jTextArea1);
  58.  
  59.         jButton1.setText("Execute Command(s)");
  60.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  61.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  62.                 jButton1ActionPerformed(evt);
  63.             }
  64.         });
  65.  
  66.         jButton2.setText("Cancel");
  67.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  68.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  69.                 jButton2ActionPerformed(evt);
  70.             }
  71.         });
  72.  
  73.         jTextArea2.setEditable(false);
  74.         jTextArea2.setColumns(20);
  75.         jTextArea2.setLineWrap(true);
  76.         jTextArea2.setRows(2);
  77.         jTextArea2.setText("!!Separate commands by writing each command in new line with proper syntax just like in MySQL.");
  78.         jTextArea2.setFocusable(false);
  79.         jTextArea2.setHighlighter(null);
  80.         jTextArea2.setOpaque(false);
  81.         jScrollPane2.setViewportView(jTextArea2);
  82.  
  83.         jLabel2.setText("Enter Command(s) below:");
  84.  
  85.         jTextArea3.setEditable(false);
  86.         jTextArea3.setColumns(10);
  87.         jTextArea3.setRows(5);
  88.         jScrollPane3.setViewportView(jTextArea3);
  89.  
  90.         jLabel3.setText("Command Outputs:");
  91.  
  92.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  93.         getContentPane().setLayout(layout);
  94.         layout.setHorizontalGroup(
  95.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  96.             .addGroup(layout.createSequentialGroup()
  97.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  98.                     .addGroup(layout.createSequentialGroup()
  99.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  100.                             .addGroup(layout.createSequentialGroup()
  101.                                 .addContainerGap()
  102.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  103.                                     .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
  104.                                     .addComponent(jScrollPane1)))
  105.                             .addGroup(layout.createSequentialGroup()
  106.                                 .addGap(247, 247, 247)
  107.                                 .addComponent(jLabel2)))
  108.                         .addGap(18, 18, 18)
  109.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  110.                             .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
  111.                             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  112.                                 .addComponent(jLabel3)
  113.                                 .addGap(40, 40, 40))))
  114.                     .addGroup(layout.createSequentialGroup()
  115.                         .addGap(222, 222, 222)
  116.                         .addComponent(jLabel1))
  117.                     .addGroup(layout.createSequentialGroup()
  118.                         .addGap(122, 122, 122)
  119.                         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
  120.                         .addGap(95, 95, 95)
  121.                         .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
  122.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  123.         );
  124.         layout.setVerticalGroup(
  125.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  126.             .addGroup(layout.createSequentialGroup()
  127.                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
  128.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  129.                 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  130.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  131.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  132.                     .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
  133.                     .addComponent(jLabel3))
  134.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  135.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136.                     .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
  137.                     .addComponent(jScrollPane1))
  138.                 .addGap(18, 18, 18)
  139.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  140.                     .addComponent(jButton1)
  141.                     .addComponent(jButton2))
  142.                 .addContainerGap())
  143.         );
  144.  
  145.         pack();
  146.     }// </editor-fold>                        
  147.  
  148.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  149.         String x = jTextArea1.getText();
  150.         Scanner scanner = new Scanner(x);
  151. while (scanner.hasNextLine()) {
  152.   String line = scanner.nextLine();
  153.   if(line.contains("use ")){
  154.       if(line.contains("use ")&&!GlobalParams.usedb){
  155.          JOptionPane.showMessageDialog(this, "Please use the Program to select database.");  
  156.         }
  157.       else{
  158.       try{
  159.        Class.forName("java.sql.DriverManager");
  160.        Connection con = (Connection)
  161.        DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",GlobalParams.uservar, GlobalParams.passvar);
  162.        Statement stmnt = (Statement) con.createStatement();
  163.         String query = line;
  164.         stmnt.executeUpdate(query);
  165.         JOptionPane.showMessageDialog(this, "Commands Executed Successfully");
  166.         this.dispose();}
  167.       catch(Exception e){
  168.         JOptionPane.showMessageDialog(this,e.getMessage());
  169.     }
  170.   }}
  171.   if(line.contains("create table ")){
  172.       if(line.contains("create table ")&&!GlobalParams.usedb){
  173.          JOptionPane.showMessageDialog(this, "You can only create tables and manipulate them in next section");  
  174.         }
  175.       else{
  176.       try{
  177.        Class.forName("java.sql.DriverManager");
  178.        Connection con = (Connection)
  179.        DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",GlobalParams.uservar, GlobalParams.passvar);
  180.        Statement stmnt = (Statement) con.createStatement();
  181.         String query = line;
  182.         stmnt.executeQuery("use "+GlobalParams.dbvar);
  183.         stmnt.executeUpdate(query);
  184.         JOptionPane.showMessageDialog(this, "Commands Executed Successfully");
  185.         this.dispose();}
  186.       catch(Exception e){
  187.         JOptionPane.showMessageDialog(this,e.getMessage());
  188.     }
  189.   }}
  190.         else{
  191.   try{
  192.        Class.forName("java.sql.DriverManager");
  193.        Connection con = (Connection)
  194.        DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",GlobalParams.uservar, GlobalParams.passvar);
  195.        Statement stmnt = (Statement) con.createStatement();
  196.         String query = line;
  197.         stmnt.executeUpdate(query);
  198.         JOptionPane.showMessageDialog(this, "Commands Executed Successfully");
  199.         this.dispose();}
  200.  
  201.     catch(Exception e){
  202.         JOptionPane.showMessageDialog(this,e.getMessage());
  203.     }}}
  204. try{
  205.         Class.forName("java.sql.DriverManager");
  206.         Connection con = (Connection)
  207.         DriverManager.getConnection("jdbc:mysql://localhost:"+GlobalParams.portvar+"/",""+k,""+j);
  208.         Statement stmnt = (Statement) con.createStatement();
  209.         String query = "show databases";
  210.         ResultSet rs = stmnt.executeQuery(query);
  211.         while (rs.next()){
  212.         String db = rs.getString("Database");
  213.         }
  214. }
  215.     catch(Exception e){
  216.         JOptionPane.showMessageDialog(this,e.getMessage());
  217.     }
  218.         GlobalParams.usedb=false;
  219.     }                                        
  220.  
  221.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  222. int dialogResult = JOptionPane.showConfirmDialog (this, "You may loose your commands if you did not execute them. Are you sure you want to Proceed?");
  223. if(dialogResult == JOptionPane.YES_OPTION){dispose();}
  224. else{};
  225.     }                                        
  226.  
  227.     /**
  228.      * @param args the command line arguments
  229.      */
  230.     public static void main(String args[]) {
  231.         /* Set the Nimbus look and feel */
  232.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  233.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  234.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  235.          */
  236.         try {
  237.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  238.                 if ("Nimbus".equals(info.getName())) {
  239.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  240.                     break;
  241.                 }
  242.             }
  243.         } catch (ClassNotFoundException ex) {
  244.             java.util.logging.Logger.getLogger(CustComm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  245.         } catch (InstantiationException ex) {
  246.             java.util.logging.Logger.getLogger(CustComm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  247.         } catch (IllegalAccessException ex) {
  248.             java.util.logging.Logger.getLogger(CustComm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  249.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  250.             java.util.logging.Logger.getLogger(CustComm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  251.         }
  252.         //</editor-fold>
  253.  
  254.         /* Create and display the form */
  255.         java.awt.EventQueue.invokeLater(new Runnable() {
  256.             public void run() {
  257.                 new CustComm().setVisible(true);
  258.             }
  259.         });
  260.     }
  261.     // Variables declaration - do not modify                    
  262.     private javax.swing.JButton jButton1;
  263.     private javax.swing.JButton jButton2;
  264.     private javax.swing.JLabel jLabel1;
  265.     private javax.swing.JLabel jLabel2;
  266.     private javax.swing.JLabel jLabel3;
  267.     private javax.swing.JScrollPane jScrollPane1;
  268.     private javax.swing.JScrollPane jScrollPane2;
  269.     private javax.swing.JScrollPane jScrollPane3;
  270.     public static javax.swing.JTextArea jTextArea1;
  271.     private javax.swing.JTextArea jTextArea2;
  272.     private javax.swing.JTextArea jTextArea3;
  273.     // End of variables declaration                  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement