krystal
krystal

Reputation: 27

Searching a word from a database in my Java app

I have a quizzes table where I store the name of the test and the question, and I want the user(teacher) to enter a word in a Jtextfield, and the program to show all the questions which contain the given word. I tried using the LIKE operator from SQL, it produces no error but also doesn't show any output.

ResultSet rs = stmt.executeQuery("SELECT * FROM quizzes WHERE testName =\"" + 
testName + "\" AND question LIKE '%\""+ word + "\"%';");

Have I written something wrong?

Upvotes: 0

Views: 478

Answers (2)

The Impaler
The Impaler

Reputation: 48770

Fix the following things:

  • Try single quotes (not double quotes) in varchar equality.
  • Remove the double quotes in the LIKE operator.
  • Remove the semi colon at the end.

It should look like:

ResultSet rs = stmt.executeQuery("SELECT * FROM quizzes WHERE testName ='" + 
  testName + "' AND question LIKE '%"+ word + "%'");

Upvotes: 2

Amila
Amila

Reputation: 5213

Try using % inside quotations.

Upvotes: 0

Related Questions