如何在Java中两次使用同一结果集
我想使用两次相同的结果集,一次是检查它是否为空,并根据结果执行代码,然后使用如何在Java中两次使用同一结果集,java,sql,Java,Sql,我想使用两次相同的结果集,一次是检查它是否为空,并根据结果执行代码,然后使用(resultset.next()第二个结果集将由于第一个结果集而始终跳过一行 以下是我目前的代码: connection = DriverManager.getConnection(connection); statement = (Statement) connection.createStatement(); resultset = statement.executeQuery(query); String id
(resultset.next()
第二个结果集将由于第一个结果集而始终跳过一行
以下是我目前的代码:
connection = DriverManager.getConnection(connection);
statement = (Statement) connection.createStatement();
resultset = statement.executeQuery(query);
String id = "0";
String idfromdb;
if(resultset.next() == false){
System.out.println("table is empty")
statement.executeUpdate(" INSERT INTO table VALUES ('value') ");
}
else{
while(resultset.next()){
idfromdb = resultset.getString("value")
if(! idfromdb = id){
System.out.println("no similar data has been found");
}
else{
System.out.println("similar data has been found");
}
}
}
当您调用
resultset.next()
时,它将获取一条记录,因此您需要调用重置光标
完整代码:
boolean hasRecord = true;
if(!resultset.next()){// you are missing ) here
System.out.println("table is empty")
statement.executeUpdate(" INSERT INTO table VALUES ('value') ");
hasRecord = false;
}
if(hasRecord){
resultset.previous();//move to the previous record
}
当您调用
resultset.next()
时,它将获取一条记录,因此您需要调用重置光标
完整代码:
boolean hasRecord = true;
if(!resultset.next()){// you are missing ) here
System.out.println("table is empty")
statement.executeUpdate(" INSERT INTO table VALUES ('value') ");
hasRecord = false;
}
if(hasRecord){
resultset.previous();//move to the previous record
}
更改为循环:
do while在循环的底部而不是顶部计算其表达式
更改为循环:
do while在循环的底部而不是顶部计算其表达式
@KevinGuswanto这是由于您的代码本身,您缺少
)
,请检查我的更新答案否,自动完成代码没有给我。hasNext()option@KevinGuswanto这是由于您的代码本身,您缺少)
,请检查我的更新答案否,自动完成代码没有给我。hasNext()选项但是我应该把我的(如果是空的)代码放在哪里?过一段时间后?@Kevingus想保留它,然后在其他声明中进行更改,好的。那么if(不等于),else if(等于),else但是我应该把我的(如果为空)代码放在哪里呢?过一段时间后?@Kevingus想保留它,然后在其他声明中进行更改,好的。所以如果(不相等),否则如果(相等),否则