我必须开发一个 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
没有收到回复。请帮助我。
我如何开发这些。给我解决方案或想法。
最佳答案
可能是用户名变量不包含有效值,请尝试在调用查询之前打印它,也就是说,我看到您正在使用PreparedStatement
,当使用PreparedStatement时,您应该使用来自 PreparedStatement API
的方法以及查询中的占位符
。
PreparedStatement statement = con.prepareStatement("SELECT * FROM xcart_customers where login = ?);
statement.setString(1, username);
ResultSet result = statement.executeQuery();
关于java - 从mysql数据库获取数据并显示在android上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14623679/