· 8 years ago · Feb 23, 2018, 09:30 PM
1package ch.lexx.sqlManager;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.SQLException;
7
8import org.bukkit.Bukkit;
9
10import ch.lexx.untils.Data;
11
12public class sqlConnection {
13
14 public void MainListener(ch.lexx.main.Main Main) {
15 this.pl = Main;
16 }
17 public static ch.lexx.main.Main pl;
18 final static String username=Data.SQLUser; //Enter in your db username
19 final static String password=Data.SQLPW; //Enter your password for the db
20 final static String url = Data.SQLurl; //Enter URL w/db name
21
22 public static Connection connection;
23 static int sqlConSch;
24 public static boolean sqlConnect() {
25 Bukkit.getConsoleSender().sendMessage("§6Starten Connection zur MySQL....");
26 try {
27 Class.forName("com.mysql.jdbc.Driver"); //this accesses Driver in jdbc.
28 } catch (ClassNotFoundException e) {
29 e.printStackTrace();
30 System.err.println("§4jdbc driver unavailable!");
31
32 Bukkit.getConsoleSender().sendMessage("§4jdbc driver unavailable!");
33 return false;
34 }
35 try { //Another try catch to get any SQL errors (for example connections errors)
36 connection = DriverManager.getConnection(url,username,password);
37 Bukkit.getConsoleSender().sendMessage("§9SQL Connection §aOK");
38 return true;
39 //with the method getConnection() from DriverManager, we're trying to set
40 //the connection's url, username, password to the variables we made earlier and
41 //trying to get a connection at the same time. JDBC allows us to do this.
42 } catch (SQLException e) { //catching errors)
43 e.printStackTrace(); //prints out SQLException errors to the console (if any)
44 Bukkit.getConsoleSender().sendMessage("§4"+username);
45 Bukkit.getConsoleSender().sendMessage("§4"+password);
46 Bukkit.getConsoleSender().sendMessage("§4"+url);
47 return false;
48 }
49 }
50
51 public static boolean sqlCloseConnection() {
52 try { //using a try catch to catch connection errors (like wrong sql password...)
53 if (connection!=null && !connection.isClosed()){ //checking if connection isn't null to
54 //avoid receiving a nullpointer
55 connection.close(); //closing the connection field variable.
56 return true;
57 }
58 return true;
59 } catch(Exception e) {
60 e.printStackTrace();
61 return false;
62 }
63 }
64 public static boolean checkSqlTables() {
65 String TableUsers = "Nope";
66 String TableUsersStats = "Nope";
67 Bukkit.getConsoleSender().sendMessage("§6Checking SQL Tables....");
68 String sql = "CREATE TABLE IF NOT EXISTS Users("
69 + "UID integer not null auto_increment primary key,"
70 + "MUser varchar(50),"
71 + "UserPassword varchar(255),"
72 + "Kills Integer default 0,"
73 + "Deads Integer default 0,"
74 + "Coins double default 200,"
75 + "OnlineStats varchar(30) default 'NEW',"
76 + "Wertung integer default 0,"
77 + "LastOnlineDate date,"
78 + "LastOnlineTime time,"
79 + "PlayTime time default 0,"
80 + "LastIp varchar(15)"
81 + ");";
82 // prepare the statement to be executed
83 try {
84 PreparedStatement stmt = connection.prepareStatement(sql);
85 // I use executeUpdate() to update the databases table.
86 stmt.executeUpdate();
87 TableUsers = "OK";
88 Bukkit.getConsoleSender().sendMessage("§6Table §bUsers §aOK");
89 } catch (SQLException e) {
90 e.printStackTrace();
91 Bukkit.getConsoleSender().sendMessage("§6Table §bUsers §4Failed");
92 return false;
93 }
94 sql = "CREATE TABLE IF NOT EXISTS UserStats(" +
95 " BID integer not null auto_increment primary key," +
96 " UID integer not null," +
97 " Status varchar(20)," +
98 " Reason text," +
99 " Datum_Bis date," +
100 " Zeit_Bis time," +
101 " Foreign key (UID) references Users(UID)" +
102 ");";
103 // prepare the statement to be executed
104 try {
105 PreparedStatement stmt = connection.prepareStatement(sql);
106 // I use executeUpdate() to update the databases table.
107 stmt.executeUpdate();
108 TableUsersStats = "OK";
109 Bukkit.getConsoleSender().sendMessage("§6Table §bUsersStats §aOK");
110 } catch (SQLException e) {
111 e.printStackTrace();
112 Bukkit.getConsoleSender().sendMessage("§6Table §bUsersStats §4Failed");
113 return false;
114 }
115 if(TableUsers == "OK" && TableUsersStats == "OK") {
116 return true;
117 }else {
118 return false;
119 }
120 }
121}