我想連接到SQL Server遠程數據庫,但我無法在Android中創建新連接,儘管我可以通過具有相同代碼的Java應用程序進行連接。 DriverManager.getConnection()
引發異常。這裏是我的代碼將android連接到遠程sqlserver
public DatabaseManipulation()
{
URL=String.format(URL,localDatabaseName);
Connect();
}
public void Connect() {
try {
connection = DriverManager.getConnection(URL,
DATABASE_USERNAME, DATABASE_PASSWORD);
statement=connection.createStatement();
} catch(Exception e) {
Log.d("con",e.getMessage);
}
}
例外:
"The TCP/IP connection to the host 192.168.100.23, port 1433 has failed. Error: "/192.168.100.23:1433 - Network is unreachable. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall."."
192.168.100.23是不是從互聯網訪問的私網IP地址。您的設備是否位於同一網絡上?順便說一下,從移動設備直接訪問遠程數據庫會帶來很多安全和體系結構問題。你有沒有考慮在你的數據庫前面有一項服務來支持移動應用程序? – wdosanjos 2014-08-28 06:04:17
是設備在同一個網絡中。我可以連接到另一臺PC上的Java應用程序,但沒有問題,但Android不起作用。 – Alireza 2014-08-28 06:13:40
您的應用程序是否具有在AndroidManifest.xml中設置的網絡訪問權限? (''使用權限的android:name =「android.permission.INTERNET」/>') – wdosanjos 2014-08-28 06:17:25