Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. package me.nivcoo.Points;
  2.  
  3. /** Faites tous ces imports maintenant, vous n'aurez pas à les faire par la suite :D */
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class DataBase {
  11. private String host;
  12. private String name;
  13. private String user;
  14. private String pass;
  15. private String url;
  16. private Connection conn;
  17. public DataBase(String h, String n, String u, String p){
  18. this.host = h;
  19. this.name = n;
  20. this.user = u;
  21. this.pass = p;
  22. this.url = "jdbc:mysql://"+this.host+"/"+this.name;
  23. }
  24. public void connection(){
  25. try {
  26. Class.forName("com.mysql.jdbc.Driver"); // Ici, on appel la Class du Driver du Connecteur que nous avons importez dans notre Build Path :p
  27. } catch (ClassNotFoundException e) {
  28. e.printStackTrace();
  29. }
  30.  
  31. try {
  32. this.conn = DriverManager.getConnection(this.url, this.user, this.pass);
  33. System.out.println("[Points] La base de donnée est bien liée");
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. System.out.println("[Points] Impossible de se co a la bdd");
  37. }
  38. }
  39. public boolean connected() {
  40. return this.conn != null;
  41. }
  42.  
  43. private void connectIfNot(){
  44. if (!this.connected()) this.connection();
  45. }
  46. public void disconnection(){
  47. if(this.connected()) this.conn = null;
  48. }
  49.  
  50. public String getString(String request, int ci){
  51. this.connectIfNot();
  52. try {
  53. Statement state = this.conn.createStatement();
  54. ResultSet result = state.executeQuery(request);
  55. try {
  56. while(result.next()){
  57. return result.getString(ci);
  58. }
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. } catch (SQLException e) {
  63. e.printStackTrace();
  64. }
  65. return "NULL";
  66. }
  67. public int getInt(String request, int ci){
  68. this.connectIfNot();
  69. try {
  70. Statement state = this.conn.createStatement();
  71. ResultSet result = state.executeQuery(request);
  72. try {
  73. while(result.next()){
  74. return result.getInt(ci);
  75. }
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. } catch (SQLException e) {
  80. e.printStackTrace();
  81. }
  82. return -1;
  83. }
  84. public void sendRequest(String request){
  85. this.connectIfNot();
  86.  
  87. try{
  88. Statement state = this.conn.createStatement();
  89. state.executeUpdate(request);
  90. state.close();
  91. System.out.println("Oee! ca marche " + request);
  92. }
  93. catch(SQLException e){
  94. e.printStackTrace();
  95. System.out.println("Noo! ce marche pas " + request);
  96. }
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement