• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Search Engine Optimization and Internet Marketing
  • Earning with Website and Blogging
  • Software and Hardware
  • New and Happening
  • Other
  • Sponsored Post
You are here: Home / Programming and Scripting / Java: How To Use SQL LIKE Clasue with PreparedStatement?

Java: How To Use SQL LIKE Clasue with PreparedStatement?

April 13, 2013 by Sze Hau Leave a Comment

One may find that using SQL’s LIKE clause in Java PreparedStatement is not straight forward. There is no documentation on how the LIKE clause should be used when it is used with the PreparedStatement object. Thus, most of the programmer would rather use the full SQL to execute the statement.

A programmer may also try the following SQL and put it through the PreparedStatement and get a rejection complain while preparing the SQL statement.

SELECT * FROM USER WHERE NAME LIKE '%?%'

 

The above statement is incorrect. You cannot put the question mark inside the quotes. You must treat the question mark as the whole value as the input. The correct way to define the SQL is to put the question mark without the quotes. For example:

SELECT * FROM USER WHERE NAME LIKE ?

 

When you need to query a specific patter using LIKE clause, the “pattern” option should be set outside of the SQL itself. For example:

String query = "SELECT * FROM USER WHERE NAME LIKE ?"
PreparedStatement stmt = con.prepareStatement(query);
String value = "John";
stmt.setString(1, "%"+value+"%");
ResultSet rst = stmt.executeQuery();

More from my site

  • SQL: How To Select Data With Same Values On ColumnsSQL: How To Select Data With Same Values On Columns
  • Database: CHAR and VARCHAR – What are the differences?Database: CHAR and VARCHAR – What are the differences?
  • Java: How To Select Top N Objects From A ListJava: How To Select Top N Objects From A List
  • Java: Continuously Read Data From FileChannel Without MappedByteBufferJava: Continuously Read Data From FileChannel Without MappedByteBuffer
  • Java: Unlimited Strength Jurisdiction PolicyJava: Unlimited Strength Jurisdiction Policy
  • Java: Static Initializer (Static Constructor)Java: Static Initializer (Static Constructor)

Filed Under: Programming and Scripting Tagged With: Java, PreparedStatement, SQL

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Search Engine Optimization

Short URL: Top 5 Websites That Provide Free Short URL Service

December 10, 2009 By Sze Hau Leave a Comment

Google Chromium OS

November 23, 2009 By Sze Hau Leave a Comment

WordPress Plugin: Official StatCounter Plugin

November 13, 2009 By Sze Hau 1 Comment

Make Money With Website And Blogging

Wordpress

WordPress: How To Change Admin Username

September 23, 2015 By Sze Hau Leave a Comment

WordPress: Transfer Your Blogs To Another Hosting With Minimum Downtime

February 3, 2014 By Sze Hau Leave a Comment

Virtualmin: How To Install Virtualmin in CentOS

August 26, 2013 By Sze Hau Leave a Comment

Software and Hardware

iOS 5.1 Unable to Update

March 13, 2012 By Sze Hau Leave a Comment

Blog Network

  • All Gadget Latest Gadget Reviews and News
  • Blog Portal A place where bloggers gather
  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments
  • Vista Talks Software News, Software Updates, Tips And Tricks

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2021 · Magazine Pro Theme on Genesis Framework · WordPress · Log in