我正在使用 SOAP WebServices 为 MySQL 数据库连接开发一个 Android 项目。
在我的数据库中,我有 3 个字段(用户名、密码、电子邮件)。
在这里,我插入 3 个用户的值。我为登录表单创建了一个网络服务代码。
登录表单代码为:
package com.userlogin.ws;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Login {
public String authentication(String userName, String password) {
String retrievedUserName = "";
String retrievedPassword = "";
String status = "";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidlogin", "root", "");
PreparedStatement statement = con.prepareStatement("SELECT * FROM user WHERE username = '" + userName + "'");
ResultSet result = statement.executeQuery();
while (result.next()) {
retrievedUserName = result.getString("username");
retrievedPassword = result.getString("password");
}
if (retrievedUserName.equals(userName) && retrievedPassword.equals(password)) {
status = "Success!";
} else {
status = "Login fail!!!";
}
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
}
忘记密码。网络服务代码是:
package com.userlogin.ws;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
public class ForgetPassword {
public String customerData(){
String customerInfo = "";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidlogin","root","");
//Find customer information where the customer ID is maximum
PreparedStatement statement = con.prepareStatement("SELECT * FROM user WHERE username = (SELECT username FROM user)");
ResultSet result = statement.executeQuery();
while(result.next()){
customerInfo = customerInfo + result.getString("username") + "&" + result.getString("password") + "&"+result.getString("email");
//Here "&"s are added to the return string. This is help to split the string in Android application
}
}
catch(Exception exc){
System.out.println(exc.getMessage());
}
return customerInfo;
}
}
如果我点击了忘记密码,应用程序应该启动下一个Activity
.
下一个Activity
应该要求电子邮件,id..
输入邮件 ID 后,它应该继续处理另一个Activity
并仅显示该特定人员的密码。
我应该使用哪个查询?