0

我必须开发一个 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

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

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

4

2 回答 2

1

It could be that username variable doesn't hold a valid value, try to print it before you invoke the query, sayin that, i see you are using PreparedStatement, when using PreparedStatement you should use methods from PreparedStatement API along with the place holders in the query.

 PreparedStatement statement =  con.prepareStatement("SELECT * FROM xcart_customers where login = ?);
statement.setString(1, username);
 ResultSet result = statement.executeQuery();
于 2013-01-31T10:38:23.980 回答
0

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

这并非旨在处理由于失去连接等而在您的设备上出现严重错误的所有事情。

您最好创建一个小(我建议使用 REST)Web 服务来公开您的数据库。

于 2013-01-31T10:41:56.983 回答