Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1.  
  2. public class BDD {
  3.  
  4.  
  5.     private String pwd,server,user,bdd;
  6.  
  7.     private Connection connection;
  8.  
  9.     /**
  10.      *
  11.      * @param server
  12.      * @param user
  13.      * @param pwd
  14.      * @param bdd
  15.      */
  16.     public BDD(String server, String user, String pwd, String bdd)
  17.     {
  18.         this.server = server;
  19.         this.user = user;
  20.         this.pwd = pwd;
  21.         this.bdd = bdd;
  22.     }
  23.  
  24.  
  25.     /**
  26.      *
  27.      */
  28.     public void loadLibrary()
  29.     {
  30.         try {
  31.             Class.forName("com.mysql.cj.jdbc.Driver");
  32.         }catch (ClassNotFoundException e){
  33.             System.out.println("Absence du pilote connector JDBC");
  34.         }
  35.     }
  36.  
  37.     /**
  38.      *
  39.      */
  40.     public void login()
  41.     {
  42.         this.loadLibrary();
  43.         String url = "jdbc:mysql://" + this.server + "/" + this.bdd;
  44.         try {
  45.             this.connection = DriverManager.getConnection(url,this.user,this.pwd);
  46.         } catch (SQLException e) {
  47.             System.out.println("Connection impossible à " + url);
  48.         }
  49.     }
  50.  
  51.     /**
  52.      *
  53.      */
  54.     public void logout()
  55.     {
  56.         try {
  57.             if(this.connection != null) {
  58.                 this.connection.close();
  59.             }
  60.         } catch (SQLException e) {
  61.             System.out.println("Imposible de fermer la connection");
  62.         }
  63.  
  64.     }
  65.  
  66.     /**
  67.      *
  68.      * @param table
  69.      * @param fieldValue
  70.      * @param where
  71.      * @return
  72.      */
  73.     public static String getQueryUpdateTable(String table, String[][] fieldValue, String[][] where)
  74.     {
  75.  
  76.         /*
  77.          *         String table = "table"
  78.          *         String[][]  fieldValue = {
  79.          *                 {"FIELD","ANOTHER"},
  80.          *                 {"TT","1"},
  81.          *                 {"true","false"}
  82.          *         };
  83.          *         String[][] where = {
  84.          *                 {"id","email"},
  85.          *                 {"=","LIKE"},
  86.          *                 {"1","AZ"},
  87.          *                 {"false","true"}
  88.          *         };
  89.          *
  90.          *         RETURN QUERY
  91.          *         UPDATE table SET FIELD = 'TT', ANOTHER = 1 WHERE id = 1 AND email LIKE 'AZ';
  92.          */
  93.  
  94.         String query = "UPDATE " + table + " SET ";
  95.         for(int i = 0;i< fieldValue[0].length;i++){
  96.             query += "`" + fieldValue[0][i] + "` =";
  97.             if(fieldValue[2][i] == "true"){
  98.                 query += "'" + fieldValue[1][i]+"'";
  99.             } else {
  100.                 query += "" + fieldValue[1][i]+"";
  101.             }
  102.             if (i+1 != fieldValue[0].length) {
  103.                 query += " , ";
  104.             }
  105.         }
  106.  
  107.         for(int i = 0;i<where[0].length;i++){
  108.             if(i==0){
  109.                 query = query + " WHERE `" + where[0][i] + "` ";
  110.                 if(where[3][i] == "true"){
  111.                     query += where[1][i] + " '" + where[2][i] + "'";
  112.                 } else {
  113.                     query += where[1][i] + " " + where[2][i] + "";
  114.                 }
  115.  
  116.  
  117.             } else {
  118.                 query += " AND `" + where[0][i] + "` " + where[1][i] + " '" + where[2][i]+ "'";
  119.             }
  120.         }
  121.         query += ";";
  122.         return query;
  123.     }
  124.  
  125.     public static String getQueryInsertTable(String table, String[][] fieldValue) {
  126.         String query = "INSERT INTO " + table + " (";
  127.  
  128.         for(int i = 0; i<fieldValue[0].length;i++) {
  129.             query += ("`"+fieldValue[0][i]+"` ");
  130.             if(i != fieldValue[0].length - 1) {
  131.                 query += ",";
  132.             }
  133.         }
  134.  
  135.         query += ") VALUES (";
  136.  
  137.         // INSERT INTO TABLE (field1,field2) VALUES (
  138.         for(int i=0;i<fieldValue[1].length;i++) {
  139.             if(fieldValue[2][i].equals("true")) {
  140.                 query += "'"+fieldValue[1][i] +"'";
  141.             } else {
  142.                 query += fieldValue[1][i];
  143.             }
  144.             if(i != fieldValue[1].length - 1) {
  145.                 query += ",";
  146.             }
  147.         }
  148.  
  149.         query +=");";
  150.         return query;
  151.     }
  152.  
  153.  
  154.     public Connection getConnection() {
  155.         return this.connection;
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement