Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Resultset empty or not

Tags:

java

sqlite

jdbc

i wrote the below code to retrieve all the records from the table, but as i created a new table, i want to check if the ResultSet is empty or not to display a message if it is empty.

how can i do that

code:

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");
    }

}
like image 909
rmaik Avatar asked Jan 28 '26 22:01

rmaik


2 Answers

You can try this

if(resSet.next())
{
     do {
            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));
        }
        while (resSet.next())
}
else
{
      // Message : No Data present   
}
like image 130
Harshit Avatar answered Jan 31 '26 12:01

Harshit


Your could use a boolean variable :

    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));
    }
like image 25
Eran Avatar answered Jan 31 '26 10:01

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!