(JAVA) Error returning record amount using SQL COUNT

Asked

Viewed 106 times

0

I’m trying to return the amount of record of a table, in this table I currently have 6 records but my Query returns only one, as if there were only one record. I need you to return the full amount, 6 records.

Query I used in SQL and worked

SELECT COUNT ([num_item])
FROM tb_temp_prod;

Query in JAVA

    Connection conn = null;
    Statement stmt = null;

        Class.forName(JDBC_DRIVER).newInstance();
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        stmt = conn.createStatement();

        ResultSet result = stmt.executeQuery("SELECT COUNT(*) AS num_item FROM tb_temp_prod");

            while(result.next()){
             result.getInt("num_item"); 
             //result.getInt(1); Já tentei assim também e não deu certo.

            }

        System.out.println("Total: " + result);

Result: Total: Sqlserverresultset:1

1 answer

1


**

Solved:

**

ResultSet result = stmt.executeQuery("SELECT COUNT(*) AS num_item FROM tb_temp_prod");

            while(result.next()){

             int teste = result.getInt("num_item");
             System.out.println("Total: " + teste);

            }
  • 2

    The treatment result.getInt(1); is unnecessary.

  • Perfect, thank you. I tested it here and it worked without. TKS @Augustovasques

Browser other questions tagged

You are not signed in. Login or sign up in order to post.