FBSqlException in INSERT method with Returning

0

I'm trying to do an INSERT on my bank (FireBird) with Returning to return the registered ID, however, when it arrives at the stmt.executeUpdate() line I get the FBSqlException exception.

Insert method:

Note 1: I'm using netbeans.

public int insereBanco(BancoCTR bancoCTR) {
        try {
            conn = Conexao.obtemConexao();
            String insert = "INSERT INTO BANCO VALUES (null, ?) RETURNING "
                    + "BCO_CODIGO";

            stmt = conn.prepareStatement(insert);
            stmt.setString(1, bancoCTR.getBcoNome());
            int i = stmt.executeUpdate();


            if (i < 1){
                return -1;
            }else{
                rSet = stmt.executeQuery();
            }


            if (rSet.next()) {
                return rSet.getInt(1);
            } else {
                return -2;
            }

        } catch (Exception e) {
            System.out.println("Erro ao inserir banco: " + e.getMessage());
            return -3;
        }
    }
    
asked by anonymous 06.02.2018 / 12:31

1 answer

0

As the returning one that was causing the problem, I resolved with a gambiarra.

I'll leave it here, in case someone does not find an appropriate way to solve it either:

public int insereBanco(BancoCTR bancoCTR) {
        try {
            conn = Conexao.obtemConexao();
            String insert = "INSERT INTO BANCO VALUES (null, ?)";

            stmt = conn.prepareStatement(insert);
            stmt.setString(1, bancoCTR.getBcoNome());
            int i = stmt.executeUpdate();


            if (i < 1){
                return -1;
            }else{
                //Gambiarra ====================================================
                stmt = null;
                String codigo = "SELECT MAX(BCO_CODIGO) FROM BANCO";

                stmt = conn.prepareStatement(codigo);                
                rSet = stmt.executeQuery();
                //==============================================================
            }           

            if (rSet.next()) {
                return rSet.getInt(1);
            } else {
                return -2;
            }

        } catch (Exception e) {
            System.out.println("Erro ao inserir banco: " + e.getMessage());
            return -3;
        }finally{
            try{
            conn.close();
            }catch(Exception e){
                System.out.println("Erro ao fechar conexão: " + e.getMessage());
                return -4;
            }
        }
    }
    
06.02.2018 / 17:25