Check Connection with the bank and validate for the user

2

I would like to know the following: I am trying to make an application that makes several connections to the database. I wanted to always check the connections and if the connection is not available alert the user and close the running method ..

For example: When using Connection = DriverManager.getConnection() and testing the connection, if it was successful, it continues the method, if not, to execute and sends a user alert!

Any ideas?

    
asked by anonymous 30.08.2017 / 14:31

1 answer

0

By taking a look at the DriverManager documentation, we can see that if something goes wrong while running getConnection (), an exception of type SQLException will be thrown.

Then try the following:

try {
    Connection connection = DriverManager.getConnection();
    // continua a execução

} catch(SQLException e){
    // algo deu errado, mostrar o erro no Log de execução
    e.printStackTrace();
    // aqui você pode tratar a exceção e dizer ao usuário que algo deu errado
}

I recommend you take a look here too: Android DriverManager Documentation

A possible implementation with AsyncTask is as follows:

public class ConnectActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        connect();
    }

    private void connect(){
        new HandleConnection(this).execute();
    }

    public void getConnectionResponse(String response){
        if(response.equals("SUCCESS")){
            // prosseguir com o procedimento depois da conexão
            actionAfterConnection();
        } else {
            // exibir alerta de erro na conexão
            showAlertConnectionFailed();
        }
    }

    private void actionAfterConnection(){

    }

    private void showAlertConnectionFailed(){

    }
}

Above we have the Activity that will call the AsyncTask HandleConnection through the connect () method.

Now, the HandleConnection class:

public class HandleConnection extends AsyncTask<Void, Void, String> {
    ProgressDialog progressDialog;
    ConnectActivity connectActivity;

    public HandleConnection(ConnectActivity connectActivity){
        progressDialog = new ProgressDialog(connectActivity);
        progressDialog.setTitle("Carregando...");
        progressDialog.setCancelable(false);
        progressDialog.show();

        this.connectActivity = connectActivity;
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            Connection connection = DriverManager.getConnection("suaconexao");

            return "SUCCESS";
        } catch(SQLException e){
            e.printStackTrace();

            return "FAIL";
        }
    }

    protected void onPostExecute(String connectionResponse){
        super.onPostExecute(connectionResponse);
        try {
            progressDialog.dismiss();

            connectActivity.getConnectionResponse(connectionResponse);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

In the constructor of the HandleConnection class we request an instance of the Activity ConnectionActivity, which is the class that calls AsyncTask. Since we have the instance of an Activity, we can use its context to create a ProgressDialog, which is the window that indicates that an action is being executed and the user should wait.

In AsyncTask's InBackground method, we verify that the connection was successful or something went wrong. If something goes wrong, the "catch" block will be called and the method will return "FAIL". Otherwise, it will return "SUCCESS".

In the onPostExecute method, we get the return from doInBackground and pass the value to the Activity ConnectionActivity through the getConnectionResponse () method. Thus, the result of the operation will be visible in Activity. Now that you have the connection information, you can alert the user if the connection fails. Just create your AlertDialog in the showAlertConnectionFailed () method of Activity.

    
30.08.2017 / 16:06