我有两个数据库表。第一个表包含'Destination'列,其中包含目标电话号码,第二个表包含'Areacode',其中包含areacodes。
假设:目的地号='001891191080' Areacode:001,0018,00185等
现在的问题是,目的地号码与001 Areacode匹配,而不是0018 ..
for (int i = 10; i >= 0; i--) {
try {
String ss = destination.substring(0, i);
Statement st = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/asteriskcdrdb", "root", "techsoft");
st = con.createStatement();
rs = st.executeQuery("Select * from sp1 where areacode like '" + ss + "'");
while (rs.next()) {
areacode = rs.getString("AreaCode");
在这里先生,根据我的需要,我从目标值开始并从最后一个开始检查for循环。但是一旦它匹配第一个值,它就跳过并且不检查其他和确切的可能性...... 如何匹配这么多可能性的确切值... 任何帮助将受到高度赞赏.. Thanx提前
答案 0 :(得分:1)
Destination number='001891191080' Areacode :001,0018
通过使用String#subString(begin index,End index)
,您可以制作短字符串并尝试匹配两者。
你不应该在for循环中创建连接和语句。
答案 1 :(得分:0)
首先根据字符串的长度(更多到更少)对'Areacode'进行排序,然后检查'目的地号码'中的匹配。
答案 2 :(得分:0)
首先从大字符串匹配开始,例如
String destNumber = "001891191080";
String[] areaCodes = new String[]{ "001","0018"};
if(destNumber.indexOf(areaCodes[1])>=0){ //<- use 0018 first
//....
}else if(destNumber.indexOf(areaCodes[0])>=0){
//...
}
答案 3 :(得分:0)
您可以尝试使用此查询吗?
"Select * from sp1 where areacode like '" + ss + "%' order by areacode desc"
那应该给你
00185
0018
001
在结果集中,比较会返回列表中的第一个值。