1

I have a java class that is supposed to display a table. So i want to search FROM table called Film the filmtitle and the film genre but the OR query doesn't work? any idea how to fix this?

heres my code btw

public void searchbyTitle()
{
    String sql = "SELECT filmtitle, filmyear, filmgenre, companyname FROM production, film, 
company  WHERE LOWER(filmtitle) like LOWER
 (?) OR LOWER(filmgenre) like LOWER (?) and production.companyid = company.companyid AND production.filmid = film.filmid";
    con = DBConnect.getConnection();

    try
    {
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setString(1, "%" +txtSearch.getText()+"%");
        ResultSet rs = pst.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(rs));

    }catch(SQLException e)
    {
        e.printStackTrace();
    }


}

it only works if i remove the or,but i also want to be able to search for film genre LOWER is also what i used so that it will be case insensitive also used postgresql..any help is appreciated

1 Answer 1

1

The logic is doing exactly what you are telling MySQL to do. It is just not what you intend.

If you are having trouble with conditional expressions, then use parentheses to explicitly do what you want:

WHERE (LOWER(filmtitle) like LOWER (?) OR
       LOWER(filmgenre) like LOWER (?)
      ) and
      production.companyid = company.companyid AND production.filmid = film.filmid;

Ouch! I notice that you have commas in the FROM clause. If you had used explicit JOIN syntax, your query would have been correct to begin with. The correct way to write the query is:

SELECT filmtitle, filmyear, filmgenre, companyname
FROM production p JOIN
     film f
     ON p.filmid = f.filmid JOIN
     company c
     ON p.companyid = c.companyid
WHERE LOWER(filmtitle) like LOWER(?) OR LOWER(filmgenre) like LOWER(?);

Notice the parentheses are no longer needed in the WHERE, because there is no AND competing with the OR.

Simple rule: Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. It would have saved you a lot of time today.

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, so many people confuse the precedence of OR vs AND, so rule of thumb: Always use parenthesis when mixing OR and AND, even if you understand the precedence rules. --- Even though I just used "always", saying "never use comma" is a bit extreme, but a good rule of thumb. ;-)
thanks for answering but an error still pops if it helps...it looks like this No value specified for parameter 1. at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:216) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:244) at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302) at film.FilmIntro.searchbyTitle(FilmIntro.java:111) at film.FilmIntro$4.keyReleased(FilmIntro.java:196)
In your request you are using two ? so you have to define two parameters in your java code : pst.setString(...
That can be same value : pst.setString(1, "%" +txtSearch.getText()+"%"); pst.setString(2, "%" +txtSearch.getText()+"%");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.