Resultset of a query in java

0

Hi, I have a project in which in a java class, I did the query to the bd of oracle, I did it of type ResultSet, so that my servlet that works as a controller, could discriminate the result and see where it redirects it , the query only brings a data 0 or 1, the problem is that I do not know how to review the result of the query, the idea is to pass it to a variable and compare with an if the variable that passes the result (n) comes out in red saying as CAN NOT FIND SYMBOL error, I attach the code.

 ResultSet rs =consult.getConsultCambioBtnCollectRv(Parametro);
           try {
              if(rs != null){
               while(rs.next()){
                int  n = Integer.parseInt(rs.getString("collection_required_bool"));
                if(n == 0){
                      response.sendRedirect("test.jsp");
                      processRequest(request, response);
                  }else{
                       response.sendRedirect("test1.jsp");
                      processRequest(request, response);
                  }
                }
              }
             } catch (SQLException ex) {
              Logger.getLogger(SvrModificaCollectRv.class.getName()).log(Level.SEVERE, null, ex);
          }
    
asked by elsa 24.05.2016 в 16:02
source

3 answers

1

The first thing I see is that your code does not make much sense. It seems that the SQL statement that you execute should only bring a result, but you apply a while to read it. If this is not the case, then you should use a if :

if (rs.next()) {
    //...
}

You should not parse the string that you get from the database, leave that task to the driver:

//int  n = Integer.parseInt(rs.getString("collection_required_bool"));
int n = rs.getInt("collection_required_bool");

I see that you execute the processRequest(request, response) method after indicating the redirect. Once you call the redirect method, you should not call any additional method to write additional information to the response ( HttpServletResponse response ) since the response will be a redirect.

response.sendRedirect("test.jsp");
//processRequest(request, response);

You are not closing the ResultSet appropriately. It would be best if you use the try with resources, or if you use Java 6 or earlier as you close the object manually.

Under these recommendations, the final code would be as follows:

try (ResultSet rs = consult.getConsultCambioBtnCollectRv(Parametro)) {
    if (rs != null && rs.next()) {
        int  n = rs.getInt("collection_required_bool"));
        if (n == 0) {
            response.sendRedirect("test.jsp");
        } else {
            response.sendRedirect("test1.jsp");
        }
    }
} catch (SQLException ex) {
    Logger.getLogger(SvrModificaCollectRv.class.getName()).log(Level.SEVERE, null, ex);
}

However, we see that there is a problem when results are not obtained from ResultSet , it does not indicate what should be done. The best thing would be to apply a small refactoring and have as a variable the page to which it should be redirected by default. Here is an example:

String paginaRedirect = "test1.jsp";
try (ResultSet rs = consult.getConsultCambioBtnCollectRv(Parametro)) {
    if (rs != null && rs.next()) {
        int  n = rs.getInt("collection_required_bool"));
        if (n == 0) {
            paginaRedirect = "test.jsp";
        }
    }
} catch (SQLException ex) {
    Logger.getLogger(SvrModificaCollectRv.class.getName()).log(Level.SEVERE, null, ex);
}
//de esta manera siempre se hará un redirect
response.sendRedirect(paginaRedirect);
    
answered by 24.05.2016 в 22:17
0

n will not be visible in any other scope than within the while since that is where you are stating, so that you can see it in the scope of if you must declare n (for example ) along with ResultSet .

Initially, your variable rs is initialized with value null , so I assume that in your method consult.getConsultCambioBtnCollectRv(Parametro) you will find something like the following

public ResultSet getConsultCambioBtnCollectRv(...) {
    Statement statement = connection.getConnection().createStatement();
    resultSet = statement.executeQuery(sqlString);
    return resultSet;
}

So it would be convenient to validate that the resultSet does not have a value null , so we know that it really has something we can try to access.

int n = -1; //valor inicial que desees 

try {
    ResultSet rs = null;
    rs = consult.getConsultCambioBtnCollectRv(Parametro);

    if(rs != null) {
        while(rs.next()){
            n = rs.getInt(1); //obtengo n (siempre y cuando la posicion (1) sea un entero desde la BDD
        }

        if(n == 0) { 
        // my logica aqui 
        }

    } else {
        //Imprimir mensaje, no se pudo obtener datos 
    }
} catch (SQLException ex) {
    Logger.getLogger(SvrModificaCollectRv.class.getName()).log(Level.SEVERE, null, ex);
}

I also recommend you to continue working within the try/catch in case something goes wrong, the application does not stop its execution abruptly.

    
answered by 24.05.2016 в 18:00
0

You could do something like this:

int  n = 0;
ResultSet rs =consult.getConsultCambioBtnCollectRv(Parametro);
while (rs.next()){
      n = Integer.parseInt(rs.getString("collection_required_bool"));
    }

if(n == 0){
      response.sendRedirect("test.jsp");
      processRequest(request, response);
}else{
      response.sendRedirect("test1.jsp");
      processRequest(request, response);
}
    
answered by 26.05.2016 в 17:36