String[] items = new String[10];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://servername.database.windows.net;" +
"databaseName=School;user=username@servername;password=userpassword";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.list_item,
new ArrayList()));
new AddStringTask().execute();
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT TOP 10 * FROM dbo.tbl";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
String items = rs.getArray(2) + " " + rs.getArray(3);
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
class AddStringTask extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... unused) {
return(null);
}
@Override
protected void onProgressUpdate(String... item) {
((ArrayAdapter)getListAdapter()).add(item[0]);
}
@Override
protected void onPostExecute(Void unused) {
Toast
.makeText(AsyncDemo.this, "Done!", Toast.LENGTH_SHORT)
.show();
}
}
在onCreate里面的while循环中我希望从远程数据库中的表中得到10行和2列,这是sql azure然后将它存储在字符串数组中。然后该字符串数组作为列表视图输出给用户。 JDBC驱动程序下载链接min.bz/wC4Am(非Windows用户获取压缩文件。列出的最后一个)也更新了链接。
答案 0 :(得分:1)
Android-Java-SQLAzure很好的组合,恭喜!
另一方面,为什么直接从移动应用程序访问数据库(SQL Azure)?这有一些缺点:
因此,您应该使用Java或ASP.NET创建Web服务或REST API,您可以通过此代理执行查询,并以JSON或XML或Java可以轻松解析和调整的良好格式提供结果你的ArrayAdapter。
答案 1 :(得分:0)
有关Android的Microsoft Azure工具包的更多信息,请访问:
http://www.wadewegner.com/2011/08/windows-azure-toolkits-for-devices-now-with-android/ http://social.technet.microsoft.com/wiki/contents/articles/4598.aspx