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 116 117 118
|
package com.example.dylan.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class register extends MainActivity {
String TAG= "register";
Thread thread2;
Connection connexion2;
Statement statement2;
//ResultSet resultat2;
// Variable de connexion
//String serverAddress= "192.168.1.25";
String serverAddress= "172.22.18.8";
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();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_view);
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 btnregister = (Button) findViewById(R.id.btnTest2);
btnregister.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 = "INSERT INTO login('nom','password') VALUES('"+utilisateur+"','"+motDePasse+"')";
thread2 = new Thread(runDataBase);
if (thread2 != null) {
thread2.start();
Log.i(TAG, "Thread crée " + thread2.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
{ connexion2 = DriverManager.getConnection(url, userName, passWord);
}
catch (java.sql.SQLException e)
{ handler.sendEmptyMessage(-2);
return;
}
// Création de l'objet gérant les requètes
statement2= connexion2.createStatement();
statement2.executeUpdate(requete);
Log.i(TAG, "Requête "+requete);
connexion.close();
}
catch (java.sql.SQLException e)
{ handler.sendEmptyMessage(-1);
}
}
};
private Handler handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{ switch(msg.what)
{ case 0: Toast.makeText(getBaseContext(), "enregistrement OK", Toast.LENGTH_SHORT).show();
break;
case -1:Toast.makeText(getBaseContext(), "Problème d'enregistrement", Toast.LENGTH_SHORT).show();
break;
case -2:Toast.makeText(getBaseContext(), "Erreur d'enregistrement", Toast.LENGTH_SHORT).show();
break;
default:Toast.makeText(getBaseContext(), "Erreur inconnue", Toast.LENGTH_SHORT).show();
}
};
};
} |
Partager