vendredi 19 juin 2015

How to check table is empty with count(*)

I am trying to check if a table is empty. I code this method:

       public boolean checkIfNULL()
           throws SQLException, ClassNotFoundException {

        boolean flag=false;
        System.out.println("Checking if table is empty...");
        String sq = "select count(*) from TABLE1";        
        try {       
            Class.forName(typeDB);
            c = DriverManager.getConnection(path);            
            stm = c.prepareStatement(sq);
            PreparedStatement stm = c.prepareStatement(sq);

            int rowsAffected = stm.executeUpdate();

            if(rowsAffected == 0)
                flag=true;

        } catch (SQLException e) {
        System.out.println(e.getMessage());
    } finally {
        if (stm != null) {
            stm.close();
        }
            if (c != null) {
                    c.close();
        }
        }

        return flag;
   }

but sth wrong is hapenning and I get an error message Query returns results

Exceptionn: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)

How I check the returning value of check?

Aucun commentaire:

Enregistrer un commentaire