2012-03-21 13 views
1

私はアンドロイドアプリケーションからSQL Azureに接続しようとしています。コードはJava AzureからデータをフェッチすることができるJavaアプリケーションでは問題なく動作していますが、Androidのエラーが発生します。以下は私のコードとエラーメッセージです。
コード:AndroidでSQL Azureを接続する

String connectionString = 
        "jdbc:sqlserver://serversql.database.windows.net:1433" + ";" + 
       "database=dbname " + ";" + 
       "[email protected]" + ";" + 
       "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.". 
+2

これは、大量配布のためのアンドロイドアプリケーションになる場合、実際にはSQL Azureデータベース(または任意のデータストア)への接続をアプリケーション自体に入れないでください。これは、その王国への鍵を申請書を持っている人の手に渡します。代わりに、データの近くでホストされているデータサービスを使用して、データ要求を処理する必要があります。管理されていないアクセスからデータベースを保護するだけでなく、データキャッシュのような機能を実装する機能を提供し、サービスの需要が高まるにつれて規模を拡大するのに役立ちます。 – BrentDaCodeMonkey

+0

情報のおかげで@BrentDaCodeMonkey ..私は彼らがWEBサービスを使用することを提案する質問を投稿した後、記事を読んで... –

+0

それはちょうど私がchinnaをやっているものです。 :) – BrentDaCodeMonkey

答えて

0

は、ほとんどの場合これは、SQL Azureのファイアウォールです。おそらく、それが使用されるためにことを開く必要があります:

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

を使用すると、AndroidアプリのIPアドレスを知らないので、私は選択肢があると思うだろう:

  • オープンファイアウォールを完全にアップしてください(255.055.255.255で0.0.0.0から始まります)
  • Androidから呼び出してデータベースにアクセスできるWCFサービスを作成してください。これはおそらくより安全です。
+0

ファイアウォールを変更すると、新しいエラーが発生しました..... com.microsoft.sqlserver.jdbc.SQLServerException:ドライバは、Secure Sockets Layer(SSL)暗号化を使用してSQL Serverへの安全な接続を確立できませんでした。エラー:「Socket closed」。 ClientConnectionId:ec331467-d01a-46d8-a3f2-db63aefe255f –

関連する問題