请帮帮我。我想从sql server获取数据

时间:2012-05-08 08:59:22

标签: android

    Connection conn = null;
        String url = "jdbc:jtds:sqlserver://192.168.1.25:1433/";
        String dbName = "Demo;Instance=MSSQLSERVER;";
        String userName = "BIT"; 
        String password = "1234";
        String driver = "net.sourceforge.jtds.jdbc.Driver";
        try {
          TextView  tv1 = (TextView) findViewById(R.id.textView1);
          TextView  tv2 = (TextView) findViewById(R.id.textView2);
          TextView  tv3 = (TextView) findViewById(R.id.textView3);
          Class.forName(driver).newInstance();
          conn = DriverManager.getConnection(url+dbName,userName,password);
          Log.w("Connection","open");
          Statement stmt = conn.createStatement();
          ResultSet rs = stmt.executeQuery("SELECT ItemDesc,Qty,NetPrice FROM TrxDetail ");
          String a ="";
      String b ="";
      String c ="";

      while (rs.next()) 
      {
           a += rs.getString("ItemDesc");
           b += rs.getString("Qty");
           c += rs.getString("NetPrice");
     }
      tv1.setText(a);
      tv2.setText(b);
      tv3.setText(c);
      conn.close();
          } 

XML:                                                                    

为什么不是所有的信息。 刚刚发布了最终数据。 如何显示所有数据。 如何编辑xml正确。 我不知道该问谁。

1 个答案:

答案 0 :(得分:0)

 while (rs.next()) 
      {
           tv1.setText(rs.getString("ItemDesc"));
           tv2.setText(rs.getString("Qty"));
           tv3.setText(rs.getString("NetPrice"));
     }

对于您获得的每个结果,您将tv1,tv2和tv3的内容设置为该结果的内容,覆盖以前的内容。如果这不是你想要的(你实际上并没有问一个问题,所以很难说,但我猜你的稀疏“为什么不是所有的信息”意味着你在抱怨只看到最后一个条目?),你不应该覆盖它们。

您可以做的是String,将新值连接到当前值,然后在setText之后调用while一次?

好的,你可以这样做。您可能更好地使用StringBuffer btw,但这需要更多解释......

String tv1S = "";
String tv2S = "";
String tv3S = "";

 while (rs.next()) {
      tv1S += rs.getString("ItemDesc"));
      tv2S += rs.getString("Qty"));
      tv3S += rs.getString("NetPrice"));
 }
 tv1.setText(tv1S);
 tv2.setText(tv2S);
 tv3.setText(tv3S);

现在你也需要一些间距和类似的东西,但我不打算全部为你写,这应该足以解决它....

相关问题