Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 37.26 KB | None | 0 0
  1. 1// This class works to call the frame (loginFrame)
  2. 2package travelmanagement;
  3. 3.   
  4. 4public class TravelManagement {
  5. 5.      public static void main(String[] args) {
  6. 6.          loginFrame frame = new loginFrame();
  7. 7.      }
  8. 8}
  9. 9.   
  10. 10. package travelmanagement;
  11. 11. import java.sql.*;
  12. 12. import javax.swing.*;
  13. 13.  
  14. 14.  
  15. 15. public class mySqlConnect {
  16. 16.    
  17. 17.     Connection con = null;
  18. 18.         public static Connection ConnectDC() {
  19. 19.             try {
  20. 20.                 Class.forName("com.mysql.jdbc.Driver");
  21. 21.                 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/travelmanagement", "root", "0786");
  22. 22.                 return con;
  23. 23.             } catch (Exception e) {
  24. 24.                 return null;
  25. 25.             }
  26. 26.         }
  27. 27.     }
  28. 28.  
  29. 29.  
  30. 30. package travelmanagement;
  31. 31.  
  32. 32. import java.awt.Color;
  33. 33. import java.awt.event.ActionEvent;
  34. 34. import java.awt.event.ActionListener;
  35. 35. import javax.swing.JButton;
  36. 36. import javax.swing.JFrame;
  37. 37. import javax.swing.JLabel;
  38. 38. import javax.swing.JPasswordField;
  39. 39. import javax.swing.JTextField;
  40. 40. import java.sql.*;
  41. 41. import javax.swing.ImageIcon;
  42. 42. import javax.swing.JOptionPane;
  43. 43.  
  44. 44. public class loginFrame extends JFrame implements ActionListener {
  45. 45.  
  46. 46.     Connection con = mySqlConnect.ConnectDC();
  47. 47.     PreparedStatement pst = null;
  48. 48.     ResultSet rs = null;
  49. 49.     JLabel heading, usernameLabel, passwordLabel, errorMsg;
  50. 50.     JTextField usernameField;
  51. 51.     JPasswordField passwordField;
  52. 52.     JButton loginButton, exitButton;
  53. 53.  
  54. 54.     loginFrame() {
  55. 55.         this.setSize(350, 225);
  56. 56.         this.setLocationRelativeTo(null);
  57. 57.         this.setUndecorated(true);
  58. 58.         this.setTitle("Login");
  59. 59.         getContentPane().setBackground(Color.WHITE);
  60. 60.  
  61. 61.         heading = new JLabel("Login");
  62. 62.         usernameLabel = new JLabel("Username:");
  63. 63.         usernameField = new JTextField(12);
  64. 64.         passwordLabel = new JLabel("Password:");
  65. 65.         passwordField = new JPasswordField(12);
  66. 66.         loginButton = new JButton("Login");
  67. 67.         loginButton.addActionListener(this);
  68. 68.         exitButton = new JButton("Exit");
  69. 69.         exitButton.addActionListener(this);
  70. 70.         errorMsg = new JLabel("Incorrect Username or Password!");
  71. 71.         errorMsg.setVisible(false);
  72. 72.  
  73. 73.         // 1st x value, 2nd y value, 3rd width, 4th height
  74. 74.         this.setLayout(null);
  75. 75.         heading.setBounds(125, 5, 125, 25);
  76. 76.         heading.setFont(heading.getFont().deriveFont(20f));
  77. 77.         usernameLabel.setBounds(25, 50, 100, 50);
  78. 78.         usernameField.setBounds(150, 60, 150, 30);
  79. 79.         passwordLabel.setBounds(25, 100, 100, 50);
  80. 80.         passwordField.setBounds(150, 105, 150, 30);
  81. 81.         errorMsg.setBounds(75, 147, 200, 30);
  82. 82.         errorMsg.setForeground(Color.red);
  83. 83.         loginButton.setBounds(85, 185, 75, 30);
  84. 84.         exitButton.setBounds(165, 185, 75, 30);
  85. 85.  
  86. 86.         this.add(heading);
  87. 87.         this.add(usernameLabel);
  88. 88.         this.add(usernameField);
  89. 89.         this.add(passwordLabel);
  90. 90.         this.add(passwordField);
  91. 91.         this.add(loginButton);
  92. 92.         this.add(exitButton);
  93. 93.         this.add(errorMsg);
  94. 94.         this.setVisible(true);
  95. 95.     }
  96. 96.    
  97. 97.     public void clearFields() {
  98. 98.         usernameField.setText("");
  99. 99.         passwordField.setText("");
  100. 100.        }
  101. 101.       
  102. 102.        public void checkLogin() {
  103. 103.            String Sql = "SELECT * from login where Username=? and Password=?";
  104. 104.                try {
  105. 105.                    pst = con.prepareStatement(Sql);
  106. 106.                    pst.setString(1, usernameField.getText());
  107. 107.                    pst.setString(2, passwordField.getText());
  108. 108.                    rs = pst.executeQuery();
  109. 109.                    if (rs.next()) {
  110. 110.                        mainFrame frame = new mainFrame();
  111. 111.                        this.dispose();
  112. 112.                    } else {
  113. 113.                        errorMsg.setVisible(true);
  114. 114.                        clearFields();
  115. 115.                    }
  116. 116.                } catch (Exception e) {
  117. 117.     
  118. 118.                }
  119. 119.        }
  120. 120.     
  121. 121.        @Override
  122. 122.        public void actionPerformed(ActionEvent ae) {
  123. 123.            if (ae.getSource() == loginButton) {
  124. 124.                checkLogin();
  125. 125.            } else if (ae.getSource() == exitButton) {
  126. 126.                System.exit(0);
  127. 127.                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  128. 128.            }
  129. 129.        }
  130. 130.     
  131. 131.    }
  132. 132.     
  133. 133.     
  134. 134.    package travelmanagement;
  135. 135.     
  136. 136.    import java.awt.Color;
  137. 137.    import java.awt.event.ActionEvent;
  138. 138.    import java.awt.event.ActionListener;
  139. 139.    import javax.swing.ImageIcon;
  140. 140.    import javax.swing.JButton;
  141. 141.    import javax.swing.JFrame;
  142. 142.     
  143. 143.    public class mainFrame extends JFrame implements ActionListener {
  144. 144.       
  145. 145.        JButton passport, group;
  146. 146.     
  147. 147.        mainFrame() {
  148. 148.            this.setSize(700, 450);
  149. 149.            this.setLocationRelativeTo(null);
  150. 150.            getContentPane().setBackground(Color.WHITE);
  151. 151.            this.setTitle("Bukhari Travel Ltd");
  152. 152.            ImageIcon icon = new ImageIcon(getClass().getResource("logo.jpg"));
  153. 153.            this.setIconImage(icon.getImage());
  154. 154.            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  155. 155.           
  156. 156.            passport = new JButton(new ImageIcon(getClass().getResource("passport.png")));
  157. 157.            group = new JButton(new ImageIcon(getClass().getResource("group.jpg")));
  158. 158.           
  159. 159.            // 1st x value, 2nd y value, 3rd width, 4th height
  160. 160.            this.setLayout(null);
  161. 161.            passport.setBounds(25, 75, 300, 300);
  162. 162.            passport.addActionListener(this);
  163. 163.            group.setBounds(355, 75, 300, 300);
  164. 164.            group.addActionListener(this);
  165. 165.           
  166. 166.            this.add(passport);
  167. 167.            this.add(group);
  168. 168.            this.setVisible(true);
  169. 169.        }
  170. 170.       
  171. 171.        @Override
  172. 172.        public void actionPerformed(ActionEvent ae) {
  173. 173.            if(ae.getSource() == passport) {
  174. 174.                passportFrame frame = new passportFrame();
  175. 175.                this.dispose();
  176. 176.            } else if(ae.getSource() == group) {
  177. 177.                groupFrame frame = new groupFrame();
  178. 178.                this.dispose();
  179. 179.            }
  180. 180.        }
  181. 181.       
  182. 182.    }
  183. 183.     
  184. 184.     
  185. 185.    package travelmanagement;
  186. 186.     
  187. 187.    import com.itextpdf.text.Document;
  188. 188.    import com.itextpdf.text.PageSize;
  189. 189.    import com.itextpdf.text.pdf.PdfPTable;
  190. 190.    import com.itextpdf.text.pdf.PdfWriter;
  191. 191.    import java.awt.Color;
  192. 192.    import java.awt.Desktop;
  193. 193.    import java.awt.Dimension;
  194. 194.    import java.awt.event.ActionEvent;
  195. 195.    import java.awt.event.ActionListener;
  196. 196.    import java.io.File;
  197. 197.    import java.io.FileOutputStream;
  198. 198.    import java.sql.Connection;
  199. 199.    import java.sql.PreparedStatement;
  200. 200.    import java.sql.ResultSet;
  201. 201.    import javax.swing.ImageIcon;
  202. 202.    import javax.swing.JButton;
  203. 203.    import javax.swing.JFileChooser;
  204. 204.    import javax.swing.JFrame;
  205. 205.    import javax.swing.JLabel;
  206. 206.    import javax.swing.JOptionPane;
  207. 207.    import javax.swing.JPanel;
  208. 208.    import javax.swing.JScrollPane;
  209. 209.    import javax.swing.JTable;
  210. 210.    import javax.swing.JTextField;
  211. 211.    import javax.swing.table.DefaultTableCellRenderer;
  212. 212.    import javax.swing.table.DefaultTableModel;
  213. 213.    import net.proteanit.sql.DbUtils;
  214. 214.     
  215. 215.     
  216. 216.    public class passportFrame extends JFrame implements ActionListener {
  217. 217.     
  218. 218.        JLabel heading, agentL, groupNoL, noPassportL, sendingDateL, postageBarcodeL, receivingDateL,
  219. 219.                returnedL, commentsL, insertDataL, PPsummaryL, passportInHandL, passportInHandN, passportInLondonL,
  220. 220.                passportInLondonN;
  221. 221.        JTextField agentTF, groupNoTF, noPassportTF, sendingDateTF, postageBarcodeTF, receivingDateTF, returnedTF,
  222. 222.                commentsTF;
  223. 223.        JButton backButton, addRow, insertButton, deleteButton, editButton, printButton;
  224. 224.        JPanel mainTablePanel, insertDataPanel, summaryPanel;
  225. 225.        JTable mainTable;
  226. 226.        JScrollPane scrollPane;
  227. 227.        Connection con = mySqlConnect.ConnectDC();;
  228. 228.        PreparedStatement pst = null;
  229. 229.        ResultSet rs = null;
  230. 230.     
  231. 231.        passportFrame() {
  232. 232.            this.setLocationRelativeTo(null);
  233. 233.            this.getContentPane().setBackground(Color.WHITE);
  234. 234.            this.setTitle("Bukhari Travel Ltd");
  235. 235.            ImageIcon icon = new ImageIcon(getClass().getResource("logo.jpg"));
  236. 236.            this.setIconImage(icon.getImage());
  237. 237.            this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  238. 238.            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  239. 239.           
  240. 240.     
  241. 241.            heading = new JLabel("Manage Your Passports");
  242. 242.            insertDataL = new JLabel("Add New Group");
  243. 243.            agentL = new JLabel("Agent:");
  244. 244.            groupNoL = new JLabel("Group No.:");
  245. 245.            noPassportL = new JLabel("No. of Passports:");
  246. 246.            sendingDateL = new JLabel("Sending Date:");
  247. 247.            postageBarcodeL = new JLabel("Postage Barcode:");
  248. 248.            receivingDateL = new JLabel("Receiving Date:");
  249. 249.            returnedL = new JLabel("Returned:");
  250. 250.            commentsL = new JLabel("Travelling Date:");
  251. 251.            PPsummaryL = new JLabel("Passports Summary");
  252. 252.            passportInHandL = new JLabel("Passports in Hand");
  253. 253.            passportInHandN = new JLabel();
  254. 254.            passportInLondonL = new JLabel("Passports in London");
  255. 255.            passportInLondonN = new JLabel();
  256. 256.           
  257. 257.            agentTF = new JTextField();
  258. 258.            groupNoTF = new JTextField();
  259. 259.            noPassportTF = new JTextField();
  260. 260.            sendingDateTF = new JTextField();
  261. 261.            postageBarcodeTF = new JTextField();
  262. 262.            receivingDateTF = new JTextField();
  263. 263.            returnedTF = new JTextField();
  264. 264.            commentsTF = new JTextField();
  265. 265.     
  266. 266.            insertButton = new JButton("Add");
  267. 267.            insertButton.addActionListener(this);
  268. 268.            editButton = new JButton("Edit");
  269. 269.            editButton.addActionListener(this);
  270. 270.            deleteButton = new JButton("Delete");
  271. 271.            deleteButton.addActionListener(this);
  272. 272.            printButton = new JButton("Print PDF");
  273. 273.            printButton.addActionListener(this);
  274. 274.            backButton = new JButton("Go Back");
  275. 275.            backButton.addActionListener(this);
  276. 276.           
  277. 277.            DefaultTableModel mainTableModel = new DefaultTableModel();
  278. 278.            mainTable = new JTable(mainTableModel);
  279. 279.            scrollPane = new JScrollPane(mainTable);
  280. 280.           
  281. 281.            insertDataPanel = new JPanel();
  282. 282.            mainTablePanel = new JPanel();
  283. 283.            summaryPanel = new JPanel();
  284. 284.           
  285. 285.            insertDataPanel.add(insertDataL);
  286. 286.            insertDataPanel.add(agentL);
  287. 287.            insertDataPanel.add(groupNoL);
  288. 288.            insertDataPanel.add(noPassportL);
  289. 289.            insertDataPanel.add(sendingDateL);
  290. 290.            insertDataPanel.add(postageBarcodeL);
  291. 291.            insertDataPanel.add(receivingDateL);
  292. 292.            insertDataPanel.add(returnedL);
  293. 293.            insertDataPanel.add(commentsL);
  294. 294.            insertDataPanel.add(agentTF);
  295. 295.            insertDataPanel.add(groupNoTF);
  296. 296.            insertDataPanel.add(noPassportTF);
  297. 297.            insertDataPanel.add(sendingDateTF);
  298. 298.            insertDataPanel.add(postageBarcodeTF);
  299. 299.            insertDataPanel.add(receivingDateTF);
  300. 300.            insertDataPanel.add(returnedTF);
  301. 301.            insertDataPanel.add(commentsTF);
  302. 302.            insertDataPanel.add(insertButton);
  303. 303.            insertDataPanel.add(editButton);
  304. 304.            insertDataPanel.add(deleteButton);
  305. 305.     
  306. 306.            mainTableModel.addColumn("Agent");
  307. 307.            mainTableModel.addColumn("Group No.");
  308. 308.            mainTableModel.addColumn("No. of Passports");
  309. 309.            mainTableModel.addColumn("Sending Date");
  310. 310.            mainTableModel.addColumn("Postage Barcode");
  311. 311.            mainTableModel.addColumn("Receiving Date");
  312. 312.            mainTableModel.addColumn("Returned");
  313. 313.            mainTableModel.addColumn("Comments");
  314. 314.     
  315. 315.            summaryPanel.add(PPsummaryL);
  316. 316.            summaryPanel.add(passportInHandL);
  317. 317.            summaryPanel.add(passportInHandN);
  318. 318.            summaryPanel.add(passportInLondonL);
  319. 319.            summaryPanel.add(passportInLondonN);
  320. 320.           
  321. 321.            // 1st x value, 2nd y value, 3rd width, 4th height
  322. 322.            heading.setBounds(575, 10, 300, 30);
  323. 323.            heading.setFont(heading.getFont().deriveFont(25f));
  324. 324.     
  325. 325.            insertDataPanel.setBounds(20, 50, 375, 375);
  326. 326.            insertDataL.setBounds(120, 10, 200, 30);
  327. 327.            insertDataL.setFont(insertDataL.getFont().deriveFont(20f));
  328. 328.            agentL.setBounds(20, 62, 100, 15);
  329. 329.            groupNoL.setBounds(20, 92, 100, 15);
  330. 330.            noPassportL.setBounds(20, 122, 100, 15);
  331. 331.            sendingDateL.setBounds(20, 152, 100, 15);
  332. 332.            postageBarcodeL.setBounds(20, 182, 150, 15);
  333. 333.            receivingDateL.setBounds(20, 212, 100, 15);
  334. 334.            returnedL.setBounds(20, 242, 100, 15);
  335. 335.            commentsL.setBounds(20, 272, 100, 15);
  336. 336.     
  337. 337.            agentTF.setBounds(180, 55, 150, 25);
  338. 338.            groupNoTF.setBounds(180, 85, 150, 25);
  339. 339.            noPassportTF.setBounds(180, 115, 150, 25);
  340. 340.            sendingDateTF.setBounds(180, 145, 150, 25);
  341. 341.            postageBarcodeTF.setBounds(180, 175, 150, 25);
  342. 342.            receivingDateTF.setBounds(180, 205, 150, 25);
  343. 343.            returnedTF.setBounds(180, 235, 150, 25);
  344. 344.            commentsTF.setBounds(180, 265, 150, 25);
  345. 345.     
  346. 346.            insertButton.setBounds(55, 320, 75, 30);
  347. 347.            editButton.setBounds(155, 320, 75, 30);
  348. 348.            deleteButton.setBounds(255, 320, 75, 30);
  349. 349.     
  350. 350.            mainTablePanel.setBounds(425, 50, 900, 500);
  351. 351.            mainTable.setLayout(null);
  352. 352.            mainTable.setBounds(475, 300, 1000, 500);
  353. 353.            mainTable.setRowHeight(25);
  354. 354.            mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  355. 355.     
  356. 356.            summaryPanel.setBounds(20, 435, 375, 250);
  357. 357.            summaryPanel.setLayout(null);
  358. 358.            PPsummaryL.setFont(PPsummaryL.getFont().deriveFont(20f));
  359. 359.            PPsummaryL.setBounds(85, 10, 200, 30);
  360. 360.            passportInHandL.setBounds(20, 60, 150, 30);
  361. 361.            passportInHandN.setBounds(200, 60, 150, 30);
  362. 362.            passportInLondonL.setBounds(20, 90, 150, 30);
  363. 363.            passportInLondonN.setBounds(200, 90, 150, 30);
  364. 364.     
  365. 365.            backButton.setBounds(675, 650, 100, 35);
  366. 366.            printButton.setBounds(560, 650, 100, 35);
  367. 367.     
  368. 368.            mainTablePanel.add(scrollPane);
  369. 369.            scrollPane.setPreferredSize(new Dimension(900, 500));
  370. 370.           
  371. 371.            this.add(heading);
  372. 372.            this.add(insertDataPanel);
  373. 373.            this.add(mainTablePanel);
  374. 374.            this.add(summaryPanel);
  375. 375.            this.add(printButton);
  376. 376.            this.add(backButton);
  377. 377.            insertDataPanel.setLayout(null);
  378. 378.            summaryPanel.setLayout(null);
  379. 379.            this.setLayout(null);
  380. 380.            retrieveData();
  381. 381.            this.setVisible(true);
  382. 382.        }
  383. 383.     
  384. 384.        private void retrieveData() {
  385. 385.            try {
  386. 386.                String sqlShowData = "SELECT * FROM `passports`";
  387. 387.                pst = con.prepareStatement(sqlShowData);
  388. 388.                rs = pst.executeQuery();
  389. 389.                mainTable.setModel(DbUtils.resultSetToTableModel(rs));
  390. 390.            } catch (Exception ex) {
  391. 391.                System.out.println(ex);
  392. 392.            }
  393. 393.        }
  394. 394.     
  395. 395.        private void clearField() {
  396. 396.            agentTF.setText("");
  397. 397.            groupNoTF.setText("");
  398. 398.            noPassportTF.setText("");
  399. 399.            sendingDateTF.setText("");
  400. 400.            postageBarcodeTF.setText("");
  401. 401.            receivingDateTF.setText("");
  402. 402.            returnedTF.setText("");
  403. 403.            commentsTF.setText("");
  404. 404.        }
  405. 405.     
  406. 406.        private void addGroup() {
  407. 407.            try {
  408. 408.                String sqlInsert = "INSERT INTO `passports`(`Agent`, `GroupNo`, `NoPassports`, `SendingDate`, `Postage`, `ReceivingDate`, `Returned`, `Travelling Date`) VALUES (?,?,?,?,?,?,?,?)";
  409. 409.                pst = con.prepareStatement(sqlInsert);
  410. 410.                pst.setString(1, agentTF.getText());
  411. 411.                pst.setString(2, groupNoTF.getText());
  412. 412.                pst.setString(3, noPassportTF.getText());
  413. 413.                pst.setString(4, sendingDateTF.getText());
  414. 414.                pst.setString(5, postageBarcodeTF.getText());
  415. 415.                pst.setString(6, receivingDateTF.getText());
  416. 416.                pst.setString(7, returnedTF.getText());
  417. 417.                pst.setString(8, commentsTF.getText());
  418. 418.                pst.executeUpdate();
  419. 419.                JOptionPane.showMessageDialog(null, "Successfully Inserted!");
  420. 420.                retrieveData();
  421. 421.                clearField();
  422. 422.            } catch (Exception ex) {
  423. 423.                JOptionPane.showMessageDialog(null, ex);
  424. 424.            }
  425. 425.        }
  426. 426.     
  427. 427.        private void deleteGroup() {
  428. 428.            try {
  429. 429.                String sqlDelete = "DELETE FROM `passports` WHERE `GroupNo` = ?";
  430. 430.                pst = con.prepareStatement(sqlDelete);
  431. 431.                pst.setString(1, groupNoTF.getText());
  432. 432.                pst.executeUpdate();
  433. 433.                JOptionPane.showMessageDialog(null, "Successfully Deleted!");
  434. 434.                retrieveData();
  435. 435.                clearField();
  436. 436.            } catch (Exception ex) {
  437. 437.                JOptionPane.showMessageDialog(null, ex);
  438. 438.            }
  439. 439.        }
  440. 440.     
  441. 441.        private void updateGroup() {
  442. 442.            try {
  443. 443.                String sqlUpdate = "UPDATE `passports` SET `Agent`=?,`NoPassports`=?,`SendingDate`=?,`Postage`=?,`ReceivingDate`=?,`Returned`=?,`Travelling Date`=? WHERE `GroupNo`=?";
  444. 444.                pst = con.prepareStatement(sqlUpdate);
  445. 445.                pst.setString(8, groupNoTF.getText());
  446. 446.                pst.setString(1, agentTF.getText());
  447. 447.                pst.setString(2, noPassportTF.getText());
  448. 448.                pst.setString(3, sendingDateTF.getText());
  449. 449.                pst.setString(4, postageBarcodeTF.getText());
  450. 450.                pst.setString(5, receivingDateTF.getText());
  451. 451.                pst.setString(6, returnedTF.getText());
  452. 452.                pst.setString(7, commentsTF.getText());
  453. 453.                pst.executeUpdate();
  454. 454.                JOptionPane.showMessageDialog(null, "Successfully Updated!");
  455. 455.                retrieveData();
  456. 456.                clearField();
  457. 457.            } catch (Exception ex) {
  458. 458.                JOptionPane.showMessageDialog(null, ex);
  459. 459.            }
  460. 460.        }
  461. 461.        private void createPDF() {
  462. 462.            String path = "";
  463. 463.            JFileChooser j = new JFileChooser();
  464. 464.            j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  465. 465.            int x = j.showSaveDialog(this);
  466. 466.            if (x == JFileChooser.APPROVE_OPTION) {
  467. 467.                path = j.getSelectedFile().getPath();
  468. 468.            }
  469. 469.           
  470. 470.            Document doc = new Document(PageSize.A4.rotate());
  471. 471.            try {
  472. 472.                PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(path +".pdf"));
  473. 473.                doc.open();
  474. 474.                PdfPTable tbl = new PdfPTable(8);
  475. 475.     
  476. 476.                tbl.addCell("Agent");
  477. 477.                tbl.addCell("Group No.");
  478. 478.                tbl.addCell("No. of Passports");
  479. 479.                tbl.addCell("Sending Out Date");
  480. 480.                tbl.addCell("Postage Barcode");
  481. 481.                tbl.addCell("Receiving Back Date");
  482. 482.                tbl.addCell("Returned");
  483. 483.                tbl.addCell("Comments");
  484. 484.     
  485. 485.                for (int i = 0; i < mainTable.getRowCount(); i++) {
  486. 486.                    String agent = mainTable.getValueAt(i, 0).toString();
  487. 487.                    String groupNo = mainTable.getValueAt(i, 1).toString();
  488. 488.                    String noPassport = mainTable.getValueAt(i, 2).toString();
  489. 489.                    String sendingDate = mainTable.getValueAt(i, 3).toString();
  490. 490.                    String postageBarcode = mainTable.getValueAt(i, 3).toString();
  491. 491.                    String receivingDate = mainTable.getValueAt(i, 4).toString();
  492. 492.                    String returned = mainTable.getValueAt(i, 5).toString();
  493. 493.                    String comments = mainTable.getValueAt(i, 6).toString();
  494. 494.     
  495. 495.                    tbl.addCell(agent);
  496. 496.                    tbl.addCell(groupNo);
  497. 497.                    tbl.addCell(noPassport);
  498. 498.                    tbl.addCell(sendingDate);
  499. 499.                    tbl.addCell(postageBarcode);
  500. 500.                    tbl.addCell(receivingDate);
  501. 501.                    tbl.addCell(returned);
  502. 502.                    tbl.addCell(comments);
  503. 503.                }
  504. 504.                doc.add(tbl);
  505. 505.                doc.close();
  506. 506.                writer.close();
  507. 507.            } catch (Exception ex) {
  508. 508.                System.out.println(ex);
  509. 509.            }
  510. 510.        }
  511. 511.       
  512. 512.        @Override
  513. 513.        public void actionPerformed(ActionEvent ae) {
  514. 514.            if (ae.getSource() == backButton) {
  515. 515.                mainFrame frame = new mainFrame();
  516. 516.                this.dispose();
  517. 517.            } else if (ae.getSource() == insertButton) {
  518. 518.                addGroup();
  519. 519.            } else if (ae.getSource() == editButton) {
  520. 520.                updateGroup();
  521. 521.            } else if (ae.getSource() == deleteButton) {
  522. 522.                deleteGroup();
  523. 523.            } else if (ae.getSource() == printButton) {
  524. 524.                createPDF();
  525. 525.            }
  526. 526.        }
  527. 527.    }
  528. 528.     
  529. 529.    package travelmanagement;
  530. 530.     
  531. 531.    import com.itextpdf.text.Document;
  532. 532.    import com.itextpdf.text.PageSize;
  533. 533.    import com.itextpdf.text.pdf.PdfPTable;
  534. 534.    import com.itextpdf.text.pdf.PdfWriter;
  535. 535.    import java.awt.Color;
  536. 536.    import java.awt.Dimension;
  537. 537.    import java.awt.event.ActionEvent;
  538. 538.    import java.awt.event.ActionListener;
  539. 539.    import java.io.FileOutputStream;
  540. 540.    import java.sql.Connection;
  541. 541.    import java.sql.PreparedStatement;
  542. 542.    import java.sql.ResultSet;
  543. 543.    import javafx.scene.transform.Rotate;
  544. 544.    import javax.swing.ImageIcon;
  545. 545.    import javax.swing.JButton;
  546. 546.    import javax.swing.JFileChooser;
  547. 547.    import javax.swing.JFrame;
  548. 548.    import javax.swing.JLabel;
  549. 549.    import javax.swing.JOptionPane;
  550. 550.    import javax.swing.JPanel;
  551. 551.    import javax.swing.JScrollPane;
  552. 552.    import javax.swing.JTable;
  553. 553.    import javax.swing.JTextField;
  554. 554.    import javax.swing.table.DefaultTableModel;
  555. 555.    import javax.swing.table.TableColumnModel;
  556. 556.    import javax.swing.table.TableModel;
  557. 557.    import net.proteanit.sql.DbUtils;
  558. 558.     
  559. 559.    public class groupFrame extends JFrame implements ActionListener {
  560. 560.     
  561. 561.        Connection con = mySqlConnect.ConnectDC();
  562. 562.        PreparedStatement pst = null;
  563. 563.        PreparedStatement pst2 = null;
  564. 564.        ResultSet rs = null;
  565. 565.        JLabel heading, indexL, ageL, surnameL, forenameL, titleL, passportNoL, DOBL, expiryDateL, contactNoL, invoiceNoL, referenceL;
  566. 566.        JTextField indexTF, ageTF, surnameTF, forenameTF, titleTF, passportNoTF, DOBTF, expiryDateTF, contactNoTF, invoiceNoTF, referenceTF;
  567. 567.        JButton back, addPilgrim, deletePilgrim, updatePilgrim, savePDF;
  568. 568.        JPanel mainTablePanel, insertDataPanel;
  569. 569.        JTable mainTable;
  570. 570.        DefaultTableModel mainTableModel;
  571. 571.        JScrollPane scrollPane;
  572. 572.     
  573. 573.        groupFrame() {
  574. 574.            this.setLocationRelativeTo(null);
  575. 575.            getContentPane().setBackground(Color.WHITE);
  576. 576.            this.setTitle("Bukhari Travel Ltd");
  577. 577.            this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  578. 578.            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  579. 579.            ImageIcon icon = new ImageIcon(getClass().getResource("logo.jpg"));
  580. 580.            this.setIconImage(icon.getImage());
  581. 581.           
  582. 582.           
  583. 583.            //All the labels
  584. 584.            heading = new JLabel("Manage Your Groups");
  585. 585.            indexL = new JLabel("Index");
  586. 586.            ageL = new JLabel("Age");
  587. 587.            surnameL = new JLabel("Surname");
  588. 588.            forenameL = new JLabel("Forename(s)");
  589. 589.            titleL = new JLabel("Title");
  590. 590.            passportNoL = new JLabel("Passport No.");
  591. 591.            DOBL = new JLabel("D.O.B.");
  592. 592.            expiryDateL = new JLabel("Expiry Date");
  593. 593.            contactNoL = new JLabel("Contact No.");
  594. 594.            invoiceNoL = new JLabel("Invoice No.");
  595. 595.            referenceL = new JLabel("Reference");
  596. 596.            indexTF = new JTextField();
  597. 597.           
  598. 598.            //All the Text Fields
  599. 599.            ageTF = new JTextField();
  600. 600.            surnameTF = new JTextField();
  601. 601.            forenameTF = new JTextField();
  602. 602.            titleTF = new JTextField();
  603. 603.            passportNoTF = new JTextField();
  604. 604.            DOBTF = new JTextField();
  605. 605.            expiryDateTF = new JTextField();
  606. 606.            contactNoTF = new JTextField();
  607. 607.            invoiceNoTF = new JTextField();
  608. 608.            referenceTF = new JTextField();
  609. 609.           
  610. 610.            //Buttons
  611. 611.            back = new JButton("Go Back");
  612. 612.            back.addActionListener(this);
  613. 613.            addPilgrim = new JButton("Add Pilgrim");
  614. 614.            addPilgrim.addActionListener(this);
  615. 615.            updatePilgrim = new JButton("Update Pilgrim");
  616. 616.            updatePilgrim.addActionListener(this);
  617. 617.            deletePilgrim = new JButton("Delete Pilgrim");
  618. 618.            deletePilgrim.addActionListener(this);
  619. 619.            savePDF = new JButton("Save PDF");
  620. 620.            savePDF.addActionListener(this);
  621. 621.           
  622. 622.            //Jpanels
  623. 623.            mainTablePanel = new JPanel();
  624. 624.            insertDataPanel = new JPanel();
  625. 625.     
  626. 626.            //Insert Data Panel additions
  627. 627.            insertDataPanel.add(indexL);
  628. 628.            insertDataPanel.add(indexTF);
  629. 629.            insertDataPanel.add(ageL);
  630. 630.            insertDataPanel.add(ageTF);
  631. 631.            insertDataPanel.add(surnameL);
  632. 632.            insertDataPanel.add(surnameTF);
  633. 633.            insertDataPanel.add(forenameL);
  634. 634.            insertDataPanel.add(forenameTF);
  635. 635.            insertDataPanel.add(titleL);
  636. 636.            insertDataPanel.add(titleTF);
  637. 637.            insertDataPanel.add(passportNoL);
  638. 638.            insertDataPanel.add(passportNoTF);
  639. 639.            insertDataPanel.add(DOBL);
  640. 640.            insertDataPanel.add(DOBTF);
  641. 641.            insertDataPanel.add(expiryDateL);
  642. 642.            insertDataPanel.add(expiryDateTF);
  643. 643.            insertDataPanel.add(contactNoL);
  644. 644.            insertDataPanel.add(contactNoTF);
  645. 645.            insertDataPanel.add(invoiceNoL);
  646. 646.            insertDataPanel.add(invoiceNoTF);
  647. 647.            insertDataPanel.add(referenceL);
  648. 648.            insertDataPanel.add(referenceTF);
  649. 649.            insertDataPanel.add(addPilgrim);
  650. 650.            insertDataPanel.add(updatePilgrim);
  651. 651.            insertDataPanel.add(deletePilgrim);
  652. 652.           
  653. 653.            //maintable
  654. 654.            mainTableModel = new DefaultTableModel();
  655. 655.            mainTable = new JTable(mainTableModel);
  656. 656.            scrollPane = new JScrollPane(mainTable);
  657. 657.           
  658. 658.            mainTableModel.addColumn("Index");
  659. 659.            mainTableModel.addColumn("Age");
  660. 660.            mainTableModel.addColumn("Surname");
  661. 661.            mainTableModel.addColumn("Forename(s)");
  662. 662.            mainTableModel.addColumn("Title");
  663. 663.            mainTableModel.addColumn("Passport No.");
  664. 664.            mainTableModel.addColumn("D.O.B.");
  665. 665.            mainTableModel.addColumn("Exiry Date");
  666. 666.            mainTableModel.addColumn("Contact No.");
  667. 667.            mainTableModel.addColumn("Invoice No.");
  668. 668.            mainTableModel.addColumn("Reference");
  669. 669.            mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  670. 670.     
  671. 671.            heading.setBounds(575, 10, 300, 25);
  672. 672.            heading.setFont(heading.getFont().deriveFont(20f));
  673. 673.            mainTablePanel.setBounds(0, 50, 1366, 400);
  674. 674.            insertDataPanel.setBounds(20, 480, 1325, 150);
  675. 675.            savePDF.setBounds(570, 640, 100, 35);
  676. 676.            back.setBounds(700, 640, 100, 35);
  677. 677.     
  678. 678.            indexL.setBounds(30, 10, 40, 25);
  679. 679.            ageL.setBounds(90, 10, 40, 25);
  680. 680.            surnameL.setBounds(170, 10, 80, 25);
  681. 681.            forenameL.setBounds(330, 10, 80, 25);
  682. 682.            titleL.setBounds(490, 10, 40, 25);
  683. 683.            passportNoL.setBounds(560, 10, 80, 25);
  684. 684.            DOBL.setBounds(680, 10, 80, 25);
  685. 685.            expiryDateL.setBounds(800, 10, 80, 25);
  686. 686.            contactNoL.setBounds(920, 10, 80, 25);
  687. 687.            invoiceNoL.setBounds(1050, 10, 80, 25);
  688. 688.            referenceL.setBounds(1170, 10, 80, 25);
  689. 689.            indexTF.setBounds(20, 50, 40, 30);
  690. 690.            ageTF.setBounds(80, 50, 40, 30);
  691. 691.            surnameTF.setBounds(140, 50, 150, 30);
  692. 692.            forenameTF.setBounds(310, 50, 150, 30);
  693. 693.            titleTF.setBounds(480, 50, 40, 30);
  694. 694.            passportNoTF.setBounds(540, 50, 100, 30);
  695. 695.            DOBTF.setBounds(660, 50, 100, 30);
  696. 696.            expiryDateTF.setBounds(780, 50, 100, 30);
  697. 697.            contactNoTF.setBounds(900, 50, 110, 30);
  698. 698.            invoiceNoTF.setBounds(1030, 50, 100, 30);
  699. 699.            referenceTF.setBounds(1150, 50, 150, 30);
  700. 700.            mainTable.setRowHeight(25);
  701. 701.            addPilgrim.setBounds(460, 100, 125, 35);
  702. 702.            updatePilgrim.setBounds(600, 100, 125, 35);
  703. 703.            deletePilgrim.setBounds(740, 100, 125, 35);
  704. 704.     
  705. 705.            mainTablePanel.add(scrollPane);
  706. 706.            scrollPane.setPreferredSize(new Dimension(1356, 500));
  707. 707.            this.add(heading);
  708. 708.            this.add(mainTablePanel);
  709. 709.            this.add(insertDataPanel);
  710. 710.            this.add(savePDF);
  711. 711.            this.add(back);
  712. 712.            insertDataPanel.setLayout(null);
  713. 713.            this.setLayout(null);
  714. 714.            retrieveData();
  715. 715.            scrollPane.setVisible(true);
  716. 716.            this.setVisible(true);
  717. 717.        }
  718. 718.     
  719. 719.        private void retrieveData() {
  720. 720.            try {
  721. 721.                String sqlShowData = "SELECT * FROM `grouptemplate`";
  722. 722.                pst = con.prepareStatement(sqlShowData);
  723. 723.                rs = pst.executeQuery();
  724. 724.                mainTable.setModel(DbUtils.resultSetToTableModel(rs));
  725. 725.            } catch (Exception e) {
  726. 726.            }
  727. 727.        }
  728. 728.     
  729. 729.        private void clearField() {
  730. 730.            indexTF.setText("");
  731. 731.            ageTF.setText("");
  732. 732.            surnameTF.setText("");
  733. 733.            forenameTF.setText("");
  734. 734.            titleTF.setText("");
  735. 735.            passportNoTF.setText("");
  736. 736.            DOBTF.setText("");
  737. 737.            expiryDateTF.setText("");
  738. 738.            contactNoTF.setText("");
  739. 739.            invoiceNoTF.setText("");
  740. 740.            referenceTF.setText("");
  741. 741.        }
  742. 742.     
  743. 743.        private void addPilgrim() {
  744. 744.            try {
  745. 745.                String sqlInsert = "INSERT INTO `grouptemplate`(`Index`, `Age`, `Surname`, `Forename(s)`, `Title`, `Passport Number`, `DOB`, `Expiry Date`, `Contact Number`, `Invoice Number`, `Reference`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
  746. 746.                pst = con.prepareStatement(sqlInsert);
  747. 747.                pst.setString(1, indexTF.getText());
  748. 748.                pst.setString(2, ageTF.getText());
  749. 749.                pst.setString(3, surnameTF.getText());
  750. 750.                pst.setString(4, forenameTF.getText());
  751. 751.                pst.setString(5, titleTF.getText());
  752. 752.                pst.setString(6, passportNoTF.getText());
  753. 753.                pst.setString(7, DOBTF.getText());
  754. 754.                pst.setString(8, expiryDateTF.getText());
  755. 755.                pst.setString(9, contactNoTF.getText());
  756. 756.                pst.setString(10, invoiceNoTF.getText());
  757. 757.                pst.setString(11, referenceTF.getText());
  758. 758.                pst.executeUpdate();
  759. 759.                JOptionPane.showMessageDialog(null, "Successfully Inserted!");
  760. 760.                retrieveData();
  761. 761.                clearField();
  762. 762.            } catch (Exception ex) {
  763. 763.                JOptionPane.showMessageDialog(null, ex);
  764. 764.            }
  765. 765.        }
  766. 766.     
  767. 767.        private void deletePilgrim() {
  768. 768.            try {
  769. 769.                String sqlDelete = "DELETE FROM `grouptemplate` WHERE `Index` = ?";
  770. 770.                pst = con.prepareStatement(sqlDelete);
  771. 771.                pst.setString(1, indexTF.getText());
  772. 772.                pst.executeUpdate();
  773. 773.                JOptionPane.showMessageDialog(null, "Successfully Deleted!");
  774. 774.                retrieveData();
  775. 775.                clearField();
  776. 776.            } catch (Exception ex) {
  777. 777.                JOptionPane.showMessageDialog(null, ex);
  778. 778.            }
  779. 779.        }
  780. 780.     
  781. 781.        private void updatePilgrim() {
  782. 782.            try {
  783. 783.                String sqlUpdate = "UPDATE `grouptemplate` SET `Age`=?,`Surname`=?,`Forename(s)`=?,`Title`=?,`Passport Number`=?,`DOB`=?,`Expiry Date`=?,`Contact Number`=?,`Invoice Number`=?,`Reference`=? WHERE ?";
  784. 784.                pst = con.prepareStatement(sqlUpdate);
  785. 785.                pst.setString(11, indexTF.getText());
  786. 786.                pst.setString(1, ageTF.getText());
  787. 787.                pst.setString(2, surnameTF.getText());
  788. 788.                pst.setString(3, forenameTF.getText());
  789. 789.                pst.setString(4, titleTF.getText());
  790. 790.                pst.setString(5, passportNoTF.getText());
  791. 791.                pst.setString(6, DOBTF.getText());
  792. 792.                pst.setString(7, expiryDateTF.getText());
  793. 793.                pst.setString(8, contactNoTF.getText());
  794. 794.                pst.setString(9, invoiceNoTF.getText());
  795. 795.                pst.setString(10, referenceTF.getText());
  796. 796.                pst.executeUpdate();
  797. 797.                JOptionPane.showMessageDialog(null, "Successfully Updated!");
  798. 798.                retrieveData();
  799. 799.                clearField();
  800. 800.            } catch (Exception ex) {
  801. 801.                JOptionPane.showMessageDialog(null, ex);
  802. 802.            }
  803. 803.        }
  804. 804.     
  805. 805.        private void createPDF() {
  806. 806.            String path = "";
  807. 807.            JFileChooser j = new JFileChooser();
  808. 808.            j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  809. 809.            int x = j.showSaveDialog(this);
  810. 810.            if (x == JFileChooser.APPROVE_OPTION) {
  811. 811.                path = j.getSelectedFile().getPath();
  812. 812.            }
  813. 813.           
  814. 814.            Document doc = new Document(PageSize.A4.rotate());
  815. 815.            try {
  816. 816.                PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(path +".pdf"));
  817. 817.                doc.open();
  818. 818.                PdfPTable tbl = new PdfPTable(11);
  819. 819.     
  820. 820.                tbl.addCell("Index");
  821. 821.                tbl.addCell("Age");
  822. 822.                tbl.addCell("Surname");
  823. 823.                tbl.addCell("Forename (s)");
  824. 824.                tbl.addCell("Title");
  825. 825.                tbl.addCell("Passport No.");
  826. 826.                tbl.addCell("DOB");
  827. 827.                tbl.addCell("Expiry Date");
  828. 828.                tbl.addCell("Contact No.");
  829. 829.                tbl.addCell("Inovice No.");
  830. 830.                tbl.addCell("Reference");
  831. 831.     
  832. 832.                for (int i = 0; i < mainTable.getRowCount(); i++) {
  833. 833.                    String Index = mainTable.getValueAt(i, 0).toString();
  834. 834.                    String Age = mainTable.getValueAt(i, 1).toString();
  835. 835.                    String Surname = mainTable.getValueAt(i, 2).toString();
  836. 836.                    String Forename = mainTable.getValueAt(i, 3).toString();
  837. 837.                    String Title = mainTable.getValueAt(i, 4).toString();
  838. 838.                    String PassportNo = mainTable.getValueAt(i, 5).toString();
  839. 839.                    String DOB = mainTable.getValueAt(i, 6).toString();
  840. 840.                    String ExpiryDate = mainTable.getValueAt(i, 7).toString();
  841. 841.                    String ContactNo = mainTable.getValueAt(i, 8).toString();
  842. 842.                    String InvoiceNo = mainTable.getValueAt(i, 9).toString();
  843. 843.                    String Reference = mainTable.getValueAt(i, 10).toString();
  844. 844.     
  845. 845.                    tbl.addCell(Index);
  846. 846.                    tbl.addCell(Age);
  847. 847.                    tbl.addCell(Surname);
  848. 848.                    tbl.addCell(Forename);
  849. 849.                    tbl.addCell(Title);
  850. 850.                    tbl.addCell(PassportNo);
  851. 851.                    tbl.addCell(DOB);
  852. 852.                    tbl.addCell(ExpiryDate);
  853. 853.                    tbl.addCell(ContactNo);
  854. 854.                    tbl.addCell(InvoiceNo);
  855. 855.                    tbl.addCell(Reference);
  856. 856.                }
  857. 857.                doc.add(tbl);
  858. 858.                doc.close();
  859. 859.                writer.close();
  860. 860.            } catch (Exception ex) {
  861. 861.                JOptionPane.showMessageDialog(null, ex);
  862. 862.            }
  863. 863.        }
  864. 864.     
  865. 865.        @Override
  866. 866.        public void actionPerformed(ActionEvent ae) {
  867. 867.            if (ae.getSource() == back) {
  868. 868.                mainFrame frame = new mainFrame();
  869. 869.                this.dispose();
  870. 870.            } else if (ae.getSource() == addPilgrim) {
  871. 871.                addPilgrim();
  872. 872.            } else if (ae.getSource() == deletePilgrim) {
  873. 873.                deletePilgrim();
  874. 874.            } else if (ae.getSource() == updatePilgrim) {
  875. 875.                updatePilgrim();
  876. 876.            } else if (ae.getSource() == savePDF) {
  877. 877.                createPDF();
  878. 878.            }
  879. 879.        }
  880. 880.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement