
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to use quotes correctly while using LIKE with search variable in MySQL in Java?
Following is the correct syntax to use LIKE with search variable −
String sqlQuery; sqlQuery = "select *from yourTableName where yourColumnName like '%" +yourSearchVariableName + "%'";
Let us create a table −
mysql> create table demo19 −> ( −> id int not null auto_increment primary key, −> name varchar(50) −> ); Query OK, 0 rows affected (3.48 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo19(name) values('John Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into demo19(name) values('David Miller'); Query OK, 1 row affected (0.15 sec) mysql> insert into demo19(name) values('Adam Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo19(name) values('Chris Brown'); Query OK, 1 row affected (0.13 sec)
Display records from the table using select statement −
mysql> select *from demo19
This will produce the following output −
+----+--------------+ | id | name | +----+--------------+ | 1 | John Smith | | 2 | David Miller | | 3 | Adam Smith | | 4 | Chris Brown | +----+--------------+ 4 rows in set (0.00 sec)
Example
Following is the Java code to use quotes correctly while using LIKE with search variable −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class RemoveJavaQueryError { public static void main(String[] args) { Connection con = null; Statement statement = null; try { String searchKeyWord = "Smith"; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledatabase", "root", "123456"); statement = (Statement) con.createStatement(); String sqlQuery; sqlQuery = "select *from demo19 where name like '%" + searchKeyWord + "%'"; ResultSet rs = statement.executeQuery(sqlQuery); while (rs.next()) { System.out.println(rs.getString("name")); } } catch (Exception e) { e.printStackTrace(); } } }
This will produce the following output −
John Smith Adam Smith
Following is the snapshot of sample output.
- Related Articles
- How to correctly use WITH ROLLUP in MySQL?
- How to use MySQL LIKE query to search a column value with % in it?
- How to use formatting with printf() correctly in Java?
- How to use % wildcard correctly in MySQL?
- Search record with a specific value in MySQL using LIKE or REGEXP?
- How to print double quotes with the string variable in Python?
- How to correctly use DELIMITER in a MySQL stored procedure?
- How to insert records with double quotes in MySQL?
- How to use ‘while loop’ in Java?
- How to include quotes in comma separated column with MySQL?
- Using MySQL keywords in a query surrounded with single quotes?
- How to use MySQL CASE statement while using UPDATE Query?
- Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java
- How to use user variables in MySQL LIKE clause?
- How to use ‘do while loop’ in Java?

Advertisements