Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. package me.quiabo.qcash;
  2.  
  3. import org.bukkit.entity.Player;
  4.  
  5. import java.sql.*;
  6.  
  7. public class MySQL {
  8.  
  9. public Connection openConnection() {
  10. try {
  11. String pass = Main.getInstance().getConfig().getString("MySQL" + ".senha");
  12. String user = Main.getInstance().getConfig().getString("MySQL" + ".user");
  13. String host = Main.getInstance().getConfig().getString("MySQL" + ".host");
  14. String port = Main.getInstance().getConfig().getString("MySQL" + ".port");
  15. String database = Main.getInstance().getConfig().getString("MySQL" + ".database");
  16. String type = Main.getInstance().getConfig().getString("MySQL" + ".type");
  17. String url = type + host + ":" + port + "/" + database;
  18. return DriverManager.getConnection(url, user, pass);
  19. } catch (SQLException e) {
  20.  
  21. }
  22. return null;
  23. }
  24.  
  25. public void addPlayer(Player p) {
  26. try {
  27. Connection con = openConnection();
  28. PreparedStatement st = con.prepareStatement("INSERT INTO cash VALUES (" + p.getName() + ",0);");
  29. st.executeUpdate();
  30. con.close();
  31. } catch (SQLException e) {
  32.  
  33. }
  34. }
  35.  
  36. public double getMoney(Player p){
  37. double value = 0;
  38. try {
  39. Connection con = openConnection();
  40. PreparedStatement st = con.prepareStatement("SELECT cash FROM qcash WHERE nome = " + p.getName() + ";");
  41. ResultSet rs = st.executeQuery();
  42. if(rs.next()){
  43. value = rs.getDouble("cash");
  44. }
  45. con.close();
  46. return value;
  47. } catch (SQLException e) {
  48.  
  49. }
  50. return value;
  51. }
  52.  
  53. public boolean hasPlayer(Player p) {
  54. try {
  55. Connection con = openConnection();
  56. PreparedStatement st = con.prepareStatement("SELECT cash FROM qcash WHERE nome = " + p.getName() + ";");
  57. ResultSet rs = st.executeQuery();
  58. boolean result = rs.next();
  59. con.close();
  60. return result;
  61. } catch (SQLException e) {
  62.  
  63. }
  64. return false;
  65. }
  66.  
  67. public void setPlayer(Player p, double cash) {
  68. try {
  69. Connection con = openConnection();
  70. PreparedStatement st = con.prepareStatement("UPDATE qcash SET cash = " + cash + " WHERE nome = " + p.getName() +";");
  71. st.executeUpdate();
  72. con.close();
  73. } catch (SQLException e) {
  74.  
  75. }
  76. }
  77.  
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement