java – Error while trying to create a database connection (MySQL)

Question:

I tried to make a connection to the MySQL database using AsyncTask but is giving error in the class responsible for the connection, the MysqlConnect :

The constructor Log() is not visible

I'm following this tutorial and my problem is in the following MysqlConnect snippet:

public MysqlConnect(Context context) {
    this.context = context;
    log = new Log();
}

My MysqlConnect.java :

package br.com.alerts;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class MysqlConnect extends AsyncTask<Void, Void, Boolean> {

    private static final String url = "jdbc:mysql://mysql.hostinger.com.br/u259053814_table";
    private static final String user = "root";
    private static final String pass = "root";
    private Connection con;
    private Log log;
    private Context context;
    private ProgressDialog dialog;

    public MysqlConnect(Context context) {
        this.context = context;
        log = new Log();
    }
    public Boolean isConected() {
        try
        {
            if (con == null)
            {
                return false;
            } else {
                return (!this.con.isClosed());
            } 
        } catch (SQLException e) {
            return false;
        }
    }
    public String getLog() {
        return log.toString();
    }
    public boolean connect() {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, pass);
            log.add("Conectado com sucesso!");
        } catch(Exception e) {
            e.printStackTrace();
        }
        return isConected();
    }
    public void disconnect() {
        try {
            con.close();
            con.isClosed();
            log.add("Desconectado!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(this.context);
        dialog.setMessage("Aguarde... conectando ao banco de dados...");
        dialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... params) {
        connect();
        return isConected();
    }
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        dialog.dismiss();
    }
}

Answer:

You are using log = new Log() , but this android class has no constructor available. The correct thing is to use the static methods of the Log class:

Log.d("tag","algum log"); // Debug
Log.e("tag","algum log"); // Error
Log.w("tag","algum log"); // Warning
Log.v("tag","algum log"); // Verbose
Log.i("tag","algum log"); // Info

Your code:

public class MysqlConnect extends AsyncTask<Void, Void, Boolean> {

    private static final String url = "jdbc:mysql://mysql.hostinger.com.br/u259053814_table";
    private static final String user = "root";
    private static final String pass = "root";
    private Connection con;
    //private Log log;
    private Context context;
    private ProgressDialog dialog;

    public MysqlConnect(Context context) {
        this.context = context;
        //log = new Log();
    }

    //public String getLog() {
    //    return log.toString();
    //}
    public boolean connect() {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, pass);
            //log.add("Conectado com sucesso!");
            Log.d("","Conectado com sucesso!");
        } catch(Exception e) {
            e.printStackTrace();
        }
        return isConected();
    }
    public void disconnect() {
        try {
            con.close();
            con.isClosed();
            //log.add("Desconectado!");
            Log.d("","Desconectado!");
        } catch (SQLException e) {
            e.printStackTrace();
        }
   }

   /// ...

}
Scroll to Top