2

I am struggling with an issue for some time now. I have a DB installed in a free hosting server (fdb3.freehostingeu.com). The DB is MySql. I have imported a jar file for the DB connection from here. I have import it as a dependency in the gradle, i have set the internet permissions in the Manifest and everything. I am using AsyncTasc for the connection.

Here is the class with the methods.

public class CheckLogin extends AsyncTask<String,String,String>
{
    String z = "";
    Boolean isSuccess = false;

    @Override
    protected void onPreExecute()
    {
//            progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String r)
    {
//            progressBar.setVisibility(View.GONE);
        Toast.makeText(LoginActivity.this, r, Toast.LENGTH_SHORT).show();
        if(isSuccess)
        {
            Toast.makeText(LoginActivity.this , "Login Successfull" , Toast.LENGTH_LONG).show();
            //finish();
        }
    }

    @Override
    protected String doInBackground(String... params)
    {
        java.sql.Connection con;
        String usernam = params[0];
        String passwordd = params[1];
        if(usernam.trim().equals("")|| passwordd.trim().equals(""))
            z = "Please enter Username and Password";
        else
        {
            try
            {
                con = connectionclass(); 

                if (con == null)
                {
                    z = "Check Your Internet Access!";
                }
                else
                {
                    // Change below query according to your own database.
                    String query = "select * from owner where mail = '" + usernam.toString() + "' and password = '"+ passwordd.toString() +"'  ";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    if(rs.next())
                    {
                        z = "Login successful";
                        isSuccess=true;
                        con.close();
                    }
                    else
                    {
                        z = "Invalid Credentials!";
                        isSuccess = false;
                    }
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                z = ex.getMessage();
            }
        }
        return z;
    }
}


@SuppressLint("NewApi")
public java.sql.Connection connectionclass()
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    java.sql.Connection connection = null;
    String ConnectionURL = null;
    try
    {
          Class.forName("com.mysql.jdbc.Driver");

          connection = DriverManager.getConnection(url, un, pas);

    }
    catch (SQLException se)
    {
        Log.e("error here 1 : ", se.getMessage());
    }
    catch (ClassNotFoundException e)
    {
        Log.e("error here 2 : ", e.getMessage());
    }
    catch (Exception e)
    {
        Log.e("error here 3 : ", e.getMessage());
    }
    return connection;
}

I have set the url at

jdbc:mysql://185.176.43.41:3306/DB_name

where the server IP is and the port is 3306 as the site says. And the usernmae and password as I use to connect to the DB.

I dont think i use something wrong, but still when i debug my app i get an Exception:

java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: failed to connect to /185.176.43.41 (port 3306): connect failed: ECONNREFUSED (Connection refused)

** BEGIN NESTED EXCEPTION **

java.net.SocketException MESSAGE: java.net.ConnectException: failed to connect to /185.176.43.41 (port 3306): connect failed: ECONNREFUSED (Connection refused)

STACKTRACE:

java.net.SocketException: java.net.ConnectException: failed to connect to /185.176.43.41 (port 3306): connect failed: ECONNREFUSED (Connection refused) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:225) at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805) at com.mysql.jdbc.Connection.(Connection.java:452) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411) at java.sql.DriverManager.getConnection(DriverManager.java:179) at java.sql.DriverManager.getConnection(DriverManager.java:213) at com.example.hotelreseration.LoginActivity.connectionclass(LoginActivity.java:234) at com.example.hotelreseration.LoginActivity$CheckLogin.doInBackground(LoginActivity.java:184) at com.example.hotelreseration.LoginActivity$CheckLogin.doInBackground(LoginActivity.java:149) at android.os.AsyncTask$2.call(AsyncTask.java:307) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:246) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:833)

** END NESTED EXCEPTION **

The java.sql.Connection connection is null, it is not use the (url, un, pas)

Ive been trying to find a way 2 days now, i have tried everything. I have no idea on SQL databases and i cannot get the difference between the MySql and the MSSql, and keep trying to make a connection to this host with jtds untill i understand that my DB is MySql.

If someone can help me here, this host provides a MySql DB, right?

Can I use it to store a DB and retrieve the data on my android app with JDBC?

Is the way i am trying correct or not?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kzaf
  • 563
  • 7
  • 19

1 Answers1

2

Your database url jdbc:mysql://185.176.43.41:3306/DB_name must be wrong in some part, either DB_name is not the name or 185.176.43.41:3306 is not the correct host. Try using localhost at first, try it local, be sure it works, the change your url to another one.

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • The DB_name is just an example, my real DB name is correct. So the problem is that i miss something with the URL? The code is OK? I am asking because i dont know where to focus. Thanks – Kzaf May 09 '17 at 19:27
  • If you configure your mysql, one of the think you start configuring is the localhost with the port 3306. If you are new at it, you can use some tool like mysql workbench, it will help you a lot, and then you can create a local instance of localhost:3306, start your mysql service, and after that, run your app (changing the adress and port, of course) – developer_hatch May 09 '17 at 19:35
  • So the port 3306 is for the localhost? – Kzaf May 09 '17 at 19:40
  • Yeah of course! I have always love to help :). Your question was very valid and right, don't worry you're on the right direction – developer_hatch May 09 '17 at 19:44