Advertisement
Guest User

Java Project

a guest
Aug 13th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 33.79 KB | None | 0 0
  1.  
  2. // Done by: Jethro Muller
  3. //
  4. //IMPORTS
  5. import javax.swing.*;
  6. import java.io.*;
  7. import java.awt.*;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.ActionEvent;
  12. import java.lang.*;
  13. import java.sql.*;
  14. import java.util.*;
  15. import net.proteanit.sql.DbUtils;
  16. import javax.swing.table.DefaultTableModel;
  17. import javax.imageio.*;
  18. import java.text.*;
  19.  
  20. public class RhinoGUI extends JPanel {
  21.     FlowLayout Layout = new FlowLayout();
  22.  
  23.     JLabel l1, l2, l3, l4, l5, l6;
  24.     JLabel spacer = new JLabel("                                                                                                                                                                                                                                               ");
  25.     private JTable table;
  26.     private JTable tableRanger;
  27.     private PreparedStatement st;
  28.     private ResultSet rs;
  29.     JPasswordField txtPass;
  30.     JTextField txtBox;
  31.     JScrollPane RangerScroll;
  32.     int loggedIn = 0;
  33.  
  34.     int alreadyLogged;
  35.  
  36.     JButton btnLogin;
  37.  
  38.     JButton HeadRangerLogin;
  39.     JButton AccountCreate;
  40.     JLabel HRPass;
  41.     JLabel newUser;
  42.     JLabel newPass;
  43.     JTextField TxtNewUser;
  44.     JPasswordField TxtNewPass;
  45.     JPasswordField TxtBossPass;
  46.     int HRLogged = 0;
  47.  
  48.     String delTag = " ";
  49.     JButton delAllBtn = new JButton("Delete Data");
  50.  
  51.  
  52.     String delStr;
  53.     Statement statement;
  54.  
  55.     String sName, sAge, sTag, sGender;
  56.     int iAge, iTag, passed, iGender;
  57.  
  58.     String sGPSX, sGPSY, ComboGPS, sLocation, sTime, sTagR, newTag;
  59.     int iTagR, passedR, iGPS, newITag;
  60.     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  61.  
  62.     boolean delRhino, delSighting;
  63.     int[] TagArray;
  64.     java.util.List < Integer > RhinoTagList;
  65.  
  66.     public static void main(String[] args) throws ClassNotFoundException, SQLException, Exception {
  67.         String conn = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb";
  68.         String user = "";
  69.         String pass = "";
  70.         Connection dataBase;
  71.  
  72.         //connection\
  73.         try {
  74.  
  75.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  76.             dataBase = DriverManager.getConnection(conn, user, pass);
  77.  
  78.             JOptionPane.showMessageDialog(null, "Connection to database successful");
  79.         } catch (Exception e) {
  80.  
  81.             JOptionPane.showMessageDialog(null, "error in Connection to database");
  82.             System.exit(0);
  83.         } //end
  84.  
  85.         new RhinoGUI();
  86.     }
  87.     //Date Validation
  88.  
  89.     boolean isValidDate(String input) {
  90.         if (input != null) {
  91.             try {
  92.                 format.setLenient(false);
  93.                 format.parse(input);
  94.                 return true;
  95.             } catch (ParseException e) {
  96.                 return false;
  97.             }
  98.         } else {
  99.             return false;
  100.         }
  101.  
  102.     }
  103.     /*
  104.     /////////Convert
  105.      public static int[] convertIntegers(java.util.List<Integer> elemIntegers)
  106.     {
  107.         int[] elements = new int[elemIntegers.size()];
  108.         for (int i=0; i < elements.length; i++)
  109.         {
  110.             elements[i] = elemIntegers.get(i).intValue();
  111.         }
  112.         return elements;
  113.     }*/
  114.  
  115.     ///updating the table
  116.     private void UpdateJTable() throws SQLException {
  117.  
  118.         Connection upTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", "");
  119.         String sql = "select * from TblRhinoInfo";
  120.         ResultSet rs;
  121.         PreparedStatement st = null;
  122.         try {
  123.             st = upTable.prepareStatement(sql);
  124.             rs = st.executeQuery();
  125.             table.setModel(DbUtils.resultSetToTableModel(rs));
  126.  
  127.  
  128.         } catch (Exception e) {
  129.             JOptionPane.showMessageDialog(null, e);
  130.         }
  131.     }
  132.  
  133.     ///updating the Ranger table
  134.     private void UpdateJTableRanger() throws SQLException {
  135.  
  136.         Connection upTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", "");
  137.         String sql = "select * from TblRhinoLoc";
  138.         ResultSet rs;
  139.         PreparedStatement st = null;
  140.         try {
  141.             st = upTable.prepareStatement(sql);
  142.             rs = st.executeQuery();
  143.             tableRanger.setModel(DbUtils.resultSetToTableModel(rs));
  144.             txtBox.setText("");
  145.             txtPass.setText("");
  146.  
  147.         } catch (Exception e) {
  148.             JOptionPane.showMessageDialog(null, e);
  149.         }
  150.     }
  151.  
  152.     ///deleting from the both tables
  153.     private void DeleteData() throws SQLException {
  154.  
  155.         Connection DelTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", "");
  156.         String delSQL1 = "delete * from TblRhinoLoc where RhinoTag = '" + delStr + "' ";
  157.         String delSQL2 = "delete * from TblRhinoInfo where RhinoTag = '" + delStr + "' ";
  158.         ResultSet rs;
  159.         PreparedStatement st = null;
  160.         try {
  161.             if (delSighting) {
  162.                 statement = DelTable.createStatement();
  163.                 statement.executeUpdate(delSQL1);
  164.             } else if (delRhino) {
  165.                 statement = DelTable.createStatement();
  166.                 statement.executeUpdate(delSQL1);
  167.  
  168.                 statement = DelTable.createStatement();
  169.                 statement.executeUpdate(delSQL2);
  170.             }
  171.         } catch (Exception e) {
  172.             JOptionPane.showMessageDialog(null, e);
  173.         }
  174.     }
  175.  
  176.  
  177.     ///The GUI
  178.     public RhinoGUI() {
  179.         JFrame main = new JFrame();
  180.         JTabbedPane tabbedPane = new JTabbedPane();
  181.  
  182.         main.addWindowListener(new WindowAdapter() {@Override
  183.             public void windowOpened(WindowEvent arg0) {
  184.                 try {
  185.                     UpdateJTable();
  186.                 } catch (SQLException e) {
  187.                     System.out.println("Error: " + e);
  188.                 }
  189.  
  190.             }
  191.         });
  192.  
  193.         main.setTitle("Rhino Observation Assistant 9001");
  194.         main.setSize(800, 600);
  195.         main.setLocationRelativeTo(null);
  196.         main.setResizable(false);
  197.         /////////////////////////////////////////
  198.  
  199.  
  200.         JPanel panel1 = new JPanel();
  201.         tabbedPane.addTab("Home", null, panel1, null);
  202.         ImageIcon icon = new ImageIcon("C:/Users/Jethro/Desktop/IT Project Files/RhinoGUI/Rhino.jpg");
  203.         JLabel image = new JLabel(icon);
  204.         l3 = new JLabel("                            Rhino Observation Assitant 9001                                    ");
  205.         JTextArea txtArea1 = new JTextArea("Welcome to the Rhino Observation Assistant 9001, its over 9000. Feel free to record your Rhino sightings using this handy application. Please be aware that only park rangers will be able to edit or access sensititve information. \n\nFOR PROJECT PURPOSES: To create an account you must log in as the boss using the password 'bossman'.");
  206.         txtArea1.setEditable(false);
  207.         txtArea1.setWrapStyleWord(true);
  208.         txtArea1.setLineWrap(true);
  209.         JScrollPane home = new JScrollPane(txtArea1);
  210.         home.setPreferredSize(new Dimension(450, 110));
  211.         panel1.add(l3);
  212.         panel1.add(home);
  213.         panel1.add(image);
  214.         panel1.setLayout(Layout);
  215.  
  216.  
  217.         JPanel panel2 = new JPanel();
  218.         tabbedPane.addTab("Rhinos", null, panel2);
  219.  
  220.         ////////////////Primary Table
  221.         table = new JTable();
  222.  
  223.         table.setModel(new DefaultTableModel(
  224.         new Object[][] {
  225.             {
  226.                 null, null, null, null
  227.             }, {
  228.                 null, null, null, null
  229.             }, {
  230.                 null, null, null, null
  231.             }, {
  232.                 null, null, null, null
  233.             }, {
  234.                 null, null, null, null
  235.             }, {
  236.                 null, null, null, null
  237.             },
  238.         },
  239.         new String[] {
  240.             "Tag Number", "Rhino Name", "Rhino Age", "Rhino Gender"
  241.         }) {
  242.             boolean[] columnEditables = new boolean[] {
  243.                 false, false, false, false
  244.             };
  245.             public boolean isCellEditable(int row, int column) {
  246.                 return columnEditables[column];
  247.             }
  248.         });
  249.         table.getColumnModel().getColumn(0).setResizable(false);
  250.         table.getColumnModel().getColumn(1).setResizable(false);
  251.         table.getColumnModel().getColumn(2).setResizable(false);
  252.         table.getColumnModel().getColumn(3).setResizable(false);
  253.  
  254.         JButton refreshTable = new JButton("Refresh Table");
  255.         table.setBorder(null);
  256.         JScrollPane allRhinos = new JScrollPane(table);
  257.         allRhinos.setViewportView(table);
  258.  
  259.         JLabel spacee = new JLabel("                                                                                  ");
  260.         panel2.setLayout(Layout);
  261.  
  262.         panel2.add(refreshTable);
  263.         panel2.add(spacee);
  264.         panel2.add(allRhinos);
  265.  
  266.  
  267.         ////////////////////////////////////____PANEL 3____////////////////////////////////////////////////////////////
  268.         JPanel panel3 = new JPanel();
  269.         tabbedPane.addTab("Edit Rhino Data", null, panel3);
  270.  
  271.         JButton insertTable = new JButton("Tourist Data Insert");
  272.         insertTable.setPreferredSize(new Dimension(700, 125));
  273.         JButton insertTableF = new JButton("Ranger Data Insert");
  274.         insertTableF.setPreferredSize(new Dimension(700, 125));
  275.         delAllBtn = new JButton("Delete Data");
  276.         delAllBtn.setPreferredSize(new Dimension(700, 125));
  277.         JButton UpBtn = new JButton("Update Data");
  278.         UpBtn.setPreferredSize(new Dimension(700, 125));
  279.  
  280.         panel3.add(insertTable);
  281.         panel3.add(insertTableF);
  282.         panel3.add(delAllBtn);
  283.         panel3.add(UpBtn);
  284.  
  285.         //////////////////////////////////////////////////////////////////////////////////////////////////
  286.         JPanel panel4 = new JPanel();
  287.         tabbedPane.addTab("Ranger Login", null, panel4);
  288.  
  289.         txtBox = new JTextField();
  290.         txtPass = new JPasswordField();
  291.         txtBox.setPreferredSize(new Dimension(100, 20));
  292.         txtPass.setPreferredSize(new Dimension(100, 20));
  293.         l4 = new JLabel("You are not authourised to see this information. Please log in above.");
  294.         l5 = new JLabel("Username: ");
  295.         l6 = new JLabel("Password: ");
  296.         btnLogin = new JButton("Login");
  297.  
  298.  
  299.  
  300.  
  301.         ///Ranger Table
  302.  
  303.         tableRanger = new JTable();
  304.  
  305.         tableRanger.setModel(new DefaultTableModel(
  306.         new Object[][] {
  307.             {
  308.                 null, null, null, null
  309.             }, {
  310.                 null, null, null, null
  311.             }, {
  312.                 null, null, null, null
  313.             }, {
  314.                 null, null, null, null
  315.             }, {
  316.                 null, null, null, null
  317.             }, {
  318.                 null, null, null, null
  319.             },
  320.         },
  321.         new String[] {
  322.             "Tag Number", "GPS Coordinates", "Last Time Seen", "Last Location Seen"
  323.         }) {
  324.             boolean[] columnEditables = new boolean[] {
  325.                 false, false, false, false
  326.             };
  327.             public boolean isCellEditable(int row, int column) {
  328.                 return columnEditables[column];
  329.             }
  330.         });
  331.         tableRanger.getColumnModel().getColumn(0).setResizable(false);
  332.         tableRanger.getColumnModel().getColumn(1).setResizable(false);
  333.         tableRanger.getColumnModel().getColumn(2).setResizable(false);
  334.         tableRanger.getColumnModel().getColumn(3).setResizable(false);
  335.  
  336.         tableRanger.setBorder(null);
  337.         RangerScroll = new JScrollPane(tableRanger);
  338.         RangerScroll.setViewportView(tableRanger);
  339.  
  340.         //PANEL 4  
  341.         panel4.setLayout(Layout);
  342.         panel4.add(l5);
  343.         panel4.add(txtBox);
  344.         panel4.add(l6);
  345.         panel4.add(txtPass);
  346.         panel4.add(btnLogin);
  347.         panel4.add(l4);
  348.         panel4.add(RangerScroll);
  349.  
  350.  
  351.         /////////////////////////////////////////////////////////////////////////////////////////////
  352.         JPanel panel5 = new JPanel();
  353.         tabbedPane.addTab("Ranger Account Creation", null, panel5);
  354.  
  355.         HeadRangerLogin = new JButton("Head Ranger Login");
  356.         AccountCreate = new JButton("Create Account");
  357.         HRPass = new JLabel("Admin Password: ");
  358.         newUser = new JLabel("New Username: ");
  359.         newPass = new JLabel("New Password: ");
  360.         TxtNewUser = new JTextField();
  361.         TxtNewPass = new JPasswordField();
  362.         TxtBossPass = new JPasswordField();
  363.  
  364.         TxtBossPass.setPreferredSize(new Dimension(100, 20));
  365.         TxtNewPass.setPreferredSize(new Dimension(100, 20));
  366.         TxtNewUser.setPreferredSize(new Dimension(100, 20));
  367.  
  368.         panel5.add(HRPass);
  369.         panel5.add(TxtBossPass);
  370.         panel5.add(HeadRangerLogin);
  371.         panel5.add(spacer);
  372.         panel5.add(newUser);
  373.         panel5.add(TxtNewUser);
  374.         panel5.add(newPass);
  375.         panel5.add(TxtNewPass);
  376.         panel5.add(AccountCreate);
  377.  
  378.         ////////////////////////////////////////
  379.         main.getContentPane().add(tabbedPane, BorderLayout.CENTER);
  380.  
  381.         main.setDefaultCloseOperation(main.EXIT_ON_CLOSE);
  382.         main.setVisible(true);
  383.         /////New Ranger Account Button
  384.         AccountCreate.addActionListener(new ActionListener() {
  385.             public void actionPerformed(ActionEvent e) {
  386.                 String NewUserStr = TxtNewUser.getText();
  387.                 char[] PassChar2 = TxtNewPass.getPassword();
  388.                 String NewPassStr = new String(PassChar2);
  389.  
  390.  
  391.                 if (HRLogged == 1) {
  392.  
  393.                     if (NewPassStr.equals("") || NewUserStr.equals("")) {
  394.                         JOptionPane.showMessageDialog(null, "Please enter a username and password.");
  395.                     } else {
  396.                         try {
  397.                             // Create file
  398.                             FileWriter fstream = new FileWriter("C:/Users/Jethro/Desktop/IT Project Files/RhinoGUI/RangersData.txt", true);
  399.                             BufferedWriter out = new BufferedWriter(fstream);
  400.                             out.write(NewUserStr + NewPassStr);
  401.                             out.newLine();
  402.                             //Close the output stream
  403.                             JOptionPane.showMessageDialog(null, "New ranger account created. Relogin as admin to create another.");
  404.                             TxtNewPass.setText("");
  405.                             TxtNewUser.setText("");
  406.                             HRLogged = 0;
  407.                             out.close();
  408.                         } catch (Exception eer) {
  409.                             //Catch exception if any
  410.                             System.err.println("Error: " + eer.getMessage());
  411.                         }
  412.                     }
  413.                 } else {
  414.                     JOptionPane.showMessageDialog(null, "You need to get the boss to log in before an account can be created!");
  415.                 }
  416.             }
  417.         });
  418.         /////Head Ranger Login Button
  419.         HeadRangerLogin.addActionListener(new ActionListener() {
  420.             public void actionPerformed(ActionEvent e) {
  421.                 char[] BossChar = TxtBossPass.getPassword();
  422.                 String BossPass = new String(BossChar);
  423.  
  424.  
  425.                 if (HRLogged == 1) {
  426.                     JOptionPane.showMessageDialog(null, "You are already logged in Boss!");
  427.                 } else {
  428.                     if (BossPass.equalsIgnoreCase("bossman")) {
  429.                         HRLogged = 1;
  430.                         TxtBossPass.setText("");
  431.                         JOptionPane.showMessageDialog(null, "You are now logged in.");
  432.                     } else {
  433.                         JOptionPane.showMessageDialog(null, "You failed to log in, try again!");
  434.                     }
  435.                 }
  436.             }
  437.         });
  438.  
  439.         /////Refresh Button
  440.         refreshTable.addActionListener(new ActionListener() {
  441.             public void actionPerformed(ActionEvent e) {
  442.                 try {
  443.                     UpdateJTable();
  444.                 } catch (SQLException er) {
  445.                     System.out.println("Error: " + er);
  446.                 }
  447.  
  448.             }
  449.         });
  450.  
  451.         ///////////////////_____Tourist Insert Button_____\\\\\\\\\\\\\\\\\\\\\\\
  452.         insertTable.addActionListener(new ActionListener() {
  453.             public void actionPerformed(ActionEvent e) {
  454.                 int MessageType = JOptionPane.QUESTION_MESSAGE;
  455.                 String[] options = {
  456.                     "Male", "Female"
  457.                 };
  458.  
  459.  
  460.                 while (passed == 0) {
  461.  
  462.                     try {
  463.                         sTag = JOptionPane.showInputDialog("Please enter the Rhino's Tag Number:");
  464.                         iTag = Integer.parseInt(sTag);
  465.  
  466.                         Connection upTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", "");
  467.                         String sql = "select RhinoTag from TblRhinoInfo";
  468.                         ResultSet rs;
  469.                         PreparedStatement st = null;
  470.  
  471.                         try {
  472.                             st = upTable.prepareStatement(sql);
  473.                             rs = st.executeQuery();
  474.  
  475.                             /*RhinoTagList = new ArrayList<Integer>();
  476.                             while (rs.next()) { // process results one row at a time
  477.                                 RhinoTagList.add(rs.getInt(1));
  478.                             }
  479.                             // convert to int array
  480.                
  481.                             TagArray = convertIntegers(RhinoTagList);
  482.                             RhinoTagList.toArray(TagArray);*/
  483.                         } catch (Exception ex) {
  484.                             JOptionPane.showMessageDialog(null, ex);
  485.                         }
  486.                         TagArray = new int[RhinoTagList.size()];
  487.  
  488.                         for (int i = 0; i < RhinoTagList.size(); i++) {
  489.                             System.out.println(TagArray[i]);
  490.                             if (TagArray[i] == iTag) {
  491.                                 JOptionPane.showMessageDialog(null, "Please use a unique tag number.");
  492.                             } else {
  493.                                 break;
  494.                             }
  495.                         }
  496.  
  497.  
  498.  
  499.  
  500.  
  501.                         sName = JOptionPane.showInputDialog("Please enter the Rhino's Name:");
  502.  
  503.                         sAge = JOptionPane.showInputDialog("Please enter the Rhino's Age:");
  504.                         iAge = Integer.parseInt(sAge);
  505.  
  506.                         iGender = JOptionPane.showOptionDialog(null, "Please enter the gender of the Rhino:", "Rhino Gender", 0, JOptionPane.QUESTION_MESSAGE, null, options, null);
  507.                         switch (iGender) {
  508.                         case 0:
  509.                             sGender = "Male";
  510.                             break;
  511.                         case 1:
  512.                             sGender = "Female";
  513.                             break;
  514.                         }
  515.  
  516.                         passed = 1;
  517.                     } catch (Throwable nfe) {
  518.                         passed = 0;
  519.                         JOptionPane.showMessageDialog(null, "Please use numbers for the Rhino Tag and Rhino Age fields.");
  520.                     }
  521.                 }
  522.  
  523.                 if (passed == 1) {
  524.  
  525.                     try {
  526.                         Connection insertTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", "");
  527.                         String sql = "insert into TblRhinoInfo values ( '" + iTag + "', '" + sName + "', '" + iAge + "', '" + sGender + "')";
  528.                         PreparedStatement st = null;
  529.                         try {
  530.                             statement = insertTable.createStatement();
  531.                             statement.executeUpdate(sql);
  532.  
  533.                             JOptionPane.showMessageDialog(null, "Data entered successfully!");
  534.                         } catch (Exception err) {
  535.                             System.out.println(err);
  536.                             JOptionPane.showMessageDialog(null, err);
  537.                         }
  538.                         UpdateJTable();
  539.                     } catch (SQLException er) {
  540.                         System.out.println("Error: " + er);
  541.                     }
  542.  
  543.                 } else {
  544.  
  545.                 }
  546.             }
  547.         });
  548.  
  549.         ///////////////////////////////////_____Ranger Insert Button_____\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  550.         insertTableF.addActionListener(new ActionListener() {
  551.             public void actionPerformed(ActionEvent e) {
  552.                 int wayPoint1 = 0;
  553.                 int wayPoint2 = 0;
  554.                 int PassTime = 0;
  555.                 Statement statementR;
  556.  
  557.                 if (loggedIn == 1) {
  558.  
  559.                     while (passedR == 0) {
  560.                         try {
  561.                             if (wayPoint1 == 0) {
  562.                                 sTagR = JOptionPane.showInputDialog("Please enter the Rhino Tag number:");
  563.                                 iTagR = Integer.parseInt(sTagR);
  564.                                 wayPoint1 = 1;
  565.                             }
  566.                             if (wayPoint2 == 0) {
  567.                                 sGPSX = JOptionPane.showInputDialog("Please enter the horizontal GPS Grid Numbers(eg.3123):");
  568.                                 iGPS = Integer.parseInt(sGPSX);
  569.  
  570.                                 wayPoint2 = 1;
  571.                             }
  572.  
  573.                             sGPSY = JOptionPane.showInputDialog("Please enter the vertical GPS Grid Letters(eg.XXYY:");
  574.                             while (PassTime == 0) {
  575.                                 sTime = JOptionPane.showInputDialog("Please enter the Last date you saw the Rhino(YYYY-MM-DD):");
  576.                                 //  if (!sTime.equals("") || sTime !=null){
  577.                                 if (isValidDate(sTime)) {
  578.                                     PassTime = 1;
  579.                                 } else {
  580.                                     JOptionPane.showMessageDialog(null, "Please use the date format YYYY-MM-DD.");
  581.                                 }
  582.                                 //  } else {
  583.                                 //      JOptionPane.showMessageDialog(null, "Please use the date format YYYY-MM-DD.");
  584.                                 //  }
  585.                             }
  586.                             sLocation = JOptionPane.showInputDialog("Please enter the Last place you saw the Rhino:");
  587.  
  588.                             passedR = 1;
  589.                         } catch (NumberFormatException nfe) {
  590.                             passedR = 0;
  591.                             JOptionPane.showMessageDialog(null, "Please use numbers for the Rhino Tag field.");
  592.                         }
  593.                     }
  594.                     if (passedR == 1) {
  595.                         ComboGPS = iGPS + " " + sGPSY;
  596.                         try {
  597.                             Connection insertTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", "");
  598.                             String sql = "insert into TblRhinoLoc values ( '" + iTagR + "', '" + ComboGPS + "', '" + sTime + "', '" + sLocation + "')";
  599.  
  600.                             try {
  601.                                 statementR = insertTable.createStatement();
  602.                                 statementR.executeUpdate(sql);
  603.  
  604.                                 JOptionPane.showMessageDialog(null, "Data entered successfully!");
  605.                                 wayPoint1 = 0;
  606.                                 wayPoint2 = 0;
  607.                             } catch (Exception err) {
  608.                                 System.out.println(err);
  609.                                 JOptionPane.showMessageDialog(null, err);
  610.                             }
  611.                             UpdateJTableRanger();
  612.                         } catch (SQLException er) {
  613.                             System.out.println("Error: " + er);
  614.                         }
  615.  
  616.                     } else {
  617.  
  618.                     }
  619.                 } else {
  620.                     JOptionPane.showMessageDialog(null, "Please make sure you are logged in before editing sensitive data.");
  621.                 }
  622.             }
  623.         });
  624.  
  625.         //LOGIN BUTTON ACTIONLISTNER
  626.         btnLogin.addActionListener(new ActionListener() {
  627.             public void actionPerformed(ActionEvent e) {
  628.                 String Username = txtBox.getText();
  629.                 char[] PassChar = txtPass.getPassword();
  630.                 String Pass = new String(PassChar);
  631.                 String ActualCombo = Username + Pass;
  632.                 int passwordtrue = 0;
  633.  
  634.                 if (alreadyLogged == 0) {
  635.  
  636.                     Scanner scanf = null;
  637.  
  638.                     try {
  639.                         scanf = new Scanner(new File("C:/Users/Jethro/Desktop/IT Project Files/RhinoGUI/RangersData.txt"));
  640.                     } catch (FileNotFoundException fne) {
  641.                         System.out.println("Cannot find file");
  642.                     }
  643.                     //get number of lines in file and display the data.
  644.                     int count = 0;
  645.                     while (scanf.hasNext()) {
  646.                         String line = scanf.nextLine();
  647.                         count++;
  648.                         System.out.println(line);
  649.                     }
  650.                     scanf.close();
  651.  
  652.                     String[] Combo = new String[count];
  653.  
  654.                     //reopen file
  655.                     try {
  656.                         scanf = new Scanner(new File("C:/Users/Jethro/Desktop/IT Project Files/RhinoGUI/RangersData.txt"));
  657.                     } catch (FileNotFoundException fne) {
  658.                         JOptionPane.showMessageDialog(null, "File not found!");
  659.                     }
  660.                     //read data from file and create object, store in array
  661.                     for (int i = 0; i < count; i++) {
  662.                         String line = scanf.nextLine(); //read line from file
  663.                         Combo[i] = line;
  664.                     } //end for loop
  665.  
  666.                     for (int i = 0; i < count; i++) {
  667.                         if (ActualCombo.equalsIgnoreCase(Combo[i])) {
  668.                             passwordtrue = 1;
  669.                             break;
  670.                         } else {
  671.  
  672.                         }
  673.                     }
  674.  
  675.                     if (passwordtrue == 1) {
  676.                         try {
  677.                             loggedIn = 1;
  678.                             UpdateJTableRanger();
  679.                         } catch (SQLException er) {
  680.                             System.out.println("Error: " + er);
  681.                         }
  682.                         System.out.println("NOT FAIL!");
  683.                         alreadyLogged = 1;
  684.                         btnLogin.setText("Refresh");
  685.                         l4.setText("                             You have logged in Successfully.                                  ");
  686.                     } else {
  687.                         System.out.println("FAIL!");
  688.                         l4.setText("                                  You have not logged in Successfully.                              ");
  689.                     }
  690.                 } else {
  691.                     try {
  692.                         UpdateJTableRanger();
  693.                     } catch (SQLException errr) {
  694.                         System.out.println("Error: " + errr);
  695.                     }
  696.                     l4.setText("                                  You have already logged in Successfully.                              ");
  697.                 }
  698.             }
  699.         });
  700.  
  701.  
  702.         ///////////////////////////___Delete Button_________\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  703.         delAllBtn.addActionListener(new ActionListener() {
  704.             public void actionPerformed(ActionEvent e) {
  705.                 delRhino = false;
  706.                 delSighting = false;
  707.                 int delChoice;
  708.                 String[] DelOption = {
  709.                     "Delete Only the Sighting", "Delete All of the Rhino's Data"
  710.                 };
  711.  
  712.                 delChoice = JOptionPane.showOptionDialog(null, "Please choose the destiny of the Rhino's data:", "Choices", 0, JOptionPane.QUESTION_MESSAGE, null, DelOption, null);
  713.                 switch (delChoice) {
  714.                 case 0:
  715.                     delSighting = true;
  716.                     break;
  717.                 case 1:
  718.                     delRhino = true;
  719.                     break;
  720.                 }
  721.                 delStr = JOptionPane.showInputDialog("Please enter the tag number of the Rhino's whose data you wish to delete:");
  722.                 if (loggedIn == 1) {
  723.  
  724.                     try {
  725.                         DeleteData();
  726.                         JOptionPane.showMessageDialog(null, "Deletion was successful");
  727.                     } catch (SQLException er) {
  728.                         System.out.println("Error: " + er);
  729.                     }
  730.                 } else {
  731.                     JOptionPane.showMessageDialog(null, "Please make sure you are logged in before trying to edit data.");
  732.                 }
  733.             }
  734.         });
  735.  
  736.         ///////////////////////////////////_____Update Button_____\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  737.         UpBtn.addActionListener(new ActionListener() {
  738.             public void actionPerformed(ActionEvent e) {
  739.                 int MessageType = JOptionPane.QUESTION_MESSAGE;
  740.                 String[] options = {
  741.                     "Male", "Female"
  742.                 };
  743.  
  744.  
  745.                 while (passed == 0) {
  746.  
  747.                     try {
  748.                         sTag = JOptionPane.showInputDialog("Please enter the Rhino's Tag Number whose information you which to edit:");
  749.                         iTag = Integer.parseInt(sTag);
  750.  
  751.                         newTag = JOptionPane.showInputDialog("Please enter the Rhino's Tag Number:");
  752.                         newITag = Integer.parseInt(newTag);
  753.  
  754.                         Connection upTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", "");
  755.                         String sql = "select RhinoTag from TblRhinoInfo";
  756.                         ResultSet rs;
  757.                         PreparedStatement st = null;
  758.  
  759.                         try {
  760.                             st = upTable.prepareStatement(sql);
  761.                             rs = st.executeQuery();
  762.  
  763.                             /*RhinoTagList = new ArrayList<Integer>();
  764.                             while (rs.next()) { // process results one row at a time
  765.                                 RhinoTagList.add(rs.getInt(1));
  766.                             }
  767.                             // convert to int array
  768.                
  769.                             TagArray = convertIntegers(RhinoTagList);
  770.                             RhinoTagList.toArray(TagArray);*/
  771.                         } catch (Exception ex) {
  772.                             JOptionPane.showMessageDialog(null, ex);
  773.                         }
  774.                         /*TagArray = new int [RhinoTagList.size()];
  775.                    
  776.                     for (int i =0;i<RhinoTagList.size();i++){
  777.                         System.out.println(TagArray[i]);
  778.                         if (TagArray[i]==iTag) {
  779.                             JOptionPane.showMessageDialog(null, "Please use a unique tag number.");
  780.                         } else {
  781.                             break;
  782.                         }
  783.                     }*/
  784.  
  785.  
  786.  
  787.  
  788.  
  789.                         sName = JOptionPane.showInputDialog("Please enter the Rhino's Name:");
  790.  
  791.                         sAge = JOptionPane.showInputDialog("Please enter the Rhino's Age:");
  792.                         iAge = Integer.parseInt(sAge);
  793.  
  794.                         iGender = JOptionPane.showOptionDialog(null, "Please enter the gender of the Rhino:", "Rhino Gender", 0, JOptionPane.QUESTION_MESSAGE, null, options, null);
  795.                         switch (iGender) {
  796.                         case 0:
  797.                             sGender = "Male";
  798.                             break;
  799.                         case 1:
  800.                             sGender = "Female";
  801.                             break;
  802.                         }
  803.  
  804.                         passed = 1;
  805.                     } catch (Throwable nfe) {
  806.                         passed = 0;
  807.                         JOptionPane.showMessageDialog(null, "Please use numbers for the Rhino Tag and Rhino Age fields.");
  808.                     }
  809.                 }
  810.  
  811.                 if (passed == 1) {
  812.  
  813.                     try {
  814.                         Connection insertTable = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:/Users/Jethro/Desktop/IT Project Files/dbRhino.mdb", "", "");
  815.                         String sql = "update TblRhinoInfo set RhinoTag = '" + newITag + "', RhinoName = '" + sName + "', RhinoAge = '" + iAge + "', RhinoGender = '" + sGender + "' where RhinoTag = '" + iTag + "'";
  816.                         PreparedStatement st = null;
  817.                         try {
  818.                             statement = insertTable.createStatement();
  819.                             statement.executeUpdate(sql);
  820.  
  821.                             JOptionPane.showMessageDialog(null, "Data edited successfully!");
  822.                         } catch (Exception err) {
  823.                             System.out.println(err);
  824.                             JOptionPane.showMessageDialog(null, err);
  825.                         }
  826.                         UpdateJTable();
  827.                     } catch (SQLException er) {
  828.                         System.out.println("Error: " + er);
  829.                     }
  830.  
  831.                 } else {
  832.  
  833.                 }
  834.             }
  835.         });
  836.  
  837.  
  838.  
  839.  
  840.     }
  841. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement