Share on Google+Share on Google+

JDBC: LIKE Clause Example

Advertisement
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:
jdbc:mysql://[host][:port]/[database][?property1][=value1]...

  • host :The host name where MySQL server is running. Default is 127.0.0.1 - 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 {
			Class.forName(driverName);

			// 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);
				System.out.println("RollNo\tName\tCourse");
				System.out.println("----------------------");
				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) {
				System.out.println(e);
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output :

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

Advertisements

Advertisement

Posted on: September 26, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.