Transférer les fichiers vers 'projet_maven/src/main/java'
This commit is contained in:
parent
12fbf1601f
commit
3fda4f07ff
2 changed files with 57 additions and 0 deletions
30
projet_maven/src/main/java/InsertUser.java
Normal file
30
projet_maven/src/main/java/InsertUser.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class InsertUser {
|
||||
|
||||
// /!\ PROTOTYPE A MODIFIER SUIVANT FORMAT FINAL DE LA BDD /!\
|
||||
// fonction qui insere l'user dans la bdd insa
|
||||
public static void insertUser(String nom, String user_role) {
|
||||
// pour se connecter a la bdd
|
||||
String url = "jdbc:mysql://srv-bdens.insa-toulouse.fr:3306/projet_gei_023"; // url sur moodle
|
||||
String user = "projet_gei_023"; // login
|
||||
String password = "ohQu4ood"; // mdp sur liste binomes
|
||||
|
||||
try (Connection connection = DriverManager.getConnection(url, user, password)) {
|
||||
|
||||
Statement stmt = connection.createStatement();
|
||||
|
||||
// on cree requete SQL avec entrees user
|
||||
String insertQuery = "INSERT INTO User(nom, user_role) VALUES ('" + nom + "', '" + user_role + "')";
|
||||
|
||||
// on fait la requete
|
||||
stmt.executeUpdate(insertQuery);
|
||||
System.out.println("Ligne insérée avec succès.");
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Echec de l'insertion : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
27
projet_maven/src/main/java/Main.java
Normal file
27
projet_maven/src/main/java/Main.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//on active le scanner pour recuperer les entrees user
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
try {
|
||||
// on demande nom et role de l'user
|
||||
System.out.print("Entrez votre nom d'utilisateur (100 caractères max): "); // modifier le nb de caracteres max suivant bdd
|
||||
String nom = scanner.nextLine();
|
||||
|
||||
System.out.print("Entrez votre rôle (DEMANDEUR, BENEVOLE, VALIDATEUR) : ");
|
||||
String user_role = scanner.nextLine();
|
||||
|
||||
// on insere dans la table user dans la base de donnees insa
|
||||
InsertUser.insertUser(nom, user_role);
|
||||
} finally {
|
||||
//on libere le scanner meme si l'insertion a echoue (d'ou le bloc try/finally)
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//code a changer parce que s'il y a une erreur on doit relancer la connection à chaque fois au lieu de juste redemander
|
Loading…
Reference in a new issue