· 8 years ago · Mar 17, 2018, 07:50 PM
1package de.phil.mysql;
2
3import org.bukkit.Bukkit;
4import org.bukkit.configuration.file.YamlConfiguration;
5import org.bukkit.entity.Player;
6
7import java.io.*;
8import java.sql.*;
9
10/* created by fphil */
11
12public class MySQLManager {
13
14 public static String database;
15 private static Connection con;
16 private static String host;
17 private static String port;
18 private static String username;
19 private static String password;
20
21 public static boolean isConnected ( ) {
22 return con != null;
23 }
24
25 public static void createfile ( ) {
26
27 File file = new File ( "plugins//BeastAPI//mysql//mysql.yml" );
28 YamlConfiguration cfg = YamlConfiguration.loadConfiguration ( file );
29
30 if ( ! file.exists ( ) ) {
31
32 cfg.set ( "host" , "localhost" );
33 cfg.set ( "port" , "3306" );
34 cfg.set ( "database" , "database" );
35 cfg.set ( "username" , "root" );
36 cfg.set ( "password" , "password" );
37
38 try {
39 cfg.save ( file );
40 } catch ( IOException e ) {
41 e.printStackTrace ( );
42 }
43
44 }
45
46 host = cfg.getString ( "host" );
47 port = cfg.getString ( "port" );
48 database = cfg.getString ( "database" );
49 username = cfg.getString ( "username" );
50 password = cfg.getString ( "password" );
51
52
53 }
54
55 public static void connect ( ) {
56 if ( ! isConnected ( ) ) {
57 try {
58 con = DriverManager.getConnection ( "jdbc:mysql://" +
59 host + ":" + port + "/" + database + "?autoReconnect=true" , username ,
60 password );
61 System.out.println ( "CONNECTED" );
62 } catch ( SQLException ex ) {
63
64 Bukkit.getConsoleSender ( ).sendMessage ( database );
65 Bukkit.getConsoleSender ( ).sendMessage ( "§cES IST EIN FEHLER MIT DER MYSQL VERBINDUNG AUFGETRETEN , MEHR DARÜBER IN DEN LOGS!" );
66 File file = new File ( "plugins//BeastAPI//mysql//logs//" + System.currentTimeMillis ( ) + ".yml" );
67 YamlConfiguration cfg = YamlConfiguration.loadConfiguration ( file );
68
69 Writer writer = new StringWriter ( );
70 ex.printStackTrace ( new PrintWriter ( writer ) );
71 String error = writer.toString ( );
72
73 cfg.set ( "ERROR" , error );
74
75 if ( ! file.exists ( ) ) {
76
77 try {
78 cfg.save ( file );
79 } catch ( IOException e ) {
80 e.printStackTrace ( );
81 }
82
83 }
84
85 }
86 }
87 }
88
89 public static void disconnect ( ) {
90 try {
91 if ( con != null ) {
92 con.close ( );
93 System.out.println ( "DISCONNECT" );
94 }
95 } catch ( SQLException ex ) {
96 ex.printStackTrace ( );
97 }
98 }
99
100 public static void update ( String qry ) {
101 try {
102 Statement st = con.createStatement ( );
103 st.executeUpdate ( qry );
104 st.close ( );
105 } catch ( SQLException ex ) {
106 connect ( );
107 ex.printStackTrace ( );
108 }
109 }
110
111 public static ResultSet query ( String qry ) {
112 ResultSet rs = null;
113 try {
114 Statement st = con.createStatement ( );
115 rs = st.executeQuery ( qry );
116 } catch ( SQLException ex ) {
117 connect ( );
118 ex.printStackTrace ( );
119 }
120 return rs;
121 }
122
123 public static void createTable ( String table , String data ) {
124 update ( "CREATE TABLE IF NOT EXISTS " + table + "(" + data + ");" );
125 }
126
127 public static void insert ( String table , Player player , String points ) {
128
129
130 update ( "INSERT INTO " + table + " (uuid,name,points) VALUES ('" + player.getPlayer ( ).getUniqueId ( ).toString ( ) + "','" + player.getPlayer ( ).getName ( ) + "','" + points + "');" );
131
132
133 }
134
135 public static void update ( Player player , String table , String what , String value ) {
136
137 MySQLManager.update ( "UPDATE " + table + " SET " + what + "='" + value + "' WHERE UUID='" + player.getUniqueId ( ).toString ( ) + "';" );
138
139 }
140
141 public static boolean exists ( String uuid , String table ) {
142 try {
143 ResultSet rs = MySQLManager.query ( ( "SELECT * FROM " + table + " WHERE UUID= '" + uuid + "'" ) );
144 if ( rs.next ( ) )
145 return rs.getString ( "UUID" ) != null;
146 return false;
147 } catch ( SQLException e ) {
148 e.printStackTrace ( );
149 }
150 return false;
151 }
152
153 public static String get ( String uuid , String what , String get , String table ) {
154 try {
155 ResultSet rs = MySQLManager.query ( ( "SELECT * FROM " + table + " WHERE " + what + "= '" + uuid + "'" ) );
156 if ( rs.next ( ) )
157 return rs.getString ( get );
158
159 } catch ( SQLException e ) {
160 e.printStackTrace ( );
161 }
162 return uuid;
163 }
164}