Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;
- public class BDD {
- public static Connection cn;
- //--------------------------------------------------------------------------------
- //Exercice 1
- public void connectBDD(){
- String url = "jdbc:mysql://192.168.22.48:3306/sehinger";
- String login = "sehinger";
- String pswd = "01021996";
- try {
- Class.forName ("com.mysql.jdbc.Driver");
- cn = DriverManager.getConnection(url,login,pswd);
- System.out.println("Connecté");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void createBDD() throws SQLException{
- java.sql.Statement st;
- st = cn.createStatement();
- String requete = "CREATE TABLE IF NOT EXISTS Produits(Reference INT(4) AUTO_INCREMENT PRIMARY KEY, Designation VARCHAR(30), Quantite INT, TVA FLOAT DEFAULT 19.6, Prix_HT FLOAT)";
- st.execute(requete);
- requete = "INSERT into Produits (Designation,Quantite,Prix_HT) VALUES ('PC MM MMX', 5, 6500.00)";
- st.execute(requete);
- requete = "INSERT into Produits (Designation,Quantite,Prix_HT) VALUES ('Imprimante HP Jet', 20, 3500.00)";
- st.execute(requete);
- requete = "INSERT into Produits (Designation,Quantite,Prix_HT) VALUES ('Ecran 17\"', 15, 1200.00)";
- st.execute(requete);
- requete = "INSERT into Produits (Designation,Quantite,Prix_HT) VALUES ('CD*10', 100, 99.00)";
- st.execute(requete);
- System.out.println("Requètes correctement executées");
- st.close();
- }
- //--------------------------------------------------------------------------------
- //Exercice 2
- public void afficherBDD() throws SQLException{ //
- java.sql.Statement st;
- st = cn.createStatement();
- ResultSet resultat = st.executeQuery("SELECT * FROM Produits");
- while(resultat.next()){
- System.out.println("---------------------------");
- System.out.println("Reference: "+resultat.getInt("Reference"));
- System.out.println("Designation: "+resultat.getString("Designation"));
- System.out.println("Quantite: "+resultat.getInt("Quantite"));
- System.out.println("TVA: "+resultat.getInt("TVA"));
- System.out.println("Prix hors taxe: "+resultat.getInt("Prix_HT"));
- }
- st.close();
- }
- //--------------------------------------------------------------------------------
- //Exercice 3
- public void afficherBDD2() throws SQLException{ //
- PreparedStatement pst = cn.prepareStatement("SELECT * FROM Produits WHERE Prix_HT >=?");
- Scanner sc = new Scanner(System.in);
- System.out.println("Veillez entrer le prix : ");
- float prix = sc.nextFloat();
- pst.setFloat(1, prix);
- ResultSet resultat = pst.executeQuery();
- while(resultat.next()){
- System.out.println("---------------------------");
- System.out.println("Reference: "+resultat.getInt("Reference"));
- System.out.println("Designation: "+resultat.getString("Designation"));
- System.out.println("Quantite: "+resultat.getInt("Quantite"));
- System.out.println("TVA: "+resultat.getInt("TVA"));
- System.out.println("Prix hors taxe: "+resultat.getInt("Prix_HT"));
- }
- pst.close();
- }
- //--------------------------------------------------------------------------------
- //Exercice 4
- public void afficherRequeteQuelconque() throws SQLException{
- Scanner sc = new Scanner(System.in);
- System.out.println("Veillez entrer votre requete : ");
- String str = sc.nextLine();
- PreparedStatement pst = cn.prepareStatement(str);
- switch(str.substring(0,6)){
- case "SELECT":
- ResultSet resultat =pst.executeQuery();
- ResultSetMetaData rsmd = pst.getMetaData();
- int nbC = rsmd.getColumnCount();
- while(resultat.next()){
- System.out.println("---------------------------");
- for(int i = 1;i<=nbC;i++){
- String nomCol = rsmd.getColumnName(i);
- System.out.println( nomCol+":"+resultat.getString(i));
- }
- }
- break;
- default :
- pst.executeUpdate();
- System.out.println("Requete executée");
- break;
- }
- pst.close();
- }
- public static void main(String args[]) throws SQLException{
- try{
- BDD bdd = new BDD();
- bdd.connectBDD(); //permet de connecter une bdd
- //bdd.createBDD(); //permet de creer une table et d'y inserer des données
- //bdd.afficherBDD(); //affiche le resultat d'une requete donnée
- //bdd.afficherBDD2(); //affiche le resultat d'une requete avec données variables
- bdd.afficherRequeteQuelconque(); // affiche le resulat de n'importe quelle requete
- cn.close();
- }catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement