我正在使用Java开发软件,我正在做的是在完整的sqlite数据库(55列和1000行)中搜索值并更新它。
我可以在数据库中找到该值,但是当我尝试更新它时,我想跳过只有NULL值的列。使用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;
}
}
}
}
感谢您的帮助。
答案 0 :(得分:0)
请尝试使用数据层(SQL命令)解决problematic area
,而不是业务线索(java代码)。
您想拥有所有列必须为非空的行吗?所以只需在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)
答案 1 :(得分:0)
我找到了答案, 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;
}
}
}
}