[英]Searching oracle database using a “contains” query instead of an “exact match” query
I need to search my database which contains user information table. 我需要搜索包含用户信息表的数据库。 I know to get the result by searching with the query like the following
我知道通过搜索查询得到结果,如下所示
String query;
StringBuilder sb=new StringBuilder(1024);
sb.append("select * from userinfo where uname=").append(key);
query=sb.toString();
But what I need to do is. 但我需要做的是。
abc
abc
abc
abc
所有数据 abc
, aba
, gjabc
, abcxyz
, hjjabcxyz
abc
, aba
, gjabc
, abcxyz
, hjjabcxyz
abc
, gjabc
, abcxyz
, hjjabcxyz
abc
, gjabc
, abcxyz
, hjjabcxyz
abc
in common. abc
的共同点。 How can I achieve this? 我怎样才能做到这一点?
If you are using pure SQL you should use like surrounded with "%". 如果您使用的是纯SQL,则应使用“%”包围。 Your query will then return all rows where uname contains "abc".
然后,您的查询将返回uname包含“abc”的所有行。 Your query needs to look like this:
您的查询需要如下所示:
select * from userinfo where uname like '%key%'
Correct implementation would use a PreparedStatement. 正确的实现将使用PreparedStatement。
PreparedStatement ps = conn.prepareStatement("select * from userinfo where uname like ?");
ps.setString(1, key);
ResultSet rs = ps.executeQuery();
of course this misses all error checking and plumbing code :) 当然这错过了所有错误检查和管道代码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.