java - 将 SQL Azure 与 Android 连接

标签 java android azure azure-sql-database

我正在尝试从我的 Android 应用程序连接 SQL Azure。该代码对于 Java 应用程序运行良好,我设法从 SQL Azure 获取数据,但它对我的 android 抛出错误。下面是我的代码和错误消息。
代码:

String connectionString =
                    "jdbc:sqlserver://serversql.database.windows.net:1433" + ";" +  
                "database=dbname    " + ";" + 
                "user=user@serversql" + ";" +  
                "password=password";
            Connection connection = null;  // For making the connection
            Statement statement = null;    // For the SQL statement 
            ResultSet resultSet = null;    // For the result set, if applicable 
        try
        {

            // Ensure the SQL Server driver class is available.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");              
            // Establish the connection.
            connection = DriverManager.getConnection(connectionString);
            String filename = "SVPoster.jpg";
            String sql = "Select * from VoucherTable";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            int count;
            if(resultSet.next())
            {
                Blob test = resultSet.getBlob("Voucher"); 
                InputStream x=test.getBinaryStream();
                int size=x.available();
                OutputStream output = new FileOutputStream("/sdcard/"
                        + filename);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = x.read(data)) != -1) {
                    total += count;
                    output.write(data, 0, count);
                }
                output.flush();
                output.close();
                x.close();
            }               
        }
        catch (Exception ex)
        {
            Toast.makeText(getApplicationContext(), ex.toString(),
                     Toast.LENGTH_LONG).show();
        }

错误信息

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server.database.windows.net, port 1433 has failed. Error: "null. 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.".

最佳答案

这很可能是由于 SQL Azure 防火墙造成的。您可能必须打开它才能使用它:

https://msdn.microsoft.com/en-us/library/azure/jj553530.aspx

由于您不知道 Android 应用程序的 IP 地址,我认为选择是:

  • 完全打开防火墙(从 0.0.0.0 开始,到 255.255.255.255 结束)
  • 创建一个可以从 Android 调用的 WCF 服务,并授予该服务对数据库的访问权限,这可能是一个更好的解决方案,因为它也更安全。

关于java - 将 SQL Azure 与 Android 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9799567/

相关文章:

java - 在 JSF Web 应用程序中处理 'session expired',在 JBoss AS 5 中运行

android - Android 4.4和5.0中不同的状态栏样式

azure - 识别列中的部分文本并在 Azure 数据流中创建新列?

java - 时区和 SimpleDateFormat 奇怪的行为

java - 为什么 protected 子类的不同包中的其他类私有(private)

java - 单击按钮时,使用从 Spinner 中选择的值设置 TextView 的文本大小

iis - 如何更新approot/bin中的DLL

azure - Azure 站点是否是过渡部署的?

java - 无法看到 JTextField

java - java中的实时套接字