我正在尝试从我的 android 应用程序连接到 mysql 数据库。
我收到通信链路故障错误。
下面是代码片段:
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;
}
}
我尝试了所有可能的组合,如评论中所示,并在 logcat中的字段引起以下错误:
- 使用本地主机时 -->
Caused by: java.net.ConnectException: localhost/127.0.0.1:3306 - Connection refused
- 使用 127.0.0.1 时 -->
Caused by: java.net.ConnectException: /127.0.0.1:3306 - Connection refused
- 使用 10.0.0.0 时 -->
Caused by: java.net.SocketException: The operation timed out
- 使用 192.168.1.104 时 -->
Caused by: java.net.SocketException: The operation timed out
- 使用 MainSrv04 时 -->
Caused by: java.net.UnknownHostException: MainSrv04
我还通过telnet pingmysql
端口并且它正在工作。
另外,我已经处理了特权。
但我仍然遇到Communications link failure
错误。
任何帮助表示赞赏。