Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. package com.matrzak.przygoda.api.database;
  2.  
  3. import com.matrzak.przygoda.api.ApiPlugin;
  4. import com.matrzak.przygoda.api.utils.DateConvert;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.scheduler.BukkitRunnable;
  8.  
  9. import java.sql.*;
  10. import java.util.logging.Level;
  11.  
  12. public class Database {
  13.  
  14. private static String host;
  15. private static String user;
  16. private static String password;
  17. private static String dbname;
  18. private static int port;
  19. public static Connection conn;
  20. private PreparedStatement sql = null;
  21. private ResultSet res = null;
  22.  
  23. public Database(String _host,String _user,String _password,String _dbname,int _port){
  24. host = _host;
  25. user = _user;
  26. password = _password;
  27. dbname = _dbname;
  28. port = _port;
  29. }
  30.  
  31. public static String getHost() { return host; }
  32.  
  33. public static String getUser() { return user; }
  34.  
  35. public static String getPassword() { return password; }
  36.  
  37. public static String getDbname() { return dbname; }
  38.  
  39. public static int getPort() { return port; }
  40.  
  41. public static void setHost(String host) {
  42. Database.host = host;
  43. }
  44.  
  45. public static void setUser(String user) {
  46. Database.user = user;
  47. }
  48.  
  49. public static void setPassword(String password) {
  50. Database.password = password;
  51. }
  52.  
  53. public static void setDbname(String dbname) {
  54. Database.dbname = dbname;
  55. }
  56.  
  57. public static void setPort(int port) {
  58. Database.port = port;
  59. }
  60.  
  61. public static Connection getConn(){
  62. return conn;
  63. }
  64.  
  65. DateConvert dc = new DateConvert();
  66. public synchronized void openConnection(){
  67. try {
  68. conn = DriverManager.getConnection("jdbc:mysql://"+getHost()+":"+getPort()+"/"+getDbname()+"?user="+getUser()+"&password="+getPassword()+"");
  69. } catch (SQLException ex){
  70. Bukkit.getLogger().log(Level.INFO,"Wystapil problem z polaczeniem do bazy danych!");
  71. ApiPlugin.getInstance().logToFile(dc.onComp(" Api nie dalo rady polaczyc sie z baza danych powod: "+ex.getMessage()));
  72. }
  73. }
  74.  
  75. public synchronized void createTables(){
  76. try{
  77. sql = getConn().prepareStatement("CREATE TABLE IF NOT EXISTS `user` ( `id` INT(90) NOT NULL AUTO_INCREMENT , `uuid` VARCHAR(90) NOT NULL , `money` INT(90) NOT NULL , `exp` INT(90) NOT NULL , PRIMARY KEY (`id`));");
  78. sql.execute();
  79. } catch (SQLException ex){
  80. Bukkit.getLogger().log(Level.INFO,"Wystapil blad w kwerendzie");
  81. ApiPlugin.getInstance().logToFile(dc.onComp(" Blad kwerendy: "+ex.getMessage()));
  82. } finally {
  83. try { if (sql != null) sql.close(); } catch (Exception e) {};
  84. }
  85. }
  86.  
  87.  
  88. public synchronized void updateConnection(){
  89. new BukkitRunnable(){
  90. @Override
  91. public void run() {
  92. openConnection();
  93. }
  94. }.runTaskTimer(ApiPlugin.getInstance(),72000,72000);
  95. }
  96.  
  97. public synchronized void setMoneyBalance(Player p, int moneytoset){
  98. try{
  99. sql = getConn().prepareStatement("SELECT * FROM user WHERE uuid=?");
  100. sql.setObject(1,p.getUniqueId());
  101. res = sql.executeQuery();
  102. if(res.next()){
  103. int getmoney = res.getInt(3);
  104. sql = getConn().prepareStatement("INSERT INTO `user` (`id`,`uuid`,`money`) VALUES (NULL,?,?)");
  105. sql.setObject(1,p.getUniqueId());
  106. sql.setInt(2,getmoney+moneytoset);
  107. } else {
  108. }
  109. } catch (SQLException ex){
  110. Bukkit.getLogger().log(Level.INFO,"Wystapil blad w kwerendzie");
  111. ApiPlugin.getInstance().logToFile(dc.onComp(" Blad kwerendy: "+ex.getMessage()));
  112. } finally {
  113. try { if (sql != null) sql.close(); } catch (Exception e) {};
  114. }
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement