我写了下面的代码来检索表中的所有记录,但是当我创建一个新表时,我想检查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 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));
    }

10-07 19:07
查看更多