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
| @Override
public void actionPerformed(ActionEvent evt) {
if(evt.getSource()==b1){
Connection laConnection;
ResultSet resultat = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
laConnection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/application?useSSL=false","boutheina","30071986");
int x= Integer.parseInt(idp.getText());
String sql="select Id_prod from produit where Id_prod=x";
try {
Statement stmt = (Statement) laConnection.createStatement();
resultat = stmt.executeQuery(sql);
if ( resultat.next() ) {
System.out.println("l'indentifiant de produit existe déjà");
}
else{
String sql1 = "INSERT INTO produit (Id_prod, designation, quantite,prix) VALUES(?,?,?,?)";
try( PreparedStatement preparedStatement = (PreparedStatement) laConnection.prepareStatement(sql1) ) { //préparation de la requête
preparedStatement.setObject(1,x); //on assigne le login au premier paramètre
preparedStatement.setObject(2, desi.getText());
int z= Integer.parseInt(qts.getText());
preparedStatement.setObject(3, z);
float y= Float.parseFloat(pri.getText());
preparedStatement.setObject(4, y);
if(preparedStatement.executeUpdate()!=1){
JOptionPane.showMessageDialog(null, "An error appears in the inscription",
"Error Message",
JOptionPane.ERROR_MESSAGE);}
else{JOptionPane.showMessageDialog(null, "The inscription is done successfully",
"Information Message",
JOptionPane.INFORMATION_MESSAGE);
idp.setText("");
desi.setText("");
pri.setText("");
qts.setText("");
idp.requestFocus();
}
}
catch (SQLException e) {
System.out.println("erreur dans la requete select");
}
}
}
catch
(Exception e)
{
System.out.print("impossible de se connecter à la base");
}
}
catch
(Exception e)
{
System.out.print("impossible de se connecter à la base");
}
}
if(evt.getSource()==b2){
System.exit(0);
}
}
} |
Partager