Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package eg.edu.alexu.csd.oop.db;
  7.  
  8. import java.sql.SQLException;
  9. import java.util.StringTokenizer;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.swing.JOptionPane;
  13.  
  14. /**
  15. *
  16. * @author TOSHIBA PC
  17. */
  18. public class NewJFrame extends javax.swing.JFrame {
  19.  
  20. /**
  21. * Creates new form NewJFrame
  22. */
  23. public NewJFrame() {
  24. initComponents();
  25. }
  26.  
  27. /**
  28. * This method is called from within the constructor to initialize the form.
  29. * WARNING: Do NOT modify this code. The content of this method is always
  30. * regenerated by the Form Editor.
  31. */
  32. @SuppressWarnings("unchecked")
  33. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  34. private void initComponents() {
  35.  
  36. jScrollPane1 = new javax.swing.JScrollPane();
  37. TextArea = new javax.swing.JTextArea();
  38. CompileButton = new javax.swing.JButton();
  39. jLabel1 = new javax.swing.JLabel();
  40.  
  41. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  42.  
  43. TextArea.setColumns(20);
  44. TextArea.setRows(5);
  45. jScrollPane1.setViewportView(TextArea);
  46.  
  47. CompileButton.setText("Compile");
  48. CompileButton.addActionListener(new java.awt.event.ActionListener() {
  49. public void actionPerformed(java.awt.event.ActionEvent evt) {
  50. CompileButtonActionPerformed(evt);
  51. }
  52. });
  53.  
  54. jLabel1.setBackground(new java.awt.Color(255, 255, 255));
  55. jLabel1.setText("Enter your SQL code :");
  56.  
  57. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  58. getContentPane().setLayout(layout);
  59. layout.setHorizontalGroup(
  60. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  61. .addGroup(layout.createSequentialGroup()
  62. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  63. .addGroup(layout.createSequentialGroup()
  64. .addGap(155, 155, 155)
  65. .addComponent(CompileButton))
  66. .addGroup(layout.createSequentialGroup()
  67. .addGap(55, 55, 55)
  68. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  69. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
  70. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))
  71. .addContainerGap(75, Short.MAX_VALUE))
  72. );
  73. layout.setVerticalGroup(
  74. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75. .addGroup(layout.createSequentialGroup()
  76. .addContainerGap()
  77. .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
  78. .addGap(18, 18, 18)
  79. .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
  80. .addGap(32, 32, 32)
  81. .addComponent(CompileButton)
  82. .addContainerGap(36, Short.MAX_VALUE))
  83. );
  84.  
  85. pack();
  86. }// </editor-fold>
  87.  
  88. private void CompileButtonActionPerformed(java.awt.event.ActionEvent evt) {
  89.  
  90. // TODO add your handling code here:
  91.  
  92.  
  93.  
  94. String code=TextArea.getText();
  95. int flag=1;
  96.  
  97. if
  98. (! (TextArea.getText().trim().length() == 0) )
  99.  
  100. {
  101.  
  102. Sql sql =new Sql();
  103. String editedCode=null;
  104. editedCode=Sql.regexReplace(code);
  105. System.out.println(editedCode);
  106. StringTokenizer words= new StringTokenizer(editedCode, ",");
  107. String FirstToken=null;
  108.  
  109.  
  110. FirstToken=words.nextToken();
  111.  
  112. String SecondToken=null;
  113. if
  114. (words.hasMoreTokens())
  115. {
  116. SecondToken=words.nextToken();
  117.  
  118. }
  119. else
  120. {
  121. flag=0;
  122. }
  123.  
  124. if
  125. ((FirstToken.equalsIgnoreCase("create")||FirstToken.equalsIgnoreCase("drop"))&&SecondToken.equalsIgnoreCase("table")&&words.hasMoreTokens()&&flag==1)
  126. {
  127. Boolean b = null;
  128. try {
  129.  
  130. b=sql.executeStructureQuery(code);
  131. } catch (SQLException ex) {
  132. Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
  133. }
  134.  
  135. if
  136. (b==true)
  137. {
  138. JOptionPane.showMessageDialog(null,"Successful operation ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  139. }
  140. else if
  141. (b==false)
  142. {
  143. JOptionPane.showMessageDialog(null,"Unsuccessful operation invalid expression!! ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  144. }
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152. else if
  153. (FirstToken.equalsIgnoreCase("select")&&words.hasMoreTokens()&&flag==1)
  154. {
  155. try {
  156. sql.executeRetrievalQuery(code);
  157. } catch (SQLException ex) {
  158. Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
  159. }
  160.  
  161. }
  162.  
  163.  
  164. else if
  165. (((FirstToken.equalsIgnoreCase("delete")&&SecondToken.equalsIgnoreCase("from"))||(FirstToken.equalsIgnoreCase("insert")&&SecondToken.equalsIgnoreCase("into")))&&words.hasMoreTokens()&&flag==1)
  166. {
  167. try {
  168. sql.executeUpdateQuery(code);
  169. } catch (SQLException ex) {
  170. Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
  171. }
  172.  
  173. }
  174.  
  175. else
  176. {
  177. flag=0;
  178. }
  179.  
  180.  
  181.  
  182. if (flag==0)
  183. {
  184. JOptionPane.showMessageDialog(null,"Unsuccessful operation invalid expression!! ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200. /*
  201.  
  202.  
  203.  
  204.  
  205. int flag;
  206. Sql sql =new Sql();
  207. flag=sql.validate(code);
  208. if
  209. (flag==0)
  210. {
  211. JOptionPane.showMessageDialog(null,"Invalid expression !!","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  212. }
  213. else if
  214. (flag==2)
  215. {
  216. JOptionPane.showMessageDialog(null,"Enter only varchar & int datatypes!!","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  217. }
  218. else if
  219. (flag==3)
  220. {
  221. JOptionPane.showMessageDialog(null,"You forget to put the ';' symbol ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  222. }
  223. else if
  224. (flag==5)
  225. {
  226. JOptionPane.showMessageDialog(null,"You forget to write 'where' !! ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  227. }
  228. else if
  229. (flag==4)
  230. {
  231. JOptionPane.showMessageDialog(null,"Write your condition !!","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  232. }
  233.  
  234. else
  235. {
  236. JOptionPane.showMessageDialog(null,"Successful operation ","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  237. }
  238.  
  239.  
  240.  
  241.  
  242. */
  243. }
  244.  
  245. else
  246. {
  247. JOptionPane.showMessageDialog(null,"No code entered !!","Display Message ",JOptionPane.INFORMATION_MESSAGE);
  248. }
  249.  
  250. }
  251.  
  252. /**
  253. * @param args the command line arguments
  254. */
  255. public static void main(String args[]) {
  256. /* Set the Nimbus look and feel */
  257. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  258. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  259. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  260. */
  261. try {
  262. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  263. if ("Nimbus".equals(info.getName())) {
  264. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  265. break;
  266. }
  267. }
  268. } catch (ClassNotFoundException ex) {
  269. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  270. } catch (InstantiationException ex) {
  271. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  272. } catch (IllegalAccessException ex) {
  273. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  274. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  275. java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  276. }
  277. //</editor-fold>
  278.  
  279. /* Create and display the form */
  280. java.awt.EventQueue.invokeLater(new Runnable() {
  281. public void run() {
  282. new NewJFrame().setVisible(true);
  283. }
  284. });
  285. }
  286.  
  287. // Variables declaration - do not modify
  288. private javax.swing.JButton CompileButton;
  289. private javax.swing.JTextArea TextArea;
  290. private javax.swing.JLabel jLabel1;
  291. private javax.swing.JScrollPane jScrollPane1;
  292. // End of variables declaration
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement