Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.24 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.  
  256. process.addMouseListener(new MouseAdapter() {
  257. @Override
  258. public void mouseClicked(MouseEvent e) {
  259. ResultSetImp rs = null ;
  260.  
  261. try {
  262.  
  263. stmt.execute(input.getText());
  264. logger.setLevel(Level.INFO);
  265. logger.info(input.getText());
  266. String pattern = "dd MMMMM yyyy HH:mm:ss";
  267. SimpleDateFormat simpleDateFormat =
  268. new SimpleDateFormat(pattern);
  269.  
  270. String date = simpleDateFormat.format(new Date());
  271.  
  272. textArea.append("\n"+date+" "+input.getText());
  273.  
  274.  
  275. // logText.add(input.getText());
  276. // list.add(new JLabel(input.getText()));
  277.  
  278. } catch (SQLException e1) {
  279. // TODO Auto-generated catch block
  280. e1.printStackTrace();
  281. }
  282. if(rs.items.length>0) {
  283.  
  284. Vector<String> names1= rs.Names;
  285. table = new JTable();
  286. scrollPane.setViewportView(table);
  287. DefaultTableModel tableModel = new DefaultTableModel() {
  288.  
  289. @Override
  290. public boolean isCellEditable(int row, int column) {
  291. //all cells false
  292. return false;
  293. }
  294. };
  295. table.setModel(tableModel);
  296. model = (DefaultTableModel) table.getModel();
  297.  
  298. for(int j=0;j<names1.size();j++) {
  299. model.addColumn(names1.get(j));
  300.  
  301. }
  302. for(int i=0;i<rs.items.length;i++) {
  303.  
  304. model.addRow(rs.items[i]);
  305.  
  306. }
  307.  
  308. input.setText("success :)");
  309.  
  310. }
  311. // if (parser.checkInput(input.getText()) == 8) {
  312. // Vector<String> columns= partitions.CreateTable(input.getText());
  313. // table = new JTable();
  314. // flag=1;
  315. // scrollPane.setViewportView(table);
  316. // model = (DefaultTableModel) table.getModel();
  317. // comboBox.addItem(columns.lastElement());
  318. // counter = comboBox.getItemCount();
  319. // comboBox.setSelectedIndex(counter-1);
  320. //
  321. // for(int i=0;i<columns.size()-1;i++) {
  322. // model.addColumn(columns.elementAt(i));
  323. //
  324. // }
  325. //// arr[0]=table;
  326. //// System.out.println( table.getColumnModel().getColumnCount());
  327. //// System.out.println(tables.size());
  328. // }else if (parser.checkInput(input.getText())==3) {
  329. // partitions.DropDatabase(input.getText());
  330. // if(label.getText().compareToIgnoreCase(partitions.getDropDataBaseName())==0) {
  331. // label.setText("");
  332. //
  333. // counter=1;
  334. // JTable table2 = new JTable();
  335. // scrollPane.setViewportView(null);
  336. //// scrollPane.setViewportView(table2);
  337. // model = (DefaultTableModel) table2.getModel();
  338. // input.setText("success :)");
  339. // comboBox.removeAllItems();
  340. // }
  341. // }
  342. // else if (parser.checkInput(input.getText())==4) {
  343. // partitions.DropTable(input.getText());
  344. // for(int i=0;i<comboBox.getItemCount();i++){
  345. // if(partitions.getTablename().compareToIgnoreCase((String) comboBox.getItemAt(i))==0) {
  346. //// comboBox.remove(i);
  347. // comboBox.removeItemAt(i);
  348. // }
  349. // }
  350. // }
  351. // else if(parser.checkInput(input.getText())==2) {
  352. // comboBox.removeAllItems();
  353. // counter=1;
  354. // table = new JTable();
  355. // scrollPane.setViewportView(table);
  356. //
  357. // String name = partitions.CreateDatabase(input.getText()) ;
  358. // label.setText(name);
  359. // input.setText("success :)");
  360. // }
  361. // 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) {
  362. // try {
  363. // System.out.println("sh215o");
  364. // Vector<String> names1= db.getNames((String) comboBox.getSelectedItem());
  365. // Object[][] table1 = db.executeQuery("SELECT * FROM "+comboBox.getSelectedItem());
  366. // table = new JTable();
  367. // scrollPane.setViewportView(table);
  368. // DefaultTableModel tableModel = new DefaultTableModel() {
  369. //
  370. // @Override
  371. // public boolean isCellEditable(int row, int column) {
  372. // //all cells false
  373. // return false;
  374. // }
  375. // };
  376. // table.setModel(tableModel);
  377. // model = (DefaultTableModel) table.getModel();
  378. //
  379. // for(int j=0;j<names1.size();j++) {
  380. // model.addColumn(names1.get(j));
  381. //
  382. // }
  383. // for(int i=0;i<table1.length;i++) {
  384. //
  385. // model.addRow(table1[i]);
  386. //
  387. // }
  388. // } catch (SQLException e1) {
  389. // // TODO Auto-generated catch block
  390. // e1.printStackTrace();
  391. // }
  392. // input.setText("success :)");
  393. // }
  394. // else if (parser.checkInput(input.getText())==1) {
  395. // try {
  396. // Partitions p = new Partitions();
  397. // p.SelectTable(input.getText());
  398. // Vector<String> names1= db.getNames(p.getTablename());
  399. // Object[][] table1 = db.executeQuery("SELECT * FROM "+p.getTablename());
  400. // comboBox.setSelectedItem(p.getTablename());
  401. // counter = comboBox.getSelectedIndex()+1;
  402. // table = new JTable();
  403. // scrollPane.setViewportView(table);
  404. // DefaultTableModel tableModel = new DefaultTableModel() {
  405. //
  406. // @Override
  407. // public boolean isCellEditable(int row, int column) {
  408. // //all cells false
  409. // return false;
  410. // }
  411. // };
  412. // table.setModel(tableModel);
  413. // model = (DefaultTableModel) table.getModel();
  414. //
  415. // for(int j=0;j<names1.size();j++) {
  416. // model.addColumn(names1.get(j));
  417. //
  418. // }
  419. // for(int i=0;i<table1.length;i++) {
  420. //
  421. // model.addRow(table1[i]);
  422. //
  423. // }
  424. // } catch (SQLException e1) {
  425. // // TODO Auto-generated catch block
  426. // e1.printStackTrace();
  427. // }
  428. // input.setText("success :)");
  429. // }
  430. // else if (parser.checkInput(input.getText())==9) {
  431. // try {
  432. // Partitions p = new Partitions();
  433. // p.Select(input.getText());
  434. // Vector<String> names1= db.getNames(p.getTablename());
  435. // Object[][] table1 = db.executeQuery(input.getText());
  436. // comboBox.setSelectedItem(p.getTablename());
  437. // counter = comboBox.getSelectedIndex()+1;
  438. // table = new JTable();
  439. // scrollPane.setViewportView(table);
  440. // DefaultTableModel tableModel = new DefaultTableModel() {
  441. //
  442. // @Override
  443. // public boolean isCellEditable(int row, int column) {
  444. // //all cells false
  445. // return false;
  446. // }
  447. // };
  448. // table.setModel(tableModel);
  449. // model = (DefaultTableModel) table.getModel();
  450. //
  451. // for(int j=0;j<names1.size();j++) {
  452. // model.addColumn(names1.get(j));
  453. //
  454. // }
  455. // for(int i=0;i<table1.length;i++) {
  456. //
  457. // model.addRow(table1[i]);
  458. //
  459. // }
  460. // } catch (SQLException e1) {
  461. // // TODO Auto-generated catch block
  462. // e1.printStackTrace();
  463. // }
  464. // input.setText("success :)");
  465. // }
  466. // else if (parser.checkInput(input.getText())==10) {
  467. // try {
  468. // Partitions p = new Partitions();
  469. // p.selecttwocolumnscondition(input.getText());
  470. // Object[][] table1 = db.executeQuery(input.getText());
  471. // comboBox.setSelectedItem(p.getTablename());
  472. // counter = comboBox.getSelectedIndex()+1;
  473. // table = new JTable();
  474. // scrollPane.setViewportView(table);
  475. // DefaultTableModel tableModel = new DefaultTableModel() {
  476. //
  477. // @Override
  478. // public boolean isCellEditable(int row, int column) {
  479. // //all cells false
  480. // return false;
  481. // }
  482. // };
  483. // table.setModel(tableModel);
  484. // model = (DefaultTableModel) table.getModel();
  485. //
  486. // model.addColumn(p.getselectconditioncloumn1());
  487. // for(int i=0;i<table1.length;i++) {
  488. //
  489. // model.addRow(table1[i]);
  490. //
  491. // }
  492. // } catch (SQLException e1) {
  493. // // TODO Auto-generated catch block
  494. // e1.printStackTrace();
  495. // }
  496. // input.setText("success :)");
  497. // }
  498. // else if (parser.checkInput(input.getText()) != 0) {
  499. // input.setText("success :)");
  500. // } else {
  501. // input.setText("wrong input");
  502. // }
  503. //
  504. }
  505. });
  506.  
  507. }
  508. catch (SQLException e1) {
  509. // TODO Auto-generated catch block
  510. e1.printStackTrace();
  511. }
  512. // process.addActionListener(new ActionListener() {
  513. // public void actionPerformed(ActionEvent e) {
  514. // }
  515. // });
  516.  
  517. input = new JTextField();
  518. GridBagConstraints gbc_input = new GridBagConstraints();
  519. gbc_input.gridwidth = 8;
  520. gbc_input.insets = new Insets(0, 0, 5, 5);
  521. gbc_input.fill = GridBagConstraints.HORIZONTAL;
  522. gbc_input.gridx = 0;
  523. gbc_input.gridy = 2;
  524. frame.getContentPane().add(input, gbc_input);
  525. input.setColumns(10);
  526. GridBagConstraints gbc_process = new GridBagConstraints();
  527. gbc_process.insets = new Insets(0, 0, 5, 5);
  528. gbc_process.gridx = 10;
  529. gbc_process.gridy = 2;
  530. frame.getContentPane().add(process, gbc_process);
  531. GridBagConstraints gbc_textArea = new GridBagConstraints();
  532. gbc_textArea.insets = new Insets(0, 0, 5, 5);
  533. gbc_textArea.fill = GridBagConstraints.BOTH;
  534. gbc_textArea.gridx = 0;
  535. gbc_textArea.gridy = 6;
  536. frame.getContentPane().add(textArea, gbc_textArea);
  537.  
  538.  
  539. // addWindowListener(new WindowAdapter() {
  540. // public void windowClosing(WindowEvent e) {
  541. // int confirmed = JOptionPane.showConfirmDialog(null,
  542. // "Are you sure you want to exit the program?", "Exit Program Message Box",
  543. // JOptionPane.YES_NO_OPTION);
  544. //
  545. // if (confirmed == JOptionPane.YES_OPTION) {
  546. // dispose();
  547. // }
  548. // }
  549. // });
  550. }
  551.  
  552.  
  553. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement