gpt4 book ai didi

java - 如何检查结果集是否为空

转载 作者:行者123 更新时间:2023-11-29 10:11:38 26 4
gpt4 key购买 nike

我写了下面的代码来检索表中的所有记录,但是当我创建一个新表时,我想检查 ResultSet 是否为空或者如果为空则不显示消息。

我该怎么做

代码:

public void selectAll() throws SQLException, ClassNotFoundException {

if (this.isTableExists(this.TABLE_NAME)) {

Connection conn = this.getConnection();
Statement stmt = conn.createStatement();

ResultSet resSet = stmt.executeQuery("select * from "+this.TABLE_NAME+";");//i want to check if it is empty or not.

while (resSet.next()) {
Log.d(TAG, "selectAll", "ID: "+resSet.getString(ID_COL));
Log.d(TAG, "selectAll", "Node: "+resSet.getString(NODE_ID_COL));
Log.d(TAG, "selectAll", "Lat: "+resSet.getString(LAT_COL));
Log.d(TAG, "selectAll", "Lng: "+resSet.getString(LNG_COL));
Log.d(TAG, "selectAll", "xmlPath: "+resSet.getString(XML_PATH_COL));
}

resSet.close();
stmt.close();
conn.close();
}

else{
Log.e(TAG, "selectAll", "table: ["+this.TABLE_NAME+"] does not exist");
}

}

最佳答案

您可以使用 boolean 变量:

    boolean isEmpty = true;
while (resSet.next()) {
isEmpty = false;
Log.d(TAG, "selectAll", "ID: "+resSet.getString(ID_COL));
Log.d(TAG, "selectAll", "Node: "+resSet.getString(NODE_ID_COL));
Log.d(TAG, "selectAll", "Lat: "+resSet.getString(LAT_COL));
Log.d(TAG, "selectAll", "Lng: "+resSet.getString(LNG_COL));
Log.d(TAG, "selectAll", "xmlPath: "+resSet.getString(XML_PATH_COL));
}

关于java - 如何检查结果集是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31940332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com