简体   繁体   English

使用“包含”查询而不是“完全匹配”查询搜索oracle数据库

[英]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. 但我需要做的是。

  • If the key is abc 如果密钥是abc
  • Then I need to search for all data In the database which having the value abc 然后我需要在数据库中搜索具有值abc所有数据
  • For example 例如
  • My database contains the data abc , aba , gjabc , abcxyz , hjjabcxyz 我的数据库包含数据abcabagjabcabcxyzhjjabcxyz
  • Then I need to retrieve the values abc , gjabc , abcxyz , hjjabcxyz 然后我需要检索值abcgjabcabcxyzhjjabcxyz
  • Because all above values contains 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.

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