Home Tutorial Java Core JDBC: LIKE Clause Example


JDBC: LIKE Clause Example
Posted on: September 26, 2012 at 12:00 AM
In this section, you will learn how to use LIKE Clause for selection of table records using JDBC API.

JDBC: LIKE Clause Example

In this section, you will learn how to use LIKE Clause for selection of table records using JDBC API.

LIKE Clause :

Like Clause is used for comparing part of string. For pattern matching we use % and _ wild card characters. MySql LIKE operator provides a easy way of comparing strings.

Before writing query, first create connection to the database. Here is some important terms used for any database application -

Connection: This  interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.

Class.forName(String driver): It loads the driver.

DriverManager  :  The DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property

getConnection(String url+dbName, String userName, String password): This method establishes a connection to specified database url.

he MySQL connection URL has the following format:

  • host :The host name where MySQL server is running. Default is - the IP address of localhost.
  • port : The port number where MySQL is listening for connection. Default is 3306.
  • dbName : The name of an existing database on MySQL server. If not specified, the connection starts no current database.
  • Property : The name of a supported connection properties. "userName" and "password" are 2 most important properties.

Statement: This interface executes the SQL statement and returns the result it produces.

createStatement(): It is a method of Connection interface which returns Statement object.

executeQuery(String sql): Executes the given SQL statement, which returns a single ResultSet object.

Example : In this example we are using LIKE clause to select records of such student whose name starts with 'R' character.

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

class LikeClause{
	public static void main(String[] args){
		System.out.println("Use of LIKE Clause in JDBC...");
		Connection con = null;
		Statement statement = null;
		ResultSet rs = null;
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "students";
		String driverName = "com.mysql.jdbc.Driver";
		String userName = "root";
		String password = "root";
		try {

			// Connecting to the database
			con = DriverManager.getConnection(url + dbName, userName, password);
			try {
				statement = con.createStatement();

				// By using LIKE Clause Selecting records 
				String sql = "SELECT * FROM student WHERE name LIKE 'r%'";
				rs = statement.executeQuery(sql);
				while (rs.next()) {
					int roll = rs.getInt("roll_no");
					String name = rs.getString("name");
					String course = rs.getString("course");
					System.out.println(roll + "\t" + name + "\t" + course);


			} catch (SQLException e) {
		} catch (Exception e) {

Output :

Use of LIKE Clause in JDBC...
RollNo	Name	Course
1	Ron	MCA
3	Rose	MCA


Related Tags for JDBC: LIKE Clause Example:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 26, 2012

Recommend the tutorial

Advertisements Advertisements