java Android Studio mysql服务器超时/拒绝连接

wj8zmpe1  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(196)

我目前试图连接Android Studio到我的SQL服务器,但没有运气,然后我试图连接到我的本地MySQL数据库,我得到完全相同的错误,这是它看起来像:
E/Database: Network error IOException: failed to connect to /192.168.0.10 (port 1433) from /:: (port 54516): connect failed: ETIMEDOUT (Connection timed out)
下面是我的代码片段:
jdbc:jtds:mysql://本地主机:3306/Java数据库、Java、密码
When it comes to the ip string I've tried just about everything such as 10.0.0.2 or using my actual ipv4 address but I still get the same error.

@SuppressLint("NewApi")
public Connection openConnection() {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    try {
        String ip = "localhost";
        String db = "javabase";
        String un = "java";
        String password = "password";
        String connectionURL = "jdbc:jtds:sqlserver://" + ip + "/" + db+ ";user=" + un + ";password=" + password + ";";
        Log.d("Database", "Attempting to establish connection to database server...");
        con = DriverManager.getConnection(connectionURL);
        Log.d("Database", "Connection to " + con.getMetaData().getURL());
        return con;
    } catch (Exception ex) {
        Log.e("Database", ex.getMessage());
    }
    return null;
}

先谢了。

bqujaahr

bqujaahr1#

您的连接字符串不正确。连接字符串应为:字符串连接URL =“jdbc:jtds:sql服务器://”+ ip +“:“+端口+“;数据库名称=”+ db +“;用户=”+ un+“;密码=”+密码;

相关问题