Hi, I am creating a simple Symptom Checker application. The problem I have is that I'm trying to retrieve user input (JTextField) by comma's using StringTokenizer which contacts the database for a result which matches the user's input (SELECT * FROM DIAGNOSIS WHERE ?, ?, ?) . It successfully finds the correct result however only in a particular format. Not different combinations....
for example, if I enter say within the JTextField: "tearful, nausea, lack of motivation" it will find the result successfully (as that is how it is formatted within the particular column (in the database table) i wish to display a result from) however, if i enter a different combination of these symptoms: "nausea, lack of motivation, tearful" - it will not find any result. I'm very unsure how to make it work regardless of what is inputted first, second or last.
HERE IS THE CODE:
public void actionPerformed(ActionEvent e) { try { String abc = fieldsymp1.getText(); StringTokenizer str = new StringTokenizer(abc); while (str.hasMoreTokens()) { str.nextToken((", ")).trim(); } Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/intelli_db", "root", "root"); PreparedStatement st1 = con.prepareStatement("SELECT * FROM DIAGNOSIS WHERE SYMPTOM_1 LIKE '%" + abc + "%' OR '%" + abc + "%' OR '%" + abc + "%' OR '%" + abc + "%'"); ResultSet rs = st1.executeQuery(); if (rs.next()) { String s = rs.getString(1); String s1 = rs.getString(2); String s2 = rs.getString(3); String s3 = rs.getString(4); String s4 = rs.getString(5); String s5 = rs.getString(6); String s6 = rs.getString(7); //Sets Records in TextFields. field4.setText(s); field5.setText(s1); field6.setText(s2); field7.setText(s3); field8.setText(s4); field9.setText(s5); field10.setText(s6); } else { JOptionPane.showMessageDialog(null, "No such input found"); } //Create Exception Handler } catch (Exception ex) { System.out.println(ex); } }
Any help? Thank you.