Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. import java.sql.*;
  7. import java.util.*;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. /**
  11.  *
  12.  * @author moute
  13.  */
  14. public class DataBase {
  15.  
  16.     private String server;
  17.     private String base;
  18.     private int port;
  19.     private Connection connexion;
  20.  
  21.     public DataBase(String server, int port, String base) {
  22.         this.server = server;
  23.         this.port = port;
  24.         this.base = base;
  25.     }
  26.     public boolean Connect() {
  27.         try {
  28.             Class.forName("com.mysql.jdbc.Driver");
  29.         } catch (ClassNotFoundException ex) {
  30.             Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
  31.         }
  32.  
  33.         String url = "jdbc:mysql://" + this.server + ":" + String.valueOf(this.port) + "/" + this.base;
  34.         try {
  35.             this.connexion = DriverManager.getConnection(url, "root", "root");
  36.         } catch (SQLException ex) {
  37.             Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
  38.             return false;
  39.         }
  40.         System.out.println("DBManager : Connected to the database " + this.base);
  41.         return true;
  42.     }
  43.     public void Disconnect() {
  44.         try {
  45.             this.connexion.close();
  46.             System.out.println("DBManager : Disconnected from the database " + this.base);
  47.         } catch (SQLException ex) {
  48.             Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
  49.         }
  50.     }
  51.     public void Insert(final String table) {
  52.  
  53.     }
  54.     public List<Map<String, String>> returnRow(final String table, final String... fields) {
  55.         try {
  56.             List<Map<String, String>> rows = new ArrayList<Map<String, String>>();
  57.             Statement instruction = this.connexion.createStatement();
  58.             ResultSet results = instruction.executeQuery("SELECT " + join(Arrays.asList(fields), ", ") + " FROM " + table);
  59.             while (results.next()) {
  60.                 Map<String, String> map = new HashMap<String, String>();
  61.                 for (String field : fields)
  62.                     map.put(field, results.getString(field));
  63.                 rows.add(map);
  64.             }
  65.             return rows;
  66.         } catch (SQLException ex) {
  67.             Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
  68.             return new ArrayList<Map<String, String>>();
  69.         }
  70.  
  71.     }
  72.     public static String join(Collection<String> s, String delimiter) {
  73.         if (!s.isEmpty()) {
  74.             Iterator<String> iter = s.iterator();
  75.             StringBuffer buffer = new StringBuffer(iter.next());
  76.             while (iter.hasNext()) {
  77.                 buffer.append(delimiter);
  78.                 buffer.append(iter.next());
  79.             }
  80.             return buffer.toString();
  81.         } else {
  82.             return "";}
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement