简体   繁体   English

如何在准备好的语句java中使用特殊字符

[英]how to use special character in prepared statement java

im not so frequent user of like clause, but now i have requirement i want fetch some files based on it name . 我不太喜欢like子句,但现在我有了要求,我想根据它的名称获取一些文件。 i tried with 我尝试过

"SELECT *  FROM FILES WHERE FILENAME LIKE "+"%"+"?"+"%";

next i tried with escaping the character 接下来我尝试转义角色

 "SELECT *  FROM FILES WHERE FILENAME LIKE "+"\\%"+"?"+"\\%";

both the ways when i create prepared statement and execute its giving error , 当我创建准备好的语句并执行其给定错误时,这两种方式

please help me how can i use % ? 请帮我,我怎么用%?

Thanks in advance 提前致谢

Just do this: 只要这样做:

"SELECT *  FROM FILES WHERE FILENAME LIKE ?";

Then pass the parameter through as 然后将参数作为

"%.jpg"

Or use whatever wildcards you desire. 或使用您想要的任何通配符。

Eg 例如

PreparedStatement st = connection.prepareStatement("SELECT *  FROM FILES WHERE FILENAME LIKE ?");
st.setString(1, "%.jpg");
ResultSet rs = st.executeQuery();

I agree with cowls answer, but I think you also want a percent sign after the search string, based on your initial post. 我同意整流罩的答案,但我认为您还希望根据原始帖子在搜索字符串后加上百分号。

So, something like: 因此,类似:

"%myfilename%"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM