Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

/*

* 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 javaapplication1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
*
* @author Kratos
*/
public class JavaApplication1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here*
try {
String url = "jdbc:mysql://localhost:3306/dbbonus";
//charger le pilote
Class.forName("com.mysql.jdbc.Driver");
//connexion a la BD
Connection connection = (Connection) DriverManager.getConnection(url,
"root", "");
System.out.println("Connexion est réussie");
//Ajouter un enregistrement
int num_etudiant = 1;
int coeff = 2;
double note = 15;
String matt = "analyse";
if (note >= 0 && note <= 20) {
PreparedStatement stmt = connection.prepareStatement("insert into
notes (num_etudiant,coeff,note,matt) values (?,?,?,?)");
stmt.setInt(1, num_etudiant);
stmt.setInt(2, coeff);
stmt.setDouble(3, note);
stmt.setString(4, matt);
stmt.executeUpdate();
stmt.close();
} else {
System.out.println("note invalide");
}
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM
notes where note<10");
ResultSet resultat = stmt2.executeQuery();
while (resultat.next() == true) {
System.out.println(resultat.getInt("num_etudiant"));
System.out.println(resultat.getInt("coeff"));
System.out.println(resultat.getInt("note"));
System.out.println(resultat.getString("matt"));
}
stmt2.close();
PreparedStatement stmt3;
stmt3 = connection.prepareStatement("SELECT * FROM notes where
num_etudiant = 1");
ResultSet resultat2 = stmt3.executeQuery();
double moyenne = 0;
int co = 0;
while (resultat2.next() == true) {
moyenne = moyenne +
(resultat2.getInt("note")*resultat2.getInt("coeff"));
co = co + resultat2.getInt("coeff");
}
moyenne = moyenne / co;
System.out.println(moyenne);
connection.close();
stmt3.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

You might also like