mardi 5 janvier 2016

SQLITE FILTER QUERY SEARCH

I'm creating a program to put on my resume seeing as I am a college student with no work experience yet. For this particular part of the program I want to allow the user to search the sqlite database for an employee by either id number, first name or last name. It is working properly, however, it will only show the employee with the name that is spelled exactly and caps sensitive. I want it so that the user can type in a single letter or more and it will show everything in the database that contains that letter or couple letters and so on.

This is what I have:

        try {
        String field = (String)fieldCombo.getSelectedItem();//gets jcombobox selection
        //jcombobox fields are slightly different than the column names in sql table so i did this
        if(field.equals("First Name")) {
            newField = "firstName";
        }
        else if(field.equals("Last Name")) {
            newField = "lastName";
        }
        else if(field.equals("ID Num")) {
            newField = "idNum";
        }
        String sql = "select idNum as 'ID Number', firstName as 'First Name', lastName as 'Last Name' from tableEMPLOYEE where " + newField + " = ?";
        pst = connect.prepareStatement(sql);
        pst.setString(1, searchField.getText());
        rs = pst.executeQuery();
        empTable.setModel(DbUtils.resultSetToTableModel(rs));

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

Aucun commentaire:

Enregistrer un commentaire