Advertisement
Guest User

POULOULOU

a guest
Feb 15th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Scanner;
  3.  
  4.  
  5.  
  6. public class BDD {
  7.  
  8. public static Connection cn;
  9.  
  10. //--------------------------------------------------------------------------------
  11. //Exercice 1
  12.  
  13. public void connectBDD(){
  14. String url = "jdbc:mysql://192.168.22.48:3306/sehinger";
  15. String login = "sehinger";
  16. String pswd = "01021996";
  17.  
  18.  
  19.  
  20.  
  21. try {
  22. Class.forName ("com.mysql.jdbc.Driver");
  23. cn = DriverManager.getConnection(url,login,pswd);
  24. System.out.println("Connecté");
  25. } catch (ClassNotFoundException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. } catch (SQLException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. }
  33.  
  34.  
  35. public void createBDD() throws SQLException{
  36. java.sql.Statement st;
  37. st = cn.createStatement();
  38.  
  39. 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)";
  40. st.execute(requete);
  41.  
  42. requete = "INSERT into Produits (Designation,Quantite,Prix_HT) VALUES ('PC MM MMX', 5, 6500.00)";
  43. st.execute(requete);
  44. requete = "INSERT into Produits (Designation,Quantite,Prix_HT) VALUES ('Imprimante HP Jet', 20, 3500.00)";
  45. st.execute(requete);
  46. requete = "INSERT into Produits (Designation,Quantite,Prix_HT) VALUES ('Ecran 17\"', 15, 1200.00)";
  47. st.execute(requete);
  48. requete = "INSERT into Produits (Designation,Quantite,Prix_HT) VALUES ('CD*10', 100, 99.00)";
  49. st.execute(requete);
  50.  
  51. System.out.println("Requètes correctement executées");
  52. st.close();
  53. }
  54.  
  55. //--------------------------------------------------------------------------------
  56. //Exercice 2
  57.  
  58. public void afficherBDD() throws SQLException{ //
  59. java.sql.Statement st;
  60. st = cn.createStatement();
  61.  
  62. ResultSet resultat = st.executeQuery("SELECT * FROM Produits");
  63. while(resultat.next()){
  64. System.out.println("---------------------------");
  65. System.out.println("Reference: "+resultat.getInt("Reference"));
  66. System.out.println("Designation: "+resultat.getString("Designation"));
  67. System.out.println("Quantite: "+resultat.getInt("Quantite"));
  68. System.out.println("TVA: "+resultat.getInt("TVA"));
  69. System.out.println("Prix hors taxe: "+resultat.getInt("Prix_HT"));
  70.  
  71. }
  72. st.close();
  73. }
  74.  
  75. //--------------------------------------------------------------------------------
  76. //Exercice 3
  77.  
  78. public void afficherBDD2() throws SQLException{ //
  79. PreparedStatement pst = cn.prepareStatement("SELECT * FROM Produits WHERE Prix_HT >=?");
  80. Scanner sc = new Scanner(System.in);
  81. System.out.println("Veillez entrer le prix : ");
  82. float prix = sc.nextFloat();
  83.  
  84. pst.setFloat(1, prix);
  85.  
  86. ResultSet resultat = pst.executeQuery();
  87. while(resultat.next()){
  88. System.out.println("---------------------------");
  89. System.out.println("Reference: "+resultat.getInt("Reference"));
  90. System.out.println("Designation: "+resultat.getString("Designation"));
  91. System.out.println("Quantite: "+resultat.getInt("Quantite"));
  92. System.out.println("TVA: "+resultat.getInt("TVA"));
  93. System.out.println("Prix hors taxe: "+resultat.getInt("Prix_HT"));
  94.  
  95. }
  96. pst.close();
  97. }
  98.  
  99. //--------------------------------------------------------------------------------
  100. //Exercice 4
  101.  
  102. public void afficherRequeteQuelconque() throws SQLException{
  103. Scanner sc = new Scanner(System.in);
  104. System.out.println("Veillez entrer votre requete : ");
  105. String str = sc.nextLine();
  106. PreparedStatement pst = cn.prepareStatement(str);
  107.  
  108. switch(str.substring(0,6)){
  109. case "SELECT":
  110. ResultSet resultat =pst.executeQuery();
  111. ResultSetMetaData rsmd = pst.getMetaData();
  112. int nbC = rsmd.getColumnCount();
  113. while(resultat.next()){
  114. System.out.println("---------------------------");
  115. for(int i = 1;i<=nbC;i++){
  116. String nomCol = rsmd.getColumnName(i);
  117. System.out.println( nomCol+":"+resultat.getString(i));
  118. }
  119.  
  120. }
  121. break;
  122. default :
  123. pst.executeUpdate();
  124. System.out.println("Requete executée");
  125. break;
  126. }
  127. pst.close();
  128. }
  129.  
  130.  
  131. public static void main(String args[]) throws SQLException{
  132.  
  133. try{
  134. BDD bdd = new BDD();
  135. bdd.connectBDD(); //permet de connecter une bdd
  136. //bdd.createBDD(); //permet de creer une table et d'y inserer des données
  137. //bdd.afficherBDD(); //affiche le resultat d'une requete donnée
  138. //bdd.afficherBDD2(); //affiche le resultat d'une requete avec données variables
  139. bdd.afficherRequeteQuelconque(); // affiche le resulat de n'importe quelle requete
  140. cn.close();
  141. }catch (SQLException e) {
  142. e.printStackTrace();
  143. }
  144.  
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement