% sign in Java’s PreparedStatement Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of % sign in Java’s PreparedStatement without wasting too much if your time.

The question is published on by Tutorial Guruji team.

PreparedStatement ps = con.createStatement("select * from table1 where last_name like ?");
ps.setString(1, "'%"+lastName+"'");

Will this work the same as…

Statement s = con.createStatement("select * from table1 where last_name like %"+ lastName);

Or does PreparedStatement strip out the % sign?

Answer

% is a wildcard character (in Oracle at least), so in theory both should work the same (assuming you add the missing single-quotes)

However, the first would be regarded as better practice, since it may enable the database optimser not to re-parse the statement. The first should also protect you against SQL injection whereas the second may not.

We are here to answer your question about % sign in Java’s PreparedStatement - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji