Error trying to create a database connection (Mysql)

Asked

Viewed 428 times

3

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 excerpt from MysqlConnect:

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();
    }
}

1 answer

1

You are using log = new Log(), but this android class has no constructor available. Correct and use static class methods Log:

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();
        }
   }

   /// ...

}
  • but as I adapt to my code?

  • remove or comment lines log = new Log(); , private Log log; and the function getLog where you are using your variable log starts using Log.d("","a sua mensagem");

  • I’ll test here and give an opinion of the result..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.