Advertisement
Ajeux

[DataBase] SqlConnection

Jan 7th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. package fr.ajeux.myplugin;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class SqlConnection {
  8.  
  9. private Connection connection;
  10. private String urlbase,host,database,user,pass;
  11.  
  12. public SqlConnection(String urlbase, String host, String database, String user, String pass) {
  13. this.urlbase = urlbase;
  14. this.host = host;
  15. this.database = database;
  16. this.user = user;
  17. this.pass = pass;
  18. }
  19.  
  20. public void connection(){
  21. if(!isConnected()){
  22. try {
  23. connection = DriverManager.getConnection(urlbase + host + "/" + database, user, pass);
  24. System.out.println("[Sql] DataBase connected");
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30.  
  31. public void disconnect(){
  32. if(isConnected()){
  33. try {
  34. connection.close();
  35. System.out.println("[Sql] DataBase disconnected");
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41.  
  42. public boolean isConnected(){
  43. return connection != null;
  44. }
  45.  
  46.  
  47.  
  48. public void createAccount(Player player){
  49. if(!hasAccount(player)){
  50. try {
  51. PreparedStatement q = connection.prepareStatement("INSERT INTO joueurs(uuid,coins,grade) VALUES (?,?,?)");
  52. q.setString(1, player.getUniqueId().toString());
  53. q.setInt(2, 100);
  54. q.setString(3, "joueur");
  55. q.execute();
  56. q.close();
  57. } catch (SQLException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62.  
  63. public boolean hasAccount(Player player){
  64. try {
  65. PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
  66. q.setString(1, player.getUniqueId().toString());
  67. ResultSet resultat = q.executeQuery();
  68. boolean hasAccount = resultat.next();
  69. q.close();
  70. return hasAccount;
  71. } catch (SQLException e) {
  72. e.printStackTrace();
  73. }
  74. return false;
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement