1

So i have a problem where i want to see how many rows there are in my database, after i've made an update. Im trying to use "GetMaxRows" but it always returns 0. i suspect that it is because i dont have a ResaultSet, but i can't get that when using PreparedStatement.executeUpdate()? Any suggestions or mistakes i've made?

protected void createOrder(Carport carport, Shed shed, User user){

    Logger.getLogger("web").log(Level.INFO, "");

    String sql = "insert into carport.order (length, width, material, userId, shedIncluded) values(?,?,?,?,?)";

    int shedIncluded = 1;
    if (shed == null) shedIncluded = 0;

    int maxRows = 0;

    try (Connection connection = connectionPool.getConnection()) {
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, carport.getLength());
            ps.setInt(2, carport.getWidth());
            ps.setString(3,carport.getMaterial());
            ps.setInt(4,user.getUserId());
            ps.setInt(5,shedIncluded);
            int rowsAffected = ps.executeUpdate();
            int test = ps.getMaxRows();
            System.out.println(test);

            if(rowsAffected == 1){
                System.out.println("Carport data is saved");
            }
            else{
                throw new DatabaseException("Could not save Carport data");
            }
        }
    } catch (SQLException | DatabaseException throwables) {
        throwables.printStackTrace();
    }
    if(shedIncluded == 1) createShedOrder(shed, maxRows);
}
Chunti
  • 11
  • 1
  • 0 means that there is no upper limit set on the max rows **the ResultSet can contain**. You can set a limit with ``setMaxRows(int limit)`` – Schokokuchen Bäcker May 06 '22 at 12:21
  • The `getMaxRows` method simply does not do what you think it does. From its documentation: "Retrieves the maximum number of rows that a ResultSet object produced by this Statement object can contain. If this limit is exceeded, the excess rows are silently dropped. Returns: the current maximum number of rows for a ResultSet object produced by this Statement object; zero means there is no limit" - If you want to check how many rows already exist in your Database you need to do a query, like a simple `count (*) from carport.order` sql query or something similar. – OH GOD SPIDERS May 06 '22 at 12:22
  • Okay, i see. Then im using the wrong approach. How can i check what row the update is in my database? It should always be in the last spot and get an auto incremented ID. – Chunti May 06 '22 at 12:24
  • Okay. So I have to go and do a query. hoped i could get around that. Thanks alot guys! – Chunti May 06 '22 at 12:26
  • "How can i check what row the update is in my database?" – OH GOD SPIDERS May 06 '22 at 12:58
  • If you want to get the generated ID(s) after an insert: [If it is an auto generated key, then you can use Statement#getGeneratedKeys() for this.](https://stackoverflow.com/a/1915197/6073886) – OH GOD SPIDERS May 06 '22 at 13:45
  • THAT WORKED PERFECTLY! Exactly what i was after. Thanks alot for your help. :D – Chunti May 06 '22 at 13:53

0 Answers0