2

我与外部服务器 SQL Server 建立连接,但只能使用以下代码显示数据:

void cursorset (){
    java.sql.DatabaseMetaData dm = null;

    try {
        connection = this.getConnection();

        if (connection != null) {
            Resultset = result;
            private final String statement = "select*from *******";
            dm = connection.getMetaData();            
            Statement select = connection.createStatement();
            result = select.executeQuery(statement);

            while (result.next()) {
                mostrar_datos.append("" +result.getObject(1)+result.getObject(2)+" "+result.getObject(3));
            }

            result.close();
            result = null;
            closeConnection();
        } else {
            mostrar_datos.append("Error: No active Connection");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    dm = null;
}

问题是我需要在具有动态行和列的表或 alertdialog 上显示此返回的数据,但我不知道该怎么做。

4

2 回答 2

1

如何将 SQL Server 查询中的数据显示到 android 表中

因此,您应该创建一些解释真实数据库中的表的类,并且上述方法应该返回值列表。

随后,返回的数据可以简单地显示在AlertDialog或一些ListView

例子:

您的数据库有一个表:用户有 3 列 - id(主键)、名称、姓氏。所以你创建具有“相同结构”的类

public class User {

   private int id;
   private String name;
   private String surname;

   // getters and setters
}

然后您的方法可以如下所示:

public List<User> getAll() {
   List<User> users = new ArrayList<User>();
   User u = null;
   // initialise connection etc.
   while (resultSet.next()) {
      u = new User();
      u.setId(resultSet.getInt(1));
      u.setName(resultSet.getString(2));
      u.setSurname(resultSet.getString(3));
      users.add(u);  
   }
   return users;
   // in finally block close connection.
}
于 2013-02-20T15:43:19.563 回答
0

You could easily display the data in a ListView with a corresponding CursorAdapter, your query would return a Cursor object. The CursorAdapters responsibility is to map data from the Cursor to ids in the layout of a row in the ListView.

Here are a few tutorials to get you started:

http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/part_4_-_using_cursoradapters

http://developer.android.com/reference/android/app/ListActivity.html

于 2013-02-20T16:02:30.107 回答