Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SQL_Project;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Utilisateur {
- public static java.util.Scanner scanner = new java.util.Scanner(System.in);
- private String url = "jdbc:postgresql://localhost:5432/postgres"; // LOCAL
- //private String url="jdbc:postgresql://172.24.2.6:5432/gcurato16"; // ECOLE
- private PreparedStatement psAjoutUtilisateur;
- private PreparedStatement psAuthentification;
- private PreparedStatement psAjoutQuestion;
- private PreparedStatement psAjoutReponse;
- private Connection conn=null;
- public Utilisateur() {
- // test driver
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Driver PostgreSQL manquant !");
- System.exit(1);
- }
- // test connection
- try {
- conn = DriverManager.getConnection(url, "gcurato16", "(9YAVaP@");
- } catch (SQLException e) {
- System.out.println("Impossible de joindre le serveur");
- System.exit(1);
- }
- try {
- psAjoutUtilisateur = conn.prepareStatement("SELECT projet.ajouterUtilisateur(?,?,?);");
- psAjoutQuestion = conn.prepareStatement("SELECT projet.ajouterQuestions(?,?,?);");
- psAjoutReponse = conn.prepareStatement("SELECT projet.ajouterReponse(?,?,?);");
- psAuthentification = conn.prepareStatement("SELECT * from projet.Utilisateur WHERE desactive = 'false'");
- }catch (SQLException se){
- System.out.println("Error, "+se.getMessage());
- System.exit(1);
- }
- }
- // Inscription phase
- private void inscriptionUtilisateur() {
- try {
- System.out.println("Veuillez trouver un nom d'utilisateur");
- String nom_utilisateur = scanner.next();
- System.out.println("Veuillez entrer votre email,");
- String email = scanner.next();
- System.out.println("votre mot de passe,");
- String mot_de_passe = scanner.next();
- psAjoutUtilisateur.setString(1,nom_utilisateur);
- psAjoutUtilisateur.setString(2,email);
- psAjoutUtilisateur.setString(3,mot_de_passe);
- ResultSet rs = psAjoutUtilisateur.executeQuery();
- while(rs.next()){
- System.out.println("Inscription bien effectuée");
- }
- }catch (SQLException se){
- System.out.println("Error, "+se.getMessage());
- System.exit(1);
- }
- }
- // authentification phase
- private void authentification() {
- System.out.println("Bonjour, veuillez-vous identifier svp:");
- System.out.println("Login:");
- String s = scanner.next();
- System.out.println("Mot de Passe:");
- String s2 = scanner.next();
- if (!authentification(conn, s, s2)){
- System.out.println("Votre login/password est incorrect ou votre compte est désactivé ");
- System.exit(1);
- }
- }
- /* authentification methode */
- /**
- * @POST: return a boolean depending on the authentification's success
- * @param connection
- * @param nom_utilisateur
- * @param mdp
- */
- public boolean authentification(Connection connection, String nom_utilisateur, String mdp){
- try (ResultSet rt = psAuthentification.executeQuery()) {
- while (rt.next()){
- if (rt.getString(4).equals(mdp) && rt.getString(2).equals(nom_utilisateur))
- return true;
- }
- } catch (SQLException se) {
- return false;
- }
- return false;
- }
- // AjouterQuestion
- public int ajouterQuestion(int num_utilisateur){
- try {
- System.out.println("Veuillez inserer le titre de votre question");
- String titre = scanner.next();
- System.out.println("Veuillez ecrire le contenu de votre question");
- String contenu = scanner.next();
- psAjoutQuestion.setString(1, titre);
- psAjoutQuestion.setString(2, contenu);
- psAjoutQuestion.setInt(3, num_utilisateur);
- ResultSet rs = psAjoutQuestion.executeQuery();
- while(rs.next()) {
- return rs.getInt(1);
- }
- }catch (SQLException se){
- System.out.println("Erreur, "+se.getMessage());
- }
- return -1;
- }
- //ajouterReponse
- public int ajouterReponse(int num_auteur, int num_question) {
- try {
- System.out.println("Veuillez entrer le contenu de votre réponse");
- String contenu = scanner.next();
- psAjoutReponse.setString(1, contenu);
- psAjoutReponse.setInt(2, num_question);
- psAjoutReponse.setInt(3, num_auteur);
- ResultSet rs = psAjoutReponse.executeQuery();
- while(rs.next()) {
- return rs.getInt(2);
- }
- }catch(SQLException se) {
- System.out.println("Erreur, "+se.getMessage());
- }
- return -1;
- }
- // fermer connection
- public void close() {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- }
- }
Add Comment
Please, Sign In to add comment