1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
public class MainActivity extends Activity
{
String TAG= "MainActivity";
Thread thread;
Connection connexion;
Statement statement;
ResultSet resultat;
// Variable de connexion
String serverAddress= "192.168.1.11";
//String serverAddress= "172.22.18.8";
//String serverAddress= "192.168.1.120";
//String serverAddress= "192.168.1.245";
//String serverAddress= "193.248.42.205/secur";
public String userName= "MaBase";
public String passWord= "Mocopoc0*";
public String dataBaseName= "mabase";
int serverPort= 3306;
String url= new String();
String requete= new String();
// Variables pour l'authentification grâce à la requête
String utilisateur= new String();
String motDePasse= new String();
//variable pour création d'activity
Intent intent;
Intent intent2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{ Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
Log.i(TAG, "driver JDBC chargé");
}
catch ( ClassNotFoundException e )
{ Log.i(TAG,"Erreur de chargement du driver JDBC"+e.getMessage());
} catch (IllegalAccessException e)
{ Log.i(TAG,"Erreur (2) de chargement du driver JDBC"+e.getMessage());
} catch (InstantiationException e)
{ Log.i(TAG,"Erreur (3) de chargement du driver JDBC"+e.getMessage());
}
Button btnTeste2= (Button)findViewById(R.id.btnTest2);
btnTeste2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent2 = new Intent(MainActivity.this,register.class);
startActivity(intent2);
}
});
Button btnTeste= (Button)findViewById(R.id.btnTest);
btnTeste.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
EditText UserName= (EditText)findViewById(R.id.editUserName);
utilisateur= UserName.getText().toString();
EditText editPassword= (EditText)findViewById(R.id.editPassword);
motDePasse= editPassword.getText().toString();
url= "jdbc:mysql://"+serverAddress+String.format(":%d/",serverPort)+dataBaseName;
requete= "SELECT password FROM login WHERE nom='"+utilisateur+"'";
thread= new Thread(runDataBase);
if(thread!=null)
{ thread.start();
Log.i(TAG, "Thread crée "+thread.isAlive());
}
else
{ Log.d(TAG, "Pb de création de Thread Connect");
}
}
});
}
private Runnable runDataBase= new Runnable()
{ public void run()
{ Log.i(TAG, "Connect start");
Log.i(TAG,"url "+url+" "+userName+" "+passWord);
try
{
try
{
connexion = DriverManager.getConnection(url, userName, passWord);
}
catch (java.sql.SQLException e)
{ handler.sendEmptyMessage(-2);
Log.i(TAG, "il y a une erreur " + e.getMessage());
Log.i(TAG, "il y a une erreur " + e.getErrorCode());
return;
}
// Création de l'objet gérant les requètes
statement= connexion.createStatement();
resultat= statement.executeQuery(requete);
Log.i(TAG, "Requête "+requete);
// Atention les colonnes démarrent à 1
if( resultat.first() )
{ if(resultat.getString(1).equals(motDePasse))
handler.sendEmptyMessage(0);
else
handler.sendEmptyMessage(-1);
}
else
handler.sendEmptyMessage(-1);
connexion.close();
}
catch (java.sql.SQLException e)
{ handler.sendEmptyMessage(-1);
}
}
}; |
Partager