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 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
private String repas;
private String boisson;
private String dessert;
private String query;
private int idRepas;
private int idBoisson;
private int idDessert;
private int idFacture;
private int nombreRepas;
private int nombreBoisson;
private int nombreDessert;
private double prix;
private double prixFacture;
repas = choiceRepas.getSelectedItem();
boisson = choiceBoisson.getSelectedItem();
dessert = choiceDessert.getSelectedItem();
nombreRepas = Integer.parseInt(nombreChoiceRepas.getSelectedItem());
nombreBoisson = Integer.parseInt(nombreChoiceBoisson.getSelectedItem());
nombreDessert = Integer.parseInt(nombreChoiceDessert.getSelectedItem());
idRepas = retourneId(repas, "Repas");
idBoisson = retourneId(boisson, "Boisson");
idDessert = retourneId(dessert, "Dessert");
prix = retournePrix(repas, "Repas")*nombreRepas;
System.out.println("Prix du repas : " + prix);
prix = prix+(retournePrix(boisson, "Boisson")*nombreBoisson);
System.out.println("Prix du repas et de la boisson : " + prix);
prix = prix+(retournePrix(dessert, "Dessert")*nombreDessert);
public float retournePrix(String nom, String categorie)
{
String nomAliment = nom;
String categorieAliment = categorie;
float prix = 0;
String sql = "";
if (categorieAliment == "Boisson")
{
sql = "SELECT * FROM Boisson WHERE nom = '" + nomAliment + "';";
}else if (categorieAliment == "Repas")
{
sql = "SELECT * FROM Repas WHERE nom = '" + nomAliment + "';";
}else
{
sql = "SELECT * FROM Dessert WHERE nom = '" + nomAliment + "';";
}
final String driver ="com.mysql.jdbc.Driver";
final String url = "jdbc:mysql://127.0.0.1/***";
final String user = "***";
final String passwd = "***";
Connection con = null;
Statement state = null;
ResultSet result = null;
try
{
// Chargement du driver
Class.forName(driver).newInstance();
// Connexion � la BDD
con = DriverManager.getConnection(url, user, passwd);
// Creation de l'objet Statement
state = con.createStatement();
// Execution de la requete
// rs contient le r�sultat de la requete SQL
result = state.executeQuery(sql);
System.out.println(sql);
while (result.next())
{
prix = result.getFloat("prix");
}
// Fermeture des objets utilis�s
result.close();
state.close();
}
catch(ClassNotFoundException e)
{
System.err.println("Class non trouvée " + driver);
}
catch (SQLException e)
{
System.err.println("SQL erreur : " + sql + " " + e.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (con != null)
{
con.close();
}
}
catch (Exception e)
{
System.err.println(e);
}
}
return prix;
} |
Partager