I’m trying to connect to mysql database from my android application.
I’m getting Communications link failure error.
Below is the code snippet:
public class MySqlConnector {
private Connection con = null;
private String s = "";
private String username = "root";
private String password = "password01";
private String connectionString;
public String ConnectToDb() {
connectionString ="jdbc:mysql://192.168.1.104:3306/mydatabase";
//connectionString="jdbc:mysql://10.0.0.0:3306/mydatabase";
// connectionString="jdbc:mysql://127.0.0.1:3306/mydatabase";
//connectionString = "jdbc:mysql://MainSrv04:3306/mydatabase";
// connectionString="jdbc:mysql://localhost:3306/mydatabase";
// connectionString =
// "jdbc:mysql://localhost:3306/mydatabase?user=root&password=password01&useUnicode=true&characterEncoding=UTF-8";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
try {
con = DriverManager.getConnection(connectionString, username,
password);
Statement st = con.createStatement();
String sql = "SELECT First_Name FROM mydatabase.custinfo where CardNumber=5325784707";
ResultSet rs = st.executeQuery(sql);
s = rs.getString("First_Name");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
Log.i("MySqlConnector", "Database connection terminated");
} catch (Exception e) { /* ignore close errors */
}
}
}
if (s == "") {
s = "No Result";
}
Log.i("MySqlConnector : s=", s);
return s;
}
}
I tried all possible combinations as showed in comments and getting following error in caused by field in logcat:
- while using localhost –>
Caused by: java.net.ConnectException: localhost/127.0.0.1:3306 - Connection refused - while using 127.0.0.1 –>
Caused by: java.net.ConnectException: /127.0.0.1:3306 - Connection refused - while using 10.0.0.0 –>
Caused by: java.net.SocketException: The operation timed out - while using 192.168.1.104 –>
Caused by: java.net.SocketException: The operation timed out - while using MainSrv04 –>
Caused by: java.net.UnknownHostException: MainSrv04
I also pinged mysql port through telnet and it’s working.
Also, I have taken care of privileges.
But still I’m getting Communications link failure error.
Any help appreciated.
Is android application is on same ntework? If not
and
will not work.
Give the proper network address of mysql.