简体   繁体   English

从Android连接到MS SQL数据库时出错

[英]Error Connecting from android to MS SQL database

I've been trying to connect from android to Microsoft SQL server's database. 我一直在尝试从android连接到Microsoft SQL Server的数据库。 But i'm not able to do it. 但是我做不到。 Please help. 请帮忙。 I've tried so many things. 我已经尝试了很多东西。 I was using jtds - 1.3.1 i tried using 1.3.0 version also. 我正在使用jtds-1.3.1我也尝试使用1.3.0版本。 But still failed. 但是还是失败了。 Please help. 请帮忙。

public class ConnectionClass {
String ip = "192.168.0.101\\SQLEXPRESS:1433";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "dictionary";
String un = "";
String password = "";

@SuppressLint("NewApi")
public Connection CONN() {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection conn = null;
    String ConnURL = null;
    try {

        Class.forName(classs);
        ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
                + "databaseName=" + db + ";user=" + un + ";password="
                + password + ";";
        conn = DriverManager.getConnection(ConnURL);
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return conn;
}
}

and i'm getting this error: 我收到此错误:

E/ERRO: Unknown server host name 'Host is unresolved: 192.168.0.101\\SQLEXPRESS'. E / ERRO:未知的服务器主机名'主机未解析:192.168.0.101 \\ SQLEXPRESS'。

I've also tried removing SQLEXPRESS(like this: 192.168.0.101:1033) from above line but then another error is coming. 我也尝试过从上面的行中删除SQLEXPRESS(例如:192.168.0.101:1033),但是接下来会出现另一个错误。

I've enabled TCP/IP from MS SQL and i've also allowed port 1433 from firewall as well as i've also allowed MS SQL from firewall. 我从MS SQL启用了TCP / IP,也从防火墙允许了端口1433,也从防火墙允许了MS SQL。 MS SQL's allow remote connection is also checked. 还检查了MS SQL的允许远程连接。

Please help. 请帮忙。 Thanks.. 谢谢..

That isn't a valid url. 这不是有效的网址。 You'd need to use either the IP alone, or scheme://ip. 您需要单独使用IP或scheme:// ip。 However it is NOT recommended to directly connect to a db from a mobile device- doing so requires your password to be in the app, which means anyone who uses the app has access to your password. 但是,不建议直接从移动设备连接到数据库-这样做需要您在应用程序中输入密码,这意味着使用该应用程序的任何人都可以访问您的密码。 Use a web service instead and keep the password on your server only for security. 请改用Web服务,并将密码仅保留在服务器上以确保安全。

Also, never set the strict mode. 另外,请勿设置严格模式。 First off its a hack. 首先,它是一个hack。 Secondly it doesn't work on all devices. 其次,它不适用于所有设备。 Third of all, if you need it you're going to leave your app unresponsive and quite possibly crashing due to watchdog timers. 第三,如果需要,您将使您的应用程序无响应,并且很可能由于看门狗计时器而崩溃。 ANy site you saw using it doesn't know what they're doing. 您看到的使用它的任何站点都不知道他们在做什么。

This is not true address '192.168.0.101\\SQLEXPRESS:1433' just go on like this '192.168.0.101' 这不是真实的地址'192.168.0.101 \\ SQLEXPRESS:1433'就像这样'192.168.0.101'

Do not try it on your device(sometimes it cant see the server's ip) , firstly try it with any 'Emulator' also dont forget to make network settings on VirtualBox 不要在您的设备上尝试(有时它看不到服务器的IP),首先使用任何“仿真器”尝试它,也不要忘记在VirtualBox上进行网络设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM