Как получить значение отпечатка пальца из Arduino в Java и сохранить его в MySQL - PullRequest
0 голосов
/ 13 июня 2019

В настоящее время я работаю в системе учета отпечатков пальцев.Я использую Java, Arduino и MySQL.Я столкнулся с проблемой того, как я должен получить значение отпечатка пальца из Arduino UNO в Java и затем сохранить его в MySQL.Ваша помощь будет высоко оценена.

public class NewUserPage extends javax.swing.JFrame {

    private PanamaHitek_Arduino ino = new PanamaHitek_Arduino();


        private SerialPortEventListener listener = new SerialPortEventListener() {
        @Override
        public synchronized void serialEvent(SerialPortEvent spe) {
            String a="";
            try {
                if (ino.isMessageAvailable()) {
                     a=a+ino.printMessage();
                     jTextArea1.append(a +"\n");
//                     System.out.println(ino.receiveData());

                     System.out.println(a);
                }
            } catch (SerialPortException | ArduinoException ex) {
               Logger.getLogger(NewUserPage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    };

    /**
     * Creates new form NewUserPage
     */
    public NewUserPage() {
        initComponents();
        FillComboBox();

         try {
            ino.arduinoRXTX("COM3",115200, listener);
           }
          catch (ArduinoException ex) {
            Logger.getLogger(NewUserPage.class.getName()).log(Level.SEVERE, null, ex);
           }
    }

    public Connection getConnection()
    {
        Connection con;

        try {
            con=DriverManager.getConnection("jdbc:mysql://localhost/userlogin", "root","");
            return con;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    public ArrayList<NewStudent>getStudentList()
        {
            ArrayList<NewStudent>studentList = new ArrayList<NewStudent>();
            Connection connection = getConnection();

            String query = "SELECT * FROM `newstudents`";
            Statement st;
            ResultSet rs;

            try{
                st = connection.createStatement();
                rs = st.executeQuery(query);

                NewStudent student;

                while(rs.next())
                {
                    student = new NewStudent(rs.getString("TPNumber"),rs.getString("FirstName"),rs.getString("LastName"),rs.getString("Username"),rs.getString("Password"),rs.getString("UserType"),rs.getString("Intake"),rs.getString("Major"),rs.getString("Level"), rs.getString("Email Address"),rs.getInt("Mobile Number"),rs.getString("Location"),rs.getString("Gender"),rs.getString("Fingerprint ID"));
                    studentList.add(student);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
        }
            return studentList;
    }

    //Display Data In JTable

    public void Show_Students_In_JTable()
    {
        ArrayList<NewStudent> list = getStudentList();
        DefaultTableModel model = (DefaultTableModel)jTableDisplayStudents.getModel();
        Object[] row = new Object[14];
        for(int i = 0; i<list.size(); i++)
        {
            row[0] = list.get(i).getTP();
            row[1] = list.get(i).getFirstName();
            row[2] = list.get(i).getLastName();
            row[3] = list.get(i).getUsername();
            row[4] = list.get(i).getPassword();
            row[5] = list.get(i).getUserType();
            row[6] = list.get(i).getIntake();
            row[7] = list.get(i).getMajor();
            row[8] = list.get(i).getLevel();
            row[9] = list.get(i).getEmailAddress();
            row[10] = list.get(i).getMobileNumber();
            row[11] = list.get(i).getLocation();
            row[12] = list.get(i).getGender();
            row[13] = list.get(i).getFID();

            model.addRow(row);
        }
    }


    //Execute the SQL Query
    public void executeSQLQuery(String query, String message)
    {
        Connection con = getConnection();
        Statement st;
        try{
            st = con.createStatement();
            if(st.executeUpdate(query) ==1)
            {
                //Refresh JTbale Data
                DefaultTableModel model= (DefaultTableModel)jTableDisplayStudents.getModel();
                model.setRowCount(0);
                Show_Students_In_JTable();





                JOptionPane.showMessageDialog(null, "Data "+message+" Successfully");

            }
            else{
                JOptionPane.showMessageDialog(null, "Data Is Not "+message);
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    private void FillComboBox()
     {
         Statement st;
         ResultSet rs;
         try
         {
             Connection connection = getConnection();

            String query = "SELECT * FROM `intake`";
            st = connection.createStatement();
            rs = st.executeQuery(query);

            while(rs.next())
            {
                jComboBoxIntake.addItem(rs.getString("Intake Code"));
            }


         }
         catch(Exception e)
         {
             JOptionPane.showMessageDialog(null, e);
         }
     }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */


    private void jTextFieldLastNameActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        // TODO add your handling code here:
    }                                                  

    private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        AdminMenu ad = new AdminMenu();
        ad.setVisible(true);
        this.setVisible(false);
    }                                           

    private void jTableDisplayStudentsMouseClicked(java.awt.event.MouseEvent evt) {                                                   
        //Display Selected Row In JTexttFields
        int i = jTableDisplayStudents.getSelectedRow();
        TableModel model = jTableDisplayStudents.getModel();
        jTextFieldTPNo.setText(model.getValueAt(i,0).toString());
        jTextFieldFirstName.setText(model.getValueAt(i,1).toString());
        jTextFieldLastName.setText(model.getValueAt(i,2).toString());
        jTextFieldUName.setText(model.getValueAt(i,3).toString());
        jPasswordFieldPW.setText(model.getValueAt(i,4).toString());
        jComboBoxUUType.setSelectedItem(model.getValueAt(i,5).toString());
        jComboBoxIntake.setSelectedItem(model.getValueAt(i,6).toString());
        jTextFieldMajor.setText(model.getValueAt(i,7).toString());
        jComboBoxLevel.setSelectedItem(model.getValueAt(i,8).toString());
        jTextFieldEmail.setText(model.getValueAt(i,9).toString());
        jTextFieldMobile.setText(model.getValueAt(i,10).toString());
        jTextFieldLocation.setText(model.getValueAt(i,11).toString());
        jComboBoxGender.setSelectedItem(model.getValueAt(i,12).toString());
    }                                                  

    private void jButtonAddStudentActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        Connection connection = getConnection();
        ResultSet rs;
        Statement ps;
        try {
            String query = "SELECT * FROM `newstudents`";
            ps = connection.createStatement();
            rs = ps.executeQuery(query);

            while(rs.next())
            {
                if(ino.toString().equals(rs.getString("Fingerprint ID")))
                {
                    JOptionPane.showMessageDialog(null, "Fingerprint found");
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Fingerprint not found");
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(NewUserPage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                                 

    private void jButtonUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                              

        String query = "UPDATE `newstudents` SET  `TPNumber`='"+jTextFieldTPNo.getText()+"',`FirstName`='"+jTextFieldFirstName.getText()+"',`LastName`='"+jTextFieldLastName.getText()+"',`Username`='"+jTextFieldUName.getText()+"',`Password`='"+jPasswordFieldPW.getText()+"',`UserType`='"+jComboBoxUUType.getSelectedItem()+"',`Intake`='"+jComboBoxIntake.getSelectedItem()+"',`Major`='"+jTextFieldMajor.getText()+"',`Level`='"+jComboBoxLevel.getSelectedItem()+"',`Email Address`='"+jTextFieldEmail.getText()+"',`Mobile Number`='"+jTextFieldMobile.getText()+"',`Location`='"+jTextFieldLocation.getText()+"',`Gender`='"+jComboBoxGender.getSelectedItem()+"',`Fingerprint ID`='"+ino+"' WHERE `TPNumber` = '"+jTextFieldTPNo.getText()+"'";
        executeSQLQuery(query, "Updated");
    }  

Значение, которое я получаю от Arduino и затем сохраняю в базе данных, - это (com.panamahitek.PanamaHitek_Arduino@568fbcdf) и когда я сравниваю значение отпечатка пальцасканируется столбец, в котором хранится значение отпечатка пальца, в котором говорится, что совпадений не найдено.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...