2

am successfully finished login form in android using mysql connection via jdbc in java...its successfully worked in my localhost...but development site its not worked...the following error is displyed.

java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: Connection timed out: connect

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection timed out: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection timed out: connect
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
    at com.mysql.jdbc.Connection.<init>(Connection.java:452)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.retrieve.retrieve.main(retrieve.java:15)


** END NESTED EXCEPTION **


    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
    at com.mysql.jdbc.Connection.<init>(Connection.java:452)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.retrieve.retrieve.main(retrieve.java:15)

why dis error is occurred.give me some solutions. if am running localhost means my sql query is

             package com.example.login;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginLayoutActivity extends Activity {


    EditText username,password;
    TextView error;
    Button ok;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        username=(EditText)findViewById(R.id.et_un);
        password=(EditText)findViewById(R.id.et_pw);
        error=(TextView)findViewById(R.id.tv_error);
        ok=(Button)findViewById(R.id.btn_login);

        ok.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Editable user = username.getText();
                Editable pass = password.getText();

                try {

                    **Class.forName("com.mysql.jdbc.Driver");

                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/people", "root", "");
                        Statement st = con.createStatement();


                        ResultSet rs = st.executeQuery("SELECT password FROM customers where login='"+user+"'");**

                        if(rs.next())
                        {
                            String dbpass = rs.getString(1);
                            if(dbpass.equals(pass)){
                                 error.setText("Correct Username or Password");

                        }
                        else
                        {
                            error.setText("Sorry!! Incorrect Username or Password");
                        }
                        }
                }

                     catch (SQLException e) {
                        username.setText(e.toString());
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }


        });
    }
}

if am running development site means my query is:

              package com.example.login;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginLayoutActivity extends Activity {


    EditText username,password;
    TextView error;
    Button ok;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        username=(EditText)findViewById(R.id.et_un);
        password=(EditText)findViewById(R.id.et_pw);
        error=(TextView)findViewById(R.id.tv_error);
        ok=(Button)findViewById(R.id.btn_login);

        ok.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Editable user = username.getText();
                Editable pass = password.getText();

                try {

                    **Class.forName("com.mysql.jdbc.Driver");

                        Connection con = DriverManager.getConnection("jdbc:mysql://dev.xxxxx.com/xxx", "xxxx", "xxxx");
                        Statement st = con.createStatement();


                        ResultSet rs = st.executeQuery("SELECT password FROM customers where login='"+user+"'");**

                        if(rs.next())
                        {
                            String dbpass = rs.getString(1);
                            if(dbpass.equals(pass)){
                                 error.setText("Correct Username or Password");

                        }
                        else
                        {
                            error.setText("Sorry!! Incorrect Username or Password");
                        }
                        }
                }

                     catch (SQLException e) {
                        username.setText(e.toString());
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }


        });
    }
}

dis development site username,password,database name and url all r correct only...then why dis error is occured....give me solutions...

hariprasad
  • 45
  • 1
  • 2
  • 7

4 Answers4

1

If you are trying to connect your mysql in the localhost from an android application, it may not get communication, refer this.

This exception seems like the mysql doesn't have public access. Make your mysql available in a remote system and give the remote system's ip. Let 192.168.1.102 be its ip, then change this statement as jdbc:mysql://192.168.1.102:3306/people.

You have to modify the mysql configuration file. It may be /etc/mysql/my.cnf (in linux) or \xampp\mysql\bin\my.ini (depends on mysql refer more). By default it contains bind-address = 127.0.0.1, change it as bind-address = 0.0.0.0. When this change is made, then the mysql is available publicaly i.e. it can be accessed remotely. Now your application can access this mysql.

Community
  • 1
  • 1
Visruth
  • 3,430
  • 35
  • 48
0

If you use en emulator use a special IP to your localhost.

10.0.2.2

jdbc:mysql://10.0.2.2:3306/people

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
Gerrlt
  • 39
  • 7
0

Sounds like a network problem. Verify if you can access the Database host and port from the device you're making the connection, and if the DBMS is up and running.

Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
0

The key question is whether or not the mobile device is on the network when you try to connect to the database. If you're on the network, you'll need to talk to the MySQL database administrator to see if you have permission to access that database. I'm guessing that you do not, because you're trying to log in as root without a password. No administrator in their right mind would allow you to do that.

If you're the administrator, please call your therapist of pharmacist.

If the mobile device is not on the network, I'm guessing that the database is behind a firewall of some kind. At best you'll have to talk to some administrator to get firewall rules set up to allow access from the mobile device. Those usually require some information about the device. Firewall rules won't be practical if you intend to release an app for several users.

A better solution would be to set up a proxy that would authenticate incoming mobile devices and communicate with the database on their behalf.

I would not have a mobile device connecting directly to a database, especially the way you're trying to do it.

Who else knows about what you're trying to do? It looks to me like you're trying to connect to a database, but that fact isn't widely known. Who else knows about your project?

duffymo
  • 305,152
  • 44
  • 369
  • 561