从mysql db获取数据并在android上显示

时间:2013-01-31 10:33:58

标签: java android mysql

我必须开发一个Android示例。

它从mysql数据库执行获取数据并在android应用程序上显示。

我使用了以下网络服务代码:

public class EditProfile {
public String customerData(String Username,String Password,String Firstname,String Lastname,String Company,String Taxnumber,String Baddress,String Bcity,String Bstate,String Bcountry,String Bzipcode,String Saddress,String Scity,String Sstate,String Scountry,String Szipcode,String Email,String Phone,String Fax,String Url){

 String customerInfo = "";

try{

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://10.0.0.75:3306/xcart432pro","root","");
 PreparedStatement statement =  con.prepareStatement("SELECT * FROM xcart_customers where login = '"+Username+"'");
 ResultSet result = statement.executeQuery();

while(result.next()){
            customerInfo = customerInfo + result.getString("login") + ":" +result.getString("password")+ ":" +result.getString("firstname")
    }

     }
     catch(Exception exc){
         System.out.println(exc.getMessage());
           }
        return customerInfo;
        }

我在下面使用了android代码:

 public class RetailerActivity extends Activity {
    private final String NAMESPACE = "http://xcart.com";
  private final String URL = "http://10.0.0.75:8085/XcartLogin/services/EditProfile?wsdl";
  private int i;
  private final String SOAP_ACTION = "http://xcart.com/customerData";
  private final String METHOD_NAME = "customerData";

  String str,mTitle;
  /** Called when the activity is first created. */
  @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
         try {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

     envelope.setOutputSoapObject(request);

     HttpTransportSE ht = new HttpTransportSE(URL);

       ht.call(SOAP_ACTION, envelope);

         SoapPrimitive s = response;
        str = s.toString();
       String[] words = str.split(":");
       TextView tv = (TextView) findViewById(R.id.user);
        tv.setText(Username);

      EditText tv1 = (EditText) findViewById(R.id.pass);


        tv1.setText(words[1].toString());
      EditText tv2 = (EditText) findViewById(R.id.tf_userName);


         tv2.setText(words[2].toString());

          for (int i = 0, l = words.length; i < l; ++i) {
                }}

       catch (Exception e) {
          e.printStackTrace();
           }
            }}

这里我必须运行应用程序意味着从mysql数据库中获取数据并在android上显示数据。但是我没有得到响应。我可以得到它。

在这里我必须编写类似静态的条件:

SELECT * FROM xcart_customers where login = 'mercy'"); means fetching the data from mysql database and display the data for that user.its successfully worked.

但我已动态编写代码

"SELECT * FROM xcart_customers where login = '"+Username+"'"); means

没有得到回复。请帮助我。

我如何为这些开发这些我的解决方案或想法。

2 个答案:

答案 0 :(得分:1)

可能是username变量没有保存有效值,尝试在调用查询之前打印它,说,我看到你正在使用PreparedStatement,在使用PreparedStatement时你应该使用{ {1}}以及查询中的PreparedStatement API

place holders

答案 1 :(得分:0)

不是一个真正的答案,但据我所知,基本上是不好的做法,尝试与外部数据库建立1on1连接(通过jdbc或任何其他可想到的方式)。

这不是为了处理由于失去连接而导致设备上出现严重错误的所有内容。

你最好创建一个(我建议使用REST)webservice来公开你的数据库。