Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package eg.edu.alexu.csd.oop.db;
- import java.sql.SQLException;
- import java.util.StringTokenizer;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- /**
- *
- * @author TOSHIBA PC
- */
- public class NewJFrame extends javax.swing.JFrame {
- /**
- * Creates new form NewJFrame
- */
- public NewJFrame() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jScrollPane1 = new javax.swing.JScrollPane();
- TextArea = new javax.swing.JTextArea();
- CompileButton = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- TextArea.setColumns(20);
- TextArea.setRows(5);
- jScrollPane1.setViewportView(TextArea);
- CompileButton.setText("Compile");
- CompileButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- CompileButtonActionPerformed(evt);
- }
- });
- jLabel1.setBackground(new java.awt.Color(255, 255, 255));
- jLabel1.setText("Enter your SQL code :");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(155, 155, 155)
- .addComponent(CompileButton))
- .addGroup(layout.createSequentialGroup()
- .addGap(55, 55, 55)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addContainerGap(75, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(32, 32, 32)
- .addComponent(CompileButton)
- .addContainerGap(36, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void CompileButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- String code=TextArea.getText();
- int flag=1;
- if
- (! (TextArea.getText().trim().length() == 0) )
- {
- Sql sql =new Sql();
- String editedCode=null;
- editedCode=Sql.regexReplace(code);
- System.out.println(editedCode);
- StringTokenizer words= new StringTokenizer(editedCode, ",");
- String FirstToken=null;
- FirstToken=words.nextToken();
- String SecondToken=null;
- if
- (words.hasMoreTokens())
- {
- SecondToken=words.nextToken();
- }
- else
- {
- flag=0;
- }
- if
- ((FirstToken.equalsIgnoreCase("create")||FirstToken.equalsIgnoreCase("drop"))&&SecondToken.equalsIgnoreCase("table")&&words.hasMoreTokens()&&flag==1)
- {
- Boolean b = null;
- try {
- b=sql.executeStructureQuery(code);
- } catch (SQLException ex) {
- Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
- }
- if
- (b==true)
- {
- JOptionPane.showMessageDialog(null,"Successful operation ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- else if
- (b==false)
- {
- JOptionPane.showMessageDialog(null,"Unsuccessful operation invalid expression!! ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- }
- else if
- (FirstToken.equalsIgnoreCase("select")&&words.hasMoreTokens()&&flag==1)
- {
- try {
- sql.executeRetrievalQuery(code);
- } catch (SQLException ex) {
- Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- else if
- (((FirstToken.equalsIgnoreCase("delete")&&SecondToken.equalsIgnoreCase("from"))||(FirstToken.equalsIgnoreCase("insert")&&SecondToken.equalsIgnoreCase("into")))&&words.hasMoreTokens()&&flag==1)
- {
- try {
- sql.executeUpdateQuery(code);
- } catch (SQLException ex) {
- Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- else
- {
- flag=0;
- }
- if (flag==0)
- {
- JOptionPane.showMessageDialog(null,"Unsuccessful operation invalid expression!! ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- /*
- int flag;
- Sql sql =new Sql();
- flag=sql.validate(code);
- if
- (flag==0)
- {
- JOptionPane.showMessageDialog(null,"Invalid expression !!","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- else if
- (flag==2)
- {
- JOptionPane.showMessageDialog(null,"Enter only varchar & int datatypes!!","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- else if
- (flag==3)
- {
- JOptionPane.showMessageDialog(null,"You forget to put the ';' symbol ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- else if
- (flag==5)
- {
- JOptionPane.showMessageDialog(null,"You forget to write 'where' !! ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- else if
- (flag==4)
- {
- JOptionPane.showMessageDialog(null,"Write your condition !!","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- else
- {
- JOptionPane.showMessageDialog(null,"Successful operation ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- */
- }
- else
- {
- JOptionPane.showMessageDialog(null,"No code entered !!","Display Message ",JOptionPane.INFORMATION_MESSAGE);
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new NewJFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton CompileButton;
- private javax.swing.JTextArea TextArea;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JScrollPane jScrollPane1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement