String s="你好";
ResultSet rs=operate.executeQuery("select * from table1 where keyword='"+s+"'");//在table1中找
if(rs.next()){
//在table1中找到了
System.out.println("找到了");
}else{
//如果在table1中没找到,在table2中找
rs=operate.executeQuery("select * from table2 where keyword='"+s+"'");
}else{
//如果在table2中没找到,在table3中找
rs=operate.executeQuery("select * from table3 where keyword ='"+s+"'");
}else{
//如果在table1和table2中都找不到
System.out.println("都找不到");
}
请问这个怎么改,不要说把查询语句拿出来,那面只是我的简化代码,查询语句是不能拿出来的
Java if else
------解决方案--------------------
主要代码:
String s="你好";
String[] tableNames = String[]{"table1","table2","table3"};
//主调用方法
public void selectData(){
for(String tableName : tableNames){
ResultSet rs = getMyResult(tableName,s);
if(rs != null)
break;
}
if(rs != null)
Sysotem.out.println("查询导数据....");
else
Sysotem.out.println("没有查询到数据...");
}
//查询方法
private ResultSet getMyResult(String tableName,param s){
ResultSet rs=operate.executeQuery("select * from "+tableName+" where keyword='"+s+"'");
if(rs.hashNext()){
return rs;
}else{
return null;
}
------解决方案--------------------
String s="你好";
String zd="没找到"
ResultSet rs=operate.executeQuery("select * from table1 where keyword='"+s+"'");
if(rs.next()){
zd="在table1中找到";
}
rs=operate.executeQuery("select * from table2 where keyword='"+s+"'");
if(rs.next()){
zd="在table2中找到";
}
rs=operate.executeQuery("select * from table3 where keyword='"+s+"'");
if(rs.next()){
zd="在table3中找到";
}
System.out.println(zd);
}