Bonjour à tous,
J'aurai besoin d'aide sur ma vérification d'un champ dans une table de ma BDD. Je m'explique :
Je suis en train de réaliser une application, l'utilisateur peut enregistrer une section(idSection, idRole, idChambre). idSection est la clé primaire. Ce que je souhaite faire, c'est récupérer la valeur de idSection, et s'il elle existe déjà dans ma BDD, lui renvoyer une erreur. J'ai essayé avec un IF EXIST, mais je n'ai pas trop compris la syntaxe et ça n'a pas fonctionné. Le code me génère une erreur :"0001" est la valeur que j'ai rentré dans le formulaire. Je voulais donc savoir si c'était possible de récupérer le java.sql.SQLIntegrityConstraintViolationException et faire en sorte de dire que SI cette exception est générée, dans ce cas lui renvoyer l'erreur ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part java.sql.SQLIntegrityConstraintViolationException: Duplicata du champ '0001' pour la clef 'PRIMARY'
Voici ma fonction enregistrerSection :
Voici mon section.fxml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public static void enregistrerSection(ActionEvent event, int idSection, int idRole, int idChambre) throws SQLException { Connection connection = null; PreparedStatement preparedStatement1 = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/le_pigeonnier", "root", "root"); preparedStatement1 = connection.prepareStatement("SELECT COUNT(*) FROM section WHERE idSection = ?"); preparedStatement = connection.prepareStatement("INSERT INTO section(idSection, idRole, idChambre) VALUES (?,?,?)"); preparedStatement1.setInt(1, idSection); preparedStatement.setInt(1, idSection); preparedStatement.setInt(2, idRole); preparedStatement.setInt(3, idChambre); resultSet = preparedStatement1.executeQuery(); int resultQuery = preparedStatement.executeUpdate(); if(resultSet != null){ if(resultQuery !=0){ System.out.print("Section ajouté."); Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Insertion confirmée"); alert.setHeaderText("Confirmation"); alert.setContentText("La section a bien été enregistré."); alert.show(); changeScene(event, "/fxml/section/section_personnel.fxml", "Gestion des sections personnel", ""); } else{ System.out.print("Section non ajoutée"); Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Erreur d'insertion"); alert.setHeaderText("Erreur"); alert.setContentText("Une erreur est survenue lors de l'insertion, veuillez réessayer."); alert.show(); changeScene(event, "/fxml/section/enregistrer_section.fxml", "Gestion des sections personnel", ""); } } else{ Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Insertion échouée"); alert.setHeaderText("Erreur"); alert.setContentText("La section que vous avez essayé d'enregistrer existe déjà : " + idSection); alert.show(); changeScene(event, "/fxml/section/section_personnel.fxml", "Gestion des sections personnel", ""); } }
Vous pouvez voir ci-dessus que j'ai essayé avec un SELECT COUNT(*), mais l'exception prend le dessus.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.text.*?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #032445;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="eu.hautil.pigeonnier.section.SectionPersonnelController"> <children> <Text fill="WHITE" layoutX="223.0" layoutY="32.0" strokeType="OUTSIDE" strokeWidth="0.0" text="McPigeon" underline="true" wrappingWidth="189.06884765625"> <font> <Font name="Arial" size="35.0" /> </font> </Text> <Text fill="WHITE" layoutX="201.0" layoutY="70.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Section Personnel"> <font> <Font name="Arial Bold" size="25.0" /> </font> </Text> <Button fx:id="MS_button" layoutX="150.0" layoutY="186.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" style="-fx-background-radius: 15; -fx-background-color: #FF8F1F;" text="Modifier une section" textFill="WHITE" underline="true"> <font> <Font name="Arial Bold" size="20.0" /> </font> </Button> <Button fx:id="ES_button" layoutX="150.0" layoutY="111.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" style="-fx-background-radius: 15; -fx-background-color: #139905;" text="Enregistrer une section" textFill="WHITE" underline="true"> <font> <Font name="Arial Bold" size="20.0" /> </font> </Button> <Button fx:id="SS_button" layoutX="150.0" layoutY="259.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" style="-fx-background-radius: 15; -fx-background-color: #FF0000;" text="Supprimer une section" textFill="WHITE" underline="true"> <font> <Font name="Arial Bold" size="20.0" /> </font> </Button> <Button fx:id="PS_button" layoutX="150.0" layoutY="330.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" style="-fx-background-radius: 15; -fx-background-color: #575C53;" text="Parcourir une section" textFill="WHITE" underline="true"> <font> <Font name="Arial Bold" size="20.0" /> </font> </Button> <Button fx:id="back_button" layoutX="14.0" layoutY="349.0" mnemonicParsing="false" style="-fx-background-color: #4A0101; -fx-background-radius: 15;" text="Retour" textFill="WHITE" underline="true"> <font> <Font name="Arial Bold" size="20.0" /> </font> </Button> </children> </AnchorPane>
Merci d'avance pour vos réponses !
Partager