簡體   English   中英

從mysql db獲取數據並在android上顯示

[英]fetching data from mysql db and display on android

我必須開發一個android示例。

它執行從mysql數據庫獲取數據並顯示在android應用程序上的功能。

我已使用以下Web服務代碼:

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上顯示數據。但是我沒有得到response。我該如何獲取它。

在這里,我必須編寫類似static的條件:

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

沒有得到答復。請幫助我。

我該如何開發這些解決方案。

可能是用戶名變量沒有有效值,請在調用查詢之前嘗試打印它,例如,我看到您正在使用PreparedStatement ,當使用PreparedStatement時,應使用PreparedStatement API方法以及其中的place holders查詢。

 PreparedStatement statement =  con.prepareStatement("SELECT * FROM xcart_customers where login = ?);
statement.setString(1, username);
 ResultSet result = statement.executeQuery();

並不是一個真正的答案,但據我所知,基本上是嘗試(通過jdbc或其他任何可行的方式)與外部數據庫建立1on1連接的不良做法。

這並非旨在處理由於失去連接等而導致設備上所有嚴重錯誤的事情。

您最好創建一些(建議使用REST)Web服務來公開您的數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM