[英]How to retrieve data from Sql Server database?
我是Android新手。 我已經連接到SQL Server,並且想從數據庫中檢索數據並將其顯示在表中。
這是代碼:
public void QuerySQL(String COMANDOSQL) {
try {
//String sql="select * from Activity";
Statement stmt = ma.connect.createStatement();
stmt.executeQuery("SELECT * FROM "+excbtn.getText().toString()+"");
ResultSet rs=stmt.getResultSet();
ResultSetMetaData rsmd= rs.getMetaData();
int colcount=rsmd.getColumnCount();
int j=0;
while(rs.next()){
j++;
for(int i=1; i<=colcount; i++)
{
String ss=rsmd.getColumnName(i);
// create a new TextView
if(j<=colcount){
t = new TextView(this);
t.setText(ss);
t.setWidth(200);//Set to any meaningful text
t.setBackgroundColor(Color.YELLOW);
tr.addView(t); //Attach TextView to its parent (row)
TableRow.LayoutParams params =
(TableRow.LayoutParams)t.getLayoutParams();
params.column= i; //place at ith columns.
params.span = 1; //span these many columns,
params.setMargins(2,2,2,2);
params.width = TableRow.LayoutParams.FILL_PARENT;
params.height = TableRow.LayoutParams.WRAP_CONTENT;
t.setPadding(2, 2, 2, 2);
t.setLayoutParams(params);
//colhead=true;
}
t1 = new TextView(this);
String tabval=rs.getString(ss);
t1.setText(rs.getString(ss));
t1.setWidth(200);//Set to any meaningful text
// t1.setBackgroundColor(Color.YELLOW);
tr1.addView(t1); //Attach TextView to its parent (row)
TableRow.LayoutParams params1 =
(TableRow.LayoutParams)t.getLayoutParams();
params1.column= i; //place at ith columns.
params1.span = j; //span these many columns,
params1.setMargins(2,2,2,2);
params1.width = TableRow.LayoutParams.FILL_PARENT;
params1.height = TableRow.LayoutParams.WRAP_CONTENT;
t1.setPadding(2, 2, 2, 2);
t1.setLayoutParams(params1);
//String from = {ss1};
}
如何綁定數據以及在哪里執行表行?
通過假設您的數據庫位於遠程服務器上,
1)您需要一個WebService來從服務器檢索數據。
2)從服務器檢索數據后,您需要使用其格式(例如JSON,XML)對其進行解析
3)解析數據后,您可以根據您的應用程序需求對其進行操作或存儲。
(現在,專注於如何使用WebService從服務器檢索數據,一旦獲得數據,請專注於UI部分)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.