有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java结果集跳过列

我正在用Java开发一个软件,我正在做的是在完整的sqlite数据库(55列1000行)中搜索一个值并更新它

我可以在数据库中找到该值,但当我尝试更新它时,我希望跳过只有空值的列。使用ResultSet它会自动跳过具有非空值的其他列

如何检查ResultSet是否没有跳过列、查找所需列并更新该值

我尝试的是:

ArrayList<String> arr = new ArrayList<String>() ;
rs = statement.executeQuery(); // that is the select statement 
//Select * FROM table_name WHERE column1 like %VALUE%' OR column2 like %VALUE% and other columns like this too...
List<String> cols = Arrays.asList(getColLabels());
// I am getting Column Labels from ResultSetMetaData and it works fine  
while(rs.next()){
    for(String column: cols){
        String value;               
        if(rs.getBoolean(column)){      
    //That is the problematic area, if ResultSet finds a column with full of NULL values it skips to next column.
    // But other columns it skips too , which have not null Values and after that ResultSet does not find a proper column.

            if((value = rs.getObject(column).toString()).matches(".*"+searchedVal+".*") ){
        //searchedVal is searched value 
                arr.add("UPDATE Table_Name SET "+column+" ='"+newValue+"' WHERE "+ column+" ='" +value+"';"  );
                // then send array in another function to update to database
                break;
            }
        }
    }
}

谢谢你的帮助


共 (2) 个答案

  1. # 1 楼答案

    我找到了答案, rs.getBoolean不适用于不同类型的列类型,因此我更改了getObject函数。 工作守则如下:

    ArrayList<String> arr = new ArrayList<String>() ;
    rs = statement.executeQuery(); // that is the select statement 
    //Select * FROM table_name WHERE column1 like %VALUE%' OR column2 like %VALUE% and other columns like this too...
    List<String> cols = Arrays.asList(getColLabels());
    // I am getting Column Labels from ResultSetMetaData and it works fine  
    while(rs.next()){
        for(String column: cols){
            String value;               
            if(rs.getObject(column) != null){    
    
                if((value = rs.getObject(column).toString()).matches(".*"+searchedVal+".*") ){
            //searchedVal is searched value 
                    arr.add("UPDATE Table_Name SET "+column+" ='"+newValue+"' WHERE "+ column+" ='" +value+"';"  );
                    // then send array in another function to update to database
                    break;
                }
            }
        }
    }
    
  2. # 2 楼答案

    请尝试使用数据层(SQL命令)而不是业务trie(java代码)来解决problematic area

    您想拥有所有列都必须为非空的行吗?因此,只需向sql命令中添加更多条件子句(not null

    比如:

    Select * FROM table_name WHERE column1 like %VALUE%' OR column2 like %VALUE% and column_n like %VALUE% and (column1 not null and column2 not null and column_n not null)