Java 结果集跳过列
我正在用Java开发一个软件,我正在做的是在完整的sqlite数据库(55列1000行)中搜索一个值并更新它 我可以在数据库中找到该值,但当我尝试更新它时,我希望跳过只有空值的列。使用Java 结果集跳过列,java,database,sqlite,Java,Database,Sqlite,我正在用Java开发一个软件,我正在做的是在完整的sqlite数据库(55列1000行)中搜索一个值并更新它 我可以在数据库中找到该值,但当我尝试更新它时,我希望跳过只有空值的列。使用ResultSet它会自动跳过具有非空值的其他列 如何检查ResultSet是否没有跳过列、查找所需列并更新该值 我尝试的是: ArrayList<String> arr = new ArrayList<String>() ; rs = statement.executeQuery(); /
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;
}
}
}
}
ArrayList arr=new ArrayList();
rs=语句。executeQuery();//这就是select语句
//从表_name中选择*,其中列1类似于%VALUE%'或列2类似于%VALUE%,其他列也类似于此。。。
List cols=Arrays.asList(getColLabels());
//我从ResultSetMetaData获取列标签,它工作正常
while(rs.next()){
for(字符串列:cols){
字符串值;
if(rs.getBoolean(列)){
//这就是问题所在,如果ResultSet发现一列中满是空值,它会跳到下一列。
//但它也会跳过其他列,这些列没有空值,并且在结果集之后找不到合适的列。
if((value=rs.getObject(column.toString()).matches(“.*”+searchedVal+“*”)){
//searchedVal是搜索的值
arr.add(“更新表名称集”+column+“=”“+newValue+”,其中“+column+”=”“+value+”;”);
//然后在另一个函数中发送数组以更新到数据库
打破
}
}
}
}
谢谢您的帮助。请尝试使用数据层(SQL命令)而不是业务trie(java代码)解决问题区域 您想拥有所有列都必须为非空的行吗?因此,只需向sql命令中添加更多条件子句(
notnull
)
比如:
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)
我找到了答案,
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;
}
}
}
}
ArrayList arr=new ArrayList();
rs=语句。executeQuery();//这就是select语句
//从表_name中选择*,其中列1类似于%VALUE%'或列2类似于%VALUE%,其他列也类似于此。。。
List cols=Arrays.asList(getColLabels());
//我从ResultSetMetaData获取列标签,它工作正常
while(rs.next()){
for(字符串列:cols){
字符串值;
如果(rs.getObject(列)!=null){
if((value=rs.getObject(column.toString()).matches(“.*”+searchedVal+“*”)){
//searchedVal是搜索的值
arr.add(“更新表名称集”+column+“=”“+newValue+”,其中“+column+”=”“+value+”;”);
//然后在另一个函数中发送数组以更新到数据库
打破
}
}
}
}