Eatthe Clock
Eatthe Clock

Reputation: 1

Search database with multiple keywords separated by a comma

I have been trying for several days to write a search function where you can enter multiple keywords to output all lines that have these words stored in the respective column. Searching for a single term is not a problem. Here is my code:

private void StartSearchTagsTextKeyPressed(java.awt.event.KeyEvent evt) {                                               
    if (evt.getKeyCode()==KeyEvent.VK_ENTER) {  
    try {
        String sql = "SELECT ID, Titel, Autor, Regal, Fach, Gelesen, Tags FROM TableDB WHERE UPPER(Tags) LIKE UPPER(?) ";  // hinter select kommt entweder ein * wenn alle spalteninhalte angezeigt werden sollen oder der jeweilige spaltenname welche angezeigt werden sollen
        pst = conn.prepareStatement(sql);
        pst.setString (1, "%" +StartSearchTagsText.getText()+ "%");

        rs= pst.executeQuery ();
        StartTable.setModel (DbUtils.resultSetToTableModel(rs));
        pst.close();
        StartSearchTagsText.setText("");            
    } 
    catch (SQLException e) {
        JOptionPane.showMessageDialog (null, "searchtagskey");
    }
    }
}             

I hope someone has a suggestion.

Upvotes: 0

Views: 505

Answers (2)

MrOnkelChiller
MrOnkelChiller

Reputation: 181

String searchterm1 = "Herrmann";
String searchterm2 = "Die kleine Hexe";
String sql = "Select * from TableDB where Autor like ? and Titel like ?";
pst = conn.prepareStatement(sql);
pst.setString (1, searchterm1);
pst.setString (2, searchterm2);

It should work like that.

Upvotes: 1

daZza
daZza

Reputation: 1689

This should be the simplest solution. If you can tell us what DBMS you are using we might be able to make it shorter depending on the system used.

First of all: LIKE only works with a single value for each evaluation it does. You can't separate it by commas like in the IN statement for example.

Therefore I'd suggest that you split the value of StartSearchTagsText.getText() into an array using the the .split("DELIMITER") function.

Then you can use a loop to add "OR LIKE %" + yourArray[i] + "%" to your sql String for every term you want to search for. (lose the OR for the first pass of the loop)


Du kannst mit LIKE nicht mehrere Bedingungen evaluieren, wie es z.B. mit IN geht.

Ich würde vorschlagen, dass du dir ein Array mit den gesplitteten Werten deines Textfeldes erzeugst. Das geht mit .split("TRENNUNGSZEICHEN"). Das Trennungszeichen musst du entsprechend anpassen. Wenn ein Komma die einzelnen Wörter nach denen du suchen willst separiert, dann schreibst du statt TRENNUNGSZEICHEN entsprechend ein , in die Klammer.

Sobald du das Array hast, kannst du es einfach komplett durchlaufen und bei jedem Durchlauf dein SQL-Statement mit "OR LIKE %" + deinArray[i] + "%"erweitern. Beim ersten Durchlauf das OR weglassen. Dann liefert dir das SELECT alle Datensätze zurück, was einem der Begriffe aus deinem Textfeld entspricht.

Upvotes: 0

Related Questions