122 lines
4.5 KiB
Java
122 lines
4.5 KiB
Java
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
package bdd;
|
|
|
|
import com.test.beans.Pompier;
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
|
|
/**
|
|
*
|
|
* @author clementine.desrucques
|
|
*/
|
|
public class PompierMySql {
|
|
|
|
private Connection theConnection;
|
|
private Pompier unPompier;
|
|
|
|
/**
|
|
* Constructeur
|
|
*/
|
|
public PompierMySql() {
|
|
theConnection = Connexion.getConnect("localhost", //s erveur
|
|
"sdis29", // base de données
|
|
"admin",// user
|
|
"minda"); // mot de passe ClientMysql() {
|
|
}
|
|
|
|
/**
|
|
* Creation du pompier passé en paramètre dans la table pompier
|
|
* Requête non préparée
|
|
* @param p objet de type Pompier (sans identifiant)
|
|
* @return int : id du Pompier créé
|
|
*/
|
|
public int create(Pompier p) {
|
|
int id = -1;
|
|
try {
|
|
Statement stmt = theConnection.createStatement();
|
|
int status = stmt.executeUpdate(
|
|
"INSERT INTO pompier (idCaserne, nom, prenom, statut, mail, login, mdp, adrNo, adrRue, adrCP, adrVille, grade) "
|
|
+ "VALUES ('" + p.getIdCaserne() + "', '"
|
|
+ p.getNom() + "', '"
|
|
+ p.getPrenom() + "', '"
|
|
+ p.getStatut() + "', "
|
|
+ p.getMail() + ", '"
|
|
+ p.getLogin() + ", '"
|
|
+ p.getMdp() + ", '"
|
|
+ p.getAdrNo() + ", '"
|
|
+ p.getAdrRue() + "', '"
|
|
+ p.getAdrCP() + "', '"
|
|
+ p.getAdrVille() + "', '"
|
|
+ p.getGrade()+ "' );",
|
|
Statement.RETURN_GENERATED_KEYS);
|
|
|
|
// Recherche de l'identifiant du pompier créé
|
|
if (status > 0) {
|
|
ResultSet result = stmt.getGeneratedKeys();
|
|
if (result.first()) {
|
|
id = result.getInt(1);
|
|
}
|
|
}
|
|
} catch (SQLException ex) {
|
|
System.out.println("SQLException : " + ex.getMessage());
|
|
System.out.println("SQLState : " + ex.getSQLState());
|
|
System.out.println("Code erreur : " + ex.getErrorCode());
|
|
}
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* Creation du pompier passé en paramètre dans la table pompier
|
|
* Requête préparée
|
|
* @param p objet de type Pompier (sans identifiant)
|
|
* @return int : id du pompier créé
|
|
*/
|
|
public int createRP(Pompier p) {
|
|
int id = -1;
|
|
try {
|
|
PreparedStatement stmt = null;
|
|
String sql = "INSERT INTO pompier(idCaserne, nom, prenom, statut, mail, login, mdp, adrNo, adrRue, adrCP, adrVille, grade) "
|
|
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);";
|
|
stmt = theConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
|
stmt.setInt(1, p.getIdCaserne());
|
|
stmt.setString(2, p.getNom());
|
|
stmt.setString(3, p.getPrenom());
|
|
stmt.setInt(4, p.getStatut());
|
|
stmt.setString(5,p.getMail() );
|
|
stmt.setString(6,p.getLogin() );
|
|
stmt.setString(7,p.getMdp() );
|
|
stmt.setInt(8, p.getAdrNo());
|
|
stmt.setString(9, p.getAdrRue());
|
|
stmt.setString(10, p.getAdrCP());
|
|
stmt.setString(11, p.getAdrVille());
|
|
stmt.setInt(12, p.getGrade());
|
|
|
|
System.out.println("Requête : " + stmt.toString());
|
|
int status = stmt.executeUpdate();
|
|
|
|
|
|
// Recherche de l'identifiant du client créé
|
|
if (status > 0) {
|
|
ResultSet result = stmt.getGeneratedKeys();
|
|
if (result.first()) {
|
|
id = result.getInt(1);
|
|
}
|
|
}
|
|
} catch (SQLException ex) {
|
|
System.out.println("SQLException : " + ex.getMessage());
|
|
System.out.println("SQLState : " + ex.getSQLState());
|
|
System.out.println("Code erreur : " + ex.getErrorCode());
|
|
}
|
|
return id;
|
|
}
|
|
|
|
|
|
} |