Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.70 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.jdbc.cs39;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.EventQueue;
  5.  
  6. import javax.swing.JFrame;
  7.  
  8. import javax.swing.JButton;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.GridBagLayout;
  12. import java.awt.GridBagConstraints;
  13. import javax.swing.JTextField;
  14.  
  15. import java.awt.Insets;
  16. import java.awt.Toolkit;
  17.  
  18. import java.awt.event.MouseAdapter;
  19. import java.awt.event.MouseEvent;
  20. import java.awt.event.WindowAdapter;
  21. import java.awt.event.WindowEvent;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.sql.Connection;
  25. import java.sql.Driver;
  26. import java.sql.SQLException;
  27. import java.text.SimpleDateFormat;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.Map;
  31. import java.util.Properties;
  32. import java.util.Vector;
  33. import java.util.logging.FileHandler;
  34. import java.util.logging.Level;
  35. import java.util.logging.Logger;
  36. import java.util.logging.SimpleFormatter;
  37.  
  38. import javax.swing.JScrollPane;
  39. import javax.swing.JTable;
  40. import javax.swing.table.DefaultTableModel;
  41.  
  42. import javax.swing.JComboBox;
  43. import javax.swing.JFileChooser;
  44. import javax.swing.JLabel;
  45. import javax.swing.JOptionPane;
  46. import javax.swing.JPanel;
  47.  
  48. import java.awt.event.WindowAdapter;
  49. import java.awt.event.WindowEvent;
  50. import java.io.IOException;
  51. import javax.swing.JTextPane;
  52. import javax.swing.JList;
  53. import javax.swing.JTextArea;
  54. import java.awt.Color;
  55. import java.awt.SystemColor;
  56. import javax.swing.UIManager;
  57.  
  58. public class Gui {
  59.  
  60. private JFrame frame;
  61. private JTextField input;
  62. private JScrollPane scrollPane;
  63. private JTable table;
  64. static Logger logger = Logger.getLogger(Gui.class.getName());
  65. Connection connection;
  66.  
  67. // IDataBase db = new IDataBase();
  68. // Vector<Vector<Vector>> tables ;
  69. JTable[] arr ;
  70. public DefaultTableModel model ;
  71.  
  72. public DefaultTableModel model2 ;
  73. int counterLog=0 ;
  74. int flag=0;
  75. private JLabel label;
  76. private JButton path;
  77. public JTextArea textArea = new JTextArea();
  78. // private JList list;
  79.  
  80. /**
  81. * Launch the application.
  82. */
  83. public static void main(String[] args) {
  84. EventQueue.invokeLater(new Runnable() {
  85. public void run() {
  86. try {
  87. Gui window = new Gui();
  88. window.frame.setVisible(true);
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. }
  93.  
  94. private void addWindowListener(WindowAdapter windowAdapter) {
  95. // TODO Auto-generated method stub
  96.  
  97. }
  98. });
  99. }
  100.  
  101. /**
  102. * Create the application.
  103. */
  104. public Gui() {
  105. initialize();
  106. }
  107. public void pathInit() {
  108. JFileChooser f = new JFileChooser();
  109. f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  110. f.showSaveDialog(null);
  111. System.out.println(f.getSelectedFile());
  112. Driver driver = new DriverImp();
  113. Properties info = new Properties();
  114. File dbDir = new File(""+f.getSelectedFile());
  115. info.put("path", dbDir.getAbsoluteFile());
  116. try {
  117. connection = (Connection) driver.connect("jdbc:xmldb://localhost", info);
  118. logger.setLevel(Level.INFO);
  119. logger.info("connection to database "+f.getSelectedFile() +" succeeded");
  120. textArea.setBackground(UIManager.getColor("Button.background"));
  121. String pattern = "dd MMMMM yyyy HH:mm:ss";
  122. SimpleDateFormat simpleDateFormat =
  123. new SimpleDateFormat(pattern);
  124.  
  125. String date = simpleDateFormat.format(new Date());
  126.  
  127. textArea.append("\n"+date+" connection to database "+f.getSelectedFile() +" succeeded");
  128.  
  129.  
  130.  
  131.  
  132. } catch (SQLException e1) {
  133. // TODO Auto-generated catch block
  134. e1.printStackTrace();
  135. }
  136. }
  137.  
  138.  
  139. /**
  140. * Initialize the contents of the frame.
  141. */
  142. private void initialize() {
  143. textArea.setEditable(false);
  144. frame = new JFrame();
  145.  
  146.  
  147. Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  148. frame.setBounds(0, 0, screen.width, screen.height - 35);
  149. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  150. GridBagLayout gridBagLayout = new GridBagLayout();
  151. gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  152. gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  153. gridBagLayout.columnWeights = new double[] { 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
  154. 0.0, Double.MIN_VALUE };
  155. gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
  156. frame.getContentPane().setLayout(gridBagLayout);
  157. JPanel panel = new JPanel();
  158. JScrollPane scrollPane1 = new JScrollPane( textArea );
  159.  
  160. panel.add( scrollPane1 );
  161. JButton process = new JButton("process");
  162. SimpleDateFormat format = new SimpleDateFormat("M-d_HHmmss");
  163. try {
  164.  
  165. // This block configure the logger with handler and formatter
  166. FileHandler fh = new FileHandler("MyLogFile_"
  167. + format.format(Calendar.getInstance().getTime()) + ".log");
  168.  
  169. logger.addHandler(fh);
  170.  
  171. SimpleFormatter formatter = new SimpleFormatter();
  172. fh.setFormatter(formatter);
  173.  
  174. // the following statement is used to log any messages
  175.  
  176. } catch (SecurityException e) {
  177. e.printStackTrace();
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. }
  181.  
  182. path = new JButton("change path");
  183. path.setPreferredSize(new Dimension(150, 23));
  184. path.addMouseListener(new MouseAdapter() {
  185. @Override
  186. public void mouseClicked(MouseEvent e) {
  187.  
  188. JFileChooser f = new JFileChooser();
  189. f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  190. f.showSaveDialog(null);
  191. System.out.println(f.getSelectedFile());
  192. Driver driver = new DriverImp();
  193. Properties info = new Properties();
  194. File dbDir = new File(""+f.getSelectedFile());
  195. info.put("path", dbDir.getAbsoluteFile());
  196. try {
  197. connection = (Connection) driver.connect("jdbc:xmldb://localhost", info);
  198. logger.setLevel(Level.INFO);
  199.  
  200. logger.info("connection to database "+f.getSelectedFile() +" succeeded");
  201. String pattern = "dd MMMMM yyyy HH:mm:ss";
  202. SimpleDateFormat simpleDateFormat =
  203. new SimpleDateFormat(pattern);
  204.  
  205. String date = simpleDateFormat.format(new Date());
  206.  
  207. textArea.append("\n"+date+" connection to database "+f.getSelectedFile() +" succeeded");
  208.  
  209. } catch (SQLException e1) {
  210. // TODO Auto-generated catch block
  211. e1.printStackTrace();
  212. }
  213. }
  214. });
  215. pathInit();
  216.  
  217. GridBagConstraints gbc_path = new GridBagConstraints();
  218. gbc_path.gridwidth = 5;
  219. gbc_path.insets = new Insets(0, 0, 5, 5);
  220. gbc_path.gridx = 8;
  221. gbc_path.gridy = 1;
  222. frame.getContentPane().add(path, gbc_path);
  223.  
  224.  
  225. label = new JLabel("log");
  226. GridBagConstraints gbc_label = new GridBagConstraints();
  227. gbc_label.insets = new Insets(0, 0, 5, 5);
  228. gbc_label.gridx = 0;
  229. gbc_label.gridy = 3;
  230. frame.getContentPane().add(label, gbc_label);
  231. scrollPane = new JScrollPane();
  232. GridBagConstraints gbc_scrollPane = new GridBagConstraints();
  233. gbc_scrollPane.gridheight = 8;
  234. gbc_scrollPane.gridwidth = 12;
  235. gbc_scrollPane.fill = GridBagConstraints.BOTH;
  236. gbc_scrollPane.gridx = 2;
  237. gbc_scrollPane.gridy = 4;
  238.  
  239. frame.getContentPane().add(scrollPane, gbc_scrollPane);
  240. table = new JTable();
  241. table.setModel(new DefaultTableModel(new Object[][] {}, new String[] {}));
  242. scrollPane.setViewportView(table);
  243. DefaultTableModel tableModel = new DefaultTableModel() {
  244.  
  245. @Override
  246. public boolean isCellEditable(int row, int column) {
  247. //all cells false
  248. return false;
  249. }
  250. };
  251. table.setModel(tableModel);
  252. model = (DefaultTableModel) table.getModel();
  253.  
  254. try{StatementImp stmt = (StatementImp) connection.createStatement();
  255. frame.addWindowListener(new WindowAdapter() {
  256. @Override
  257. public void windowClosed(WindowEvent e) {
  258. //System.out.println("2flna");
  259. }
  260. @Override
  261. public void windowClosing(WindowEvent e) {
  262. //System.out.println("bn2fel");
  263. try {
  264. stmt.save();
  265. } catch (Exception e1) {
  266. // TODO Auto-generated catch block
  267. e1.printStackTrace();
  268. }
  269. }
  270. });
  271. process.addMouseListener(new MouseAdapter() {
  272. @Override
  273. public void mouseClicked(MouseEvent e) {
  274. ResultSetImp rs = null ;
  275.  
  276. try {
  277.  
  278. stmt.execute(input.getText());
  279. logger.setLevel(Level.INFO);
  280. logger.info(input.getText());
  281. String pattern = "dd MMMMM yyyy HH:mm:ss";
  282. SimpleDateFormat simpleDateFormat =
  283. new SimpleDateFormat(pattern);
  284.  
  285. String date = simpleDateFormat.format(new Date());
  286.  
  287. textArea.append("\n"+date+" "+input.getText());
  288.  
  289.  
  290. // logText.add(input.getText());
  291. // list.add(new JLabel(input.getText()));
  292.  
  293. } catch (SQLException e1) {
  294. // TODO Auto-generated catch block
  295. e1.printStackTrace();
  296. }
  297. // if(rs.items.length>0) {
  298. //
  299. // Vector<String> names1= rs.Names;
  300. // table = new JTable();
  301. // scrollPane.setViewportView(table);
  302. // DefaultTableModel tableModel = new DefaultTableModel() {
  303. //
  304. // @Override
  305. // public boolean isCellEditable(int row, int column) {
  306. // //all cells false
  307. // return false;
  308. // }
  309. // };
  310. // table.setModel(tableModel);
  311. // model = (DefaultTableModel) table.getModel();
  312. //
  313. // for(int j=0;j<names1.size();j++) {
  314. // model.addColumn(names1.get(j));
  315. //
  316. // }
  317. // for(int i=0;i<rs.items.length;i++) {
  318. //
  319. // model.addRow(rs.items[i]);
  320. //
  321. // }
  322. //
  323. // input.setText("success :)");
  324. //
  325. // }
  326. // if (parser.checkInput(input.getText()) == 8) {
  327. // Vector<String> columns= partitions.CreateTable(input.getText());
  328. // table = new JTable();
  329. // flag=1;
  330. // scrollPane.setViewportView(table);
  331. // model = (DefaultTableModel) table.getModel();
  332. // comboBox.addItem(columns.lastElement());
  333. // counter = comboBox.getItemCount();
  334. // comboBox.setSelectedIndex(counter-1);
  335. //
  336. // for(int i=0;i<columns.size()-1;i++) {
  337. // model.addColumn(columns.elementAt(i));
  338. //
  339. // }
  340. //// arr[0]=table;
  341. //// System.out.println( table.getColumnModel().getColumnCount());
  342. //// System.out.println(tables.size());
  343. // }else if (parser.checkInput(input.getText())==3) {
  344. // partitions.DropDatabase(input.getText());
  345. // if(label.getText().compareToIgnoreCase(partitions.getDropDataBaseName())==0) {
  346. // label.setText("");
  347. //
  348. // counter=1;
  349. // JTable table2 = new JTable();
  350. // scrollPane.setViewportView(null);
  351. //// scrollPane.setViewportView(table2);
  352. // model = (DefaultTableModel) table2.getModel();
  353. // input.setText("success :)");
  354. // comboBox.removeAllItems();
  355. // }
  356. // }
  357. // else if (parser.checkInput(input.getText())==4) {
  358. // partitions.DropTable(input.getText());
  359. // for(int i=0;i<comboBox.getItemCount();i++){
  360. // if(partitions.getTablename().compareToIgnoreCase((String) comboBox.getItemAt(i))==0) {
  361. //// comboBox.remove(i);
  362. // comboBox.removeItemAt(i);
  363. // }
  364. // }
  365. // }
  366. // else if(parser.checkInput(input.getText())==2) {
  367. // comboBox.removeAllItems();
  368. // counter=1;
  369. // table = new JTable();
  370. // scrollPane.setViewportView(table);
  371. //
  372. // String name = partitions.CreateDatabase(input.getText()) ;
  373. // label.setText(name);
  374. // input.setText("success :)");
  375. // }
  376. // else if (parser.checkInput(input.getText())==12||parser.checkInput(input.getText())==7||parser.checkInput(input.getText())==5||parser.checkInput(input.getText())==6||parser.checkInput(input.getText())==11) {
  377. // try {
  378. // System.out.println("sh215o");
  379. // Vector<String> names1= db.getNames((String) comboBox.getSelectedItem());
  380. // Object[][] table1 = db.executeQuery("SELECT * FROM "+comboBox.getSelectedItem());
  381. // table = new JTable();
  382. // scrollPane.setViewportView(table);
  383. // DefaultTableModel tableModel = new DefaultTableModel() {
  384. //
  385. // @Override
  386. // public boolean isCellEditable(int row, int column) {
  387. // //all cells false
  388. // return false;
  389. // }
  390. // };
  391. // table.setModel(tableModel);
  392. // model = (DefaultTableModel) table.getModel();
  393. //
  394. // for(int j=0;j<names1.size();j++) {
  395. // model.addColumn(names1.get(j));
  396. //
  397. // }
  398. // for(int i=0;i<table1.length;i++) {
  399. //
  400. // model.addRow(table1[i]);
  401. //
  402. // }
  403. // } catch (SQLException e1) {
  404. // // TODO Auto-generated catch block
  405. // e1.printStackTrace();
  406. // }
  407. // input.setText("success :)");
  408. // }
  409. // else if (parser.checkInput(input.getText())==1) {
  410. // try {
  411. // Partitions p = new Partitions();
  412. // p.SelectTable(input.getText());
  413. // Vector<String> names1= db.getNames(p.getTablename());
  414. // Object[][] table1 = db.executeQuery("SELECT * FROM "+p.getTablename());
  415. // comboBox.setSelectedItem(p.getTablename());
  416. // counter = comboBox.getSelectedIndex()+1;
  417. // table = new JTable();
  418. // scrollPane.setViewportView(table);
  419. // DefaultTableModel tableModel = new DefaultTableModel() {
  420. //
  421. // @Override
  422. // public boolean isCellEditable(int row, int column) {
  423. // //all cells false
  424. // return false;
  425. // }
  426. // };
  427. // table.setModel(tableModel);
  428. // model = (DefaultTableModel) table.getModel();
  429. //
  430. // for(int j=0;j<names1.size();j++) {
  431. // model.addColumn(names1.get(j));
  432. //
  433. // }
  434. // for(int i=0;i<table1.length;i++) {
  435. //
  436. // model.addRow(table1[i]);
  437. //
  438. // }
  439. // } catch (SQLException e1) {
  440. // // TODO Auto-generated catch block
  441. // e1.printStackTrace();
  442. // }
  443. // input.setText("success :)");
  444. // }
  445. // else if (parser.checkInput(input.getText())==9) {
  446. // try {
  447. // Partitions p = new Partitions();
  448. // p.Select(input.getText());
  449. // Vector<String> names1= db.getNames(p.getTablename());
  450. // Object[][] table1 = db.executeQuery(input.getText());
  451. // comboBox.setSelectedItem(p.getTablename());
  452. // counter = comboBox.getSelectedIndex()+1;
  453. // table = new JTable();
  454. // scrollPane.setViewportView(table);
  455. // DefaultTableModel tableModel = new DefaultTableModel() {
  456. //
  457. // @Override
  458. // public boolean isCellEditable(int row, int column) {
  459. // //all cells false
  460. // return false;
  461. // }
  462. // };
  463. // table.setModel(tableModel);
  464. // model = (DefaultTableModel) table.getModel();
  465. //
  466. // for(int j=0;j<names1.size();j++) {
  467. // model.addColumn(names1.get(j));
  468. //
  469. // }
  470. // for(int i=0;i<table1.length;i++) {
  471. //
  472. // model.addRow(table1[i]);
  473. //
  474. // }
  475. // } catch (SQLException e1) {
  476. // // TODO Auto-generated catch block
  477. // e1.printStackTrace();
  478. // }
  479. // input.setText("success :)");
  480. // }
  481. // else if (parser.checkInput(input.getText())==10) {
  482. // try {
  483. // Partitions p = new Partitions();
  484. // p.selecttwocolumnscondition(input.getText());
  485. // Object[][] table1 = db.executeQuery(input.getText());
  486. // comboBox.setSelectedItem(p.getTablename());
  487. // counter = comboBox.getSelectedIndex()+1;
  488. // table = new JTable();
  489. // scrollPane.setViewportView(table);
  490. // DefaultTableModel tableModel = new DefaultTableModel() {
  491. //
  492. // @Override
  493. // public boolean isCellEditable(int row, int column) {
  494. // //all cells false
  495. // return false;
  496. // }
  497. // };
  498. // table.setModel(tableModel);
  499. // model = (DefaultTableModel) table.getModel();
  500. //
  501. // model.addColumn(p.getselectconditioncloumn1());
  502. // for(int i=0;i<table1.length;i++) {
  503. //
  504. // model.addRow(table1[i]);
  505. //
  506. // }
  507. // } catch (SQLException e1) {
  508. // // TODO Auto-generated catch block
  509. // e1.printStackTrace();
  510. // }
  511. // input.setText("success :)");
  512. // }
  513. // else if (parser.checkInput(input.getText()) != 0) {
  514. // input.setText("success :)");
  515. // } else {
  516. // input.setText("wrong input");
  517. // }
  518. //
  519. }
  520. });
  521.  
  522. }
  523. catch (SQLException e1) {
  524. // TODO Auto-generated catch block
  525. e1.printStackTrace();
  526. }
  527. // process.addActionListener(new ActionListener() {
  528. // public void actionPerformed(ActionEvent e) {
  529. // }
  530. // });
  531.  
  532. input = new JTextField();
  533. GridBagConstraints gbc_input = new GridBagConstraints();
  534. gbc_input.gridwidth = 8;
  535. gbc_input.insets = new Insets(0, 0, 5, 5);
  536. gbc_input.fill = GridBagConstraints.HORIZONTAL;
  537. gbc_input.gridx = 0;
  538. gbc_input.gridy = 2;
  539. frame.getContentPane().add(input, gbc_input);
  540. input.setColumns(10);
  541. GridBagConstraints gbc_process = new GridBagConstraints();
  542. gbc_process.insets = new Insets(0, 0, 5, 5);
  543. gbc_process.gridx = 10;
  544. gbc_process.gridy = 2;
  545. frame.getContentPane().add(process, gbc_process);
  546. GridBagConstraints gbc_textArea = new GridBagConstraints();
  547. gbc_textArea.insets = new Insets(0, 0, 5, 5);
  548. gbc_textArea.fill = GridBagConstraints.BOTH;
  549. gbc_textArea.gridx = 0;
  550. gbc_textArea.gridy = 6;
  551. frame.getContentPane().add(textArea, gbc_textArea);
  552.  
  553.  
  554. // addWindowListener(new WindowAdapter() {
  555. // public void windowClosing(WindowEvent e) {
  556. // int confirmed = JOptionPane.showConfirmDialog(null,
  557. // "Are you sure you want to exit the program?", "Exit Program Message Box",
  558. // JOptionPane.YES_NO_OPTION);
  559. //
  560. // if (confirmed == JOptionPane.YES_OPTION) {
  561. // dispose();
  562. // }
  563. // }
  564. // });
  565. }
  566.  
  567.  
  568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement