Lucas Yoshimura
Lucas Yoshimura

Reputation: 19

How can I keep the same value?

I don't understand why the variables AUX and resultado dont return the same value that they was... I want to keep the value to return them to another class. Can you help me please?

My class is:

public class CriarConexao extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {

      String response = "";
      try {

            Class.forName("com.mysql.jdbc.Driver");  
            System.out.println("driver conectado");
            Connection con = DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/savetime", "root", "root");
            resultado = "Database connection success\n";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from ingresso");
            //ResultSetMetaData rsmd = rs.getMetaData();

            while(rs.next()) {

                resultado = rs.getString(1);
            }

            AUX = resultado ;
            System.out.println("Auxiliar 1: " + AUX + " e " + resultado);
      }
      catch(Exception e) {
          e.printStackTrace();
          System.out.println("ERRO: " + e.toString());
      }
    return response;
    }

    @Override
    protected void onPostExecute(String resultado) {
        AUX = resultado ;
        System.out.println("Auxiliar 2: "+AUX + " e " + resultado);
    }
}

the logcat:

06-20 05:00:47.475: I/System.out(1592): Auxiliar 1: 1234 e 1234
06-20 05:00:47.475: I/System.out(1592): Auxiliar:  e 

Upvotes: 0

Views: 80

Answers (2)

Sal Aldana
Sal Aldana

Reputation: 1235

In your doInBackground method you are initializing a response string but you never assign either AUX or resultado to it so that's why in the postExecute resultado is empty (just because it has the same name doesn't make it the same variable). Since you are trying to return more than one String you need to adjust your asynctask to either return an array or map that contains the two values you want to return

Upvotes: 0

Giru Bhai
Giru Bhai

Reputation: 14398

Because your doInBackground function returning response variable value which is blank.So add AUX to response
try this

@Override
    protected String doInBackground(String... urls) {

      String response = "";
      try {

            Class.forName("com.mysql.jdbc.Driver");  
            System.out.println("driver conectado");
            Connection con = DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/savetime", "root", "root");
            resultado = "Database connection success\n";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from ingresso");
            //ResultSetMetaData rsmd = rs.getMetaData();

            while(rs.next()) {

                resultado = rs.getString(1);
            }

            AUX = resultado ;
            response = AUX;  // Add here
            System.out.println("Auxiliar 1: " + AUX + " e " + resultado);
      }
      catch(Exception e) {
          e.printStackTrace();
          System.out.println("ERRO: " + e.toString());
      }
    return response;
}

Upvotes: 1

Related Questions