· 5 years ago · May 19, 2020, 06:54 PM
1package me.ihossam.sw.sql;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import org.bukkit.Bukkit;
9
10public class MySQL {
11 public static String username;
12
13 public static String password;
14
15 public static String database;
16
17 public static String host;
18
19 public static int port;
20
21 public static Connection con;
22
23 public static boolean isConnected() {
24 return (con != null);
25 }
26
27 public static void connect() {
28 if (!isConnected())
29 try {
30 con = DriverManager.getConnection("jdbc:mysql://" + "IP" + ":" +
31 3306 + "/" + "", "", "password");
32 Bukkit.getConsoleSender().sendMessage(
33 "connection established succesfully");
34 } catch (SQLException e) {
35 e.printStackTrace();
36 Bukkit.getConsoleSender().sendMessage(
37 "connection failed!");
38 }
39 }
40
41 public static void close() {
42 if (isConnected())
43 try {
44 con.close();
45 Bukkit.getConsoleSender().sendMessage(
46 "connection closed succesfully!");
47 } catch (SQLException e) {
48 e.printStackTrace();
49 }
50 }
51
52 public static void update(String qry) {
53 if (isConnected())
54 try {
55 PreparedStatement pt = con.prepareStatement(qry);
56 pt.executeUpdate();
57 } catch (SQLException e) {
58 e.printStackTrace();
59 }
60 }
61
62 public static ResultSet getResult(String qry) {
63 if (isConnected())
64 try {
65 PreparedStatement pt = con.prepareStatement(qry);
66 return pt.executeQuery();
67 } catch (SQLException e) {
68 e.printStackTrace();
69 return null;
70 }
71 return null;
72 }
73
74 public static void createTables() {
75 if (isConnected())
76 try {
77 con.createStatement().executeUpdate(
78 "CREATE TABLE IF NOT EXISTS FFA (Playername VARCHAR(100), UUID VARCHAR(100), Points int, Kills int, Deaths int, HStreak int)");
79 } catch (SQLException e) {
80 e.printStackTrace();
81 }
82 }
83}