Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. public class Connector {
  2.  
  3. private static Connection connection; //permet de récupérer la connexion
  4. public static String //ne pas toucher
  5.  
  6. url_base = "jdbc:mysql://";
  7.  
  8. public static Connection getConnection(){
  9. return connection;
  10. }
  11.  
  12.  
  13. public static boolean isConnected(){ //vérifier si la connexion est établie
  14. try {
  15. if((connection == null) || (connection.isClosed())){
  16. return false;
  17. }else{
  18. return true;
  19. }
  20. } catch (SQLException e) {
  21. e.printStackTrace();
  22. }
  23. return false;
  24. }
  25.  
  26. public static void connect(Practice practice){
  27. if(!isConnected()){
  28. try {
  29. connection = DriverManager.getConnection(url_base+practice.sqlIdentifiers.getHost()+"/"+practice.sqlIdentifiers.getDtb(), practice.sqlIdentifiers.getUser(), practice.sqlIdentifiers.getPass()); //remplacer getUser par ton utilsateur et getpass par ton password
  30. System.out.println("Connexion base de données réussite.");
  31. } catch (SQLException e) {
  32. Practice.instance.getLogger().severe("[URGENT] La connexion à la base de données est impossible. Le serveur ne peut pas démarrer.");
  33. e.printStackTrace();
  34. Bukkit.getServer().shutdown();
  35. }
  36. }
  37. }
  38.  
  39. public static void disconnect(){ // se déconnecter
  40. if(isConnected()){
  41. try {
  42. connection.close();
  43. System.out.println("Déconnexion à la base de donnée réussite.");
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement