· 6 years ago · Mar 11, 2019, 07:18 PM
1public static Connection con;
2private String table = "playerdata";
3private final String data = "CREATE TABLE IF NOT EXISTS `"+table+"` (`nick` VARCHAR(50), `server` TEXT)";
4
5private String host, database, username, password;
6private int port;
7
8
9public void setupMysql() {
10 try {
11 synchronized (this) {
12 if (getConnection() != null && !getConnection().isClosed()) {
13 return;
14 }
15
16 Class.forName("com.mysql.jdbc.Driver");
17 setConnection(DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database,
18 this.username, this.password));
19
20 Bukkit.getConsoleSender().sendMessage("§a[MySQL] Hooked with data-storage type: §eMySQL");
21 }
22 } catch (SQLException|ClassNotFoundException e) {
23 Bukkit.getConsoleSender().sendMessage("§cAn error ocurred while trying to connect to §eMySQL §cserver. Disabling plugin (Be sure you are setting correctly MySQL details, and you have privileges in case " +
24 "your MySQL server isnt localhost.");
25 Main.getInstance().getPluginLoader().disablePlugin(Main.getInstance());
26 e.printStackTrace();
27 }
28 }
29
30
31public void load(){
32
33 host = Main.getInstance().getConfig().getString("database.host");
34 port = Main.getInstance().getConfig().getInt("database.port");
35 database = Main.getInstance().getConfig().getString("database.database");
36 username = Main.getInstance().getConfig().getString("database.username");
37 password = Main.getInstance().getConfig().getString("database.password");
38 setupMysql();
39 try {
40 Statement s = con.createStatement();
41 s.executeUpdate(data);
42 s.close();
43 Bukkit.getConsoleSender().sendMessage("§a[MySQL] Connection to MySQL server has been succesfully hooked.");
44 } catch (SQLException e) {
45 Bukkit.getConsoleSender().sendMessage("§cAn error ocurred while performing start up actions. Disabling plugin");
46 Main.getInstance().getPluginLoader().disablePlugin(Main.getInstance());
47 e.printStackTrace();
48 }
49 }
50
51
52 public Connection getConnection() {
53 return con;
54 }
55
56 public void setConnection(Connection con) {
57 this.con = con;
58 }
59
60
61
62
63 public boolean isInDatabase(Player p) {
64 try {
65 PreparedStatement st = con.prepareStatement("SELECT nick FROM " + table + " WHERE nick=?");
66
67 st.setString(1, String.valueOf(p.getUniqueId()));
68 ResultSet result = st.executeQuery();
69 if(result.next()) {
70 result.close();
71 st.close();
72 return true;
73 }
74 st.close();
75 result.close();
76 } catch (SQLException ex) {
77 ex.printStackTrace();
78 }
79 return false;
80 }
81
82
83 public void registerPlayer(Player p) {
84 try {
85 PreparedStatement st = con.prepareStatement("INSERT INTO " + table + " VALUES (?,?)");
86 st.setString(1, p.getName());
87 st.setString(2, "serverName");
88
89 st.executeUpdate();
90
91 st.close();
92 } catch (SQLException ex) {
93 ex.printStackTrace();
94 }
95 }
96
97
98 public void update(Player p, String column, Object set) {
99 try {
100 PreparedStatement st = con.prepareStatement("UPDATE " + table + " SET " + column + " = '" + set + "' WHERE nick=?");
101 st.setString(1, p.getName());
102 st.executeUpdate();
103 st.close();
104 } catch (SQLException ex) {
105 ex.printStackTrace();
106 }
107 }
108
109
110 public Object get(Player p, String column) {
111 try {
112 PreparedStatement st = con.prepareStatement("SELECT " + column + " FROM " + table + " WHERE nick=?");
113 st.setString(1, String.valueOf(p.getUniqueId()));
114 ResultSet r = st.executeQuery();
115 if(r.next()) {
116 Object object = r.getObject(1);
117 r.close();
118 st.close();
119 return object;
120 }
121 } catch (SQLException ex) {
122 ex.printStackTrace();
123 }
124 return null;
125 }