【发布时间】:2012-04-05 16:10:00
【问题描述】:
我正在尝试从我的 android 应用程序连接 SQL Azure。该代码适用于我设法从 SQL Azure 获取数据的 Java 应用程序,但它会为我的 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.".
【问题讨论】:
-
附带说明,如果这将是一个用于大规模分发的 android 应用程序,您真的不应该将与 SQL Azure 数据库(或任何数据存储)的连接放入应用程序本身。这会将那个王国的钥匙交到任何拥有该应用程序的人手中。相反,您应该使用数据服务(最好托管在数据附近)来处理任何数据请求。这不仅可以保护数据库免受不受控制、不受监控的访问,还可以让您实现数据缓存等功能,这些功能将帮助您随着服务需求的增加而扩展。
-
@BrentDaCodeMonkey 感谢您提供的信息。我在发布他们建议使用 WEB SERVICE 的问题后阅读了一篇文章...
-
这正是我会做的事情。 :)
标签: java android azure azure-sql-database