好的,让我们从头开始:
在我的Mainactivity中,我有一个连接到MySQL数据库的空白和另一个发送查询并返回用户和密码的空白
我将数据库的名称和密码存储到两个变量中,然后将它们与我在用户的EditText中写入的内容和密码进行比较,问题是如果EdittText中的内容等于我从数据库中带来的东西总是告诉我它们不一样。
这是代码:
package com.example.merli.loginconmysql;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
public class MainActivity extends Activity {
EditText usuario;
EditText contra;
Button entrar;
String usuarioAchequear;
String contraAChequear;
String bd_usuario = "android";
String bd_contra = "123456";
String bd_url = "jdbc:mysql://192.168.1.13:3306/test" + "?user=" + bd_usuario + "&password=" + bd_contra + "&useUnicode=true&characterEncoding=UTF-8";
Connection conec;
Statement estado;
ResultSet query;
Context contexto = this;
int prueba;
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usuario = (EditText) findViewById(R.id.usuario);
contra = (EditText) findViewById(R.id.contrasena);
entrar = (Button) findViewById(R.id.entrar);
conxecionBaseDatos();
entrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
entrarxD();
prueba = usuario.getText().toString().length();
if (usuario.getText().toString() == usuarioAchequear)
{
AlertDialog.Builder dialogo = new AlertDialog.Builder(contexto);
dialogo.setMessage("Bienvenido! " + usuarioAchequear);
dialogo.setPositiveButton("Okey", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialogo.create();
dialogo.show();
}else
{
AlertDialog.Builder dialogo = new AlertDialog.Builder(contexto);
dialogo.setMessage("Usuario o contraseña incorrecta " + prueba);
dialogo.setPositiveButton("Okey", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialogo.create();
dialogo.show();
}
}
});
}
public void conxecionBaseDatos()
{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conec = DriverManager.getConnection(bd_url);
estado = conec.createStatement();
}catch (Exception e)
{
AlertDialog.Builder dialogo = new AlertDialog.Builder(contexto);
dialogo.setMessage(e.getMessage());
dialogo.setPositiveButton("Okey", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialogo.create();
dialogo.show();
}
}
public void entrarxD()
{
try {
query = estado.executeQuery("SELECT * FROM tabla WHERE usuario = '" + usuario.getText().toString() + "'");
query.first();
while (query.next())
{
usuarioAchequear = query.getString("usuario");
contraAChequear = query.getString("contra");
}
AlertDialog.Builder dialogo = new AlertDialog.Builder(contexto);
dialogo.setMessage("Usuario:" + usuarioAchequear + " Contraseña:" + contraAChequear);
dialogo.setPositiveButton("Okey", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialogo.create();
dialogo.show();
}catch (Exception e)
{
AlertDialog.Builder dialogo = new AlertDialog.Builder(contexto);
dialogo.setMessage(e.getMessage());
dialogo.setPositiveButton("Okey", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialogo.create();
dialogo.show();
}
}
}
答案 0 :(得分:0)
请将您的if
条件更改为以下内容:
if (usuario.getText().toString().equal(usuarioAchequear)){
//some code here
}
答案 1 :(得分:0)
使用字符串,您必须使用milliseconds TimeStamp(int mm, int dd, int HH, int MM, int SS, int yyyy) {
tm ttm = tm();
ttm.tm_year = yyyy - 1900; // Year since 1900
ttm.tm_mon = mm - 1; // Month since January
ttm.tm_mday = dd; // Day of the month [1-31]
ttm.tm_hour = HH; // Hour of the day [00-23]
ttm.tm_min = MM;
ttm.tm_sec = SS;
time_t ttime_t = mktime(&ttm);
system_clock::time_point time_point_result = std::chrono::system_clock::from_time_t(ttime_t);
milliseconds now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_point_result).time_since_epoch();
return now_ms;
}
milliseconds getRandomTimestamp(int mm_1, int dd_1, int HH_1, int MM_1, int SS_1, int yyyy_1,
int mm_2, int dd_2, int HH_2, int MM_2, int SS_2, int yyyy_2, int N) {
milliseconds lowerRangeBound = fixedTimeStamp(mm_1, dd_1, HH_1, MM_1, SS_1, yyyy_1);
milliseconds upperRangeBound = fixedTimeStamp(mm_2, dd_2, HH_2, MM_2, SS_2, yyyy_2);
long lowerRange_ = lowerRangeBound.count();
long upperRange_ = upperRangeBound.count();
//long output = rand() % (upperRange_ - lowerRange_ + 1) + lowerRange_;
// rand() replaced after @Jarod42's suggestion.
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(lowerRange_, upperRange_);
long output = distribution(generator);
std::chrono::duration<long> dur(output);
return dur;
}
方法:
equals()
答案 2 :(得分:0)
你应该comapare值
if (usuario.getText().toString().equals(usuarioAchequear))
{
//Some codes
}
而不是比较参考
usuario.getText().toString().equals(usuarioAchequear)
在此处进一步阅读:HERE