Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.matrzak.przygoda.api.database;
- import com.matrzak.przygoda.api.ApiPlugin;
- import com.matrzak.przygoda.api.utils.DateConvert;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.scheduler.BukkitRunnable;
- import java.sql.*;
- import java.util.logging.Level;
- public class Database {
- private static String host;
- private static String user;
- private static String password;
- private static String dbname;
- private static int port;
- public static Connection conn;
- private PreparedStatement sql = null;
- private ResultSet res = null;
- public Database(String _host,String _user,String _password,String _dbname,int _port){
- host = _host;
- user = _user;
- password = _password;
- dbname = _dbname;
- port = _port;
- }
- public static String getHost() { return host; }
- public static String getUser() { return user; }
- public static String getPassword() { return password; }
- public static String getDbname() { return dbname; }
- public static int getPort() { return port; }
- public static void setHost(String host) {
- Database.host = host;
- }
- public static void setUser(String user) {
- Database.user = user;
- }
- public static void setPassword(String password) {
- Database.password = password;
- }
- public static void setDbname(String dbname) {
- Database.dbname = dbname;
- }
- public static void setPort(int port) {
- Database.port = port;
- }
- public static Connection getConn(){
- return conn;
- }
- DateConvert dc = new DateConvert();
- public synchronized void openConnection(){
- try {
- conn = DriverManager.getConnection("jdbc:mysql://"+getHost()+":"+getPort()+"/"+getDbname()+"?user="+getUser()+"&password="+getPassword()+"");
- } catch (SQLException ex){
- Bukkit.getLogger().log(Level.INFO,"Wystapil problem z polaczeniem do bazy danych!");
- ApiPlugin.getInstance().logToFile(dc.onComp(" Api nie dalo rady polaczyc sie z baza danych powod: "+ex.getMessage()));
- }
- }
- public synchronized void createTables(){
- try{
- 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`));");
- sql.execute();
- } catch (SQLException ex){
- Bukkit.getLogger().log(Level.INFO,"Wystapil blad w kwerendzie");
- ApiPlugin.getInstance().logToFile(dc.onComp(" Blad kwerendy: "+ex.getMessage()));
- } finally {
- try { if (sql != null) sql.close(); } catch (Exception e) {};
- }
- }
- public synchronized void updateConnection(){
- new BukkitRunnable(){
- @Override
- public void run() {
- openConnection();
- }
- }.runTaskTimer(ApiPlugin.getInstance(),72000,72000);
- }
- public synchronized void setMoneyBalance(Player p, int moneytoset){
- try{
- sql = getConn().prepareStatement("SELECT * FROM user WHERE uuid=?");
- sql.setObject(1,p.getUniqueId());
- res = sql.executeQuery();
- if(res.next()){
- int getmoney = res.getInt(3);
- sql = getConn().prepareStatement("INSERT INTO `user` (`id`,`uuid`,`money`) VALUES (NULL,?,?)");
- sql.setObject(1,p.getUniqueId());
- sql.setInt(2,getmoney+moneytoset);
- } else {
- }
- } catch (SQLException ex){
- Bukkit.getLogger().log(Level.INFO,"Wystapil blad w kwerendzie");
- ApiPlugin.getInstance().logToFile(dc.onComp(" Blad kwerendy: "+ex.getMessage()));
- } finally {
- try { if (sql != null) sql.close(); } catch (Exception e) {};
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement