Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.rexlNico.GameClient.Api;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JOptionPane;
- public class MySQLConnection{
- private static Connection con = null;
- private String dbHost = "****"; // Hostname
- private String dbPort = "****"; // Port -- Standard: 3306
- private String dbName = "****"; // Datenbankname
- private String dbUser = "****"; // Datenbankuser
- private String dbPass = "****"; // Datenbankpasswort
- public MySQLConnection(){
- try {
- Class.forName("com.mysql.jdbc.Driver"); // Datenbanktreiber für JDBC Schnittstellen laden.
- // Verbindung zur JDBC-Datenbank herstellen.
- con = DriverManager.getConnection("jdbc:mysql://"+dbHost+":"+ dbPort+"/"+dbName+"?"+"user="+dbUser+"&"+"password="+dbPass);
- } catch (ClassNotFoundException e) {
- System.out.println("Treiber nicht gefunden");
- } catch (SQLException e) {
- System.out.println("Verbindung nicht moglich");
- System.out.println("SQLException: " + e.getMessage());
- System.out.println("SQLState: " + e.getSQLState());
- System.out.println("VendorError: " + e.getErrorCode());
- }
- }
- private static Connection getInstance(){
- if(con == null)
- new MySQLConnection();
- return con;
- }
- public static boolean hasOwnServer(int userID){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select userID from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return true;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean loginUser(String username, String pw){
- if(username == null || username.equals("")){
- return false;
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select username, password from clientSaveUsers where username = '"+username+"' and password = '"+pw+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getString("username") != null){
- if(res.getString("password") != null){
- return true;
- }
- }
- return false;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean getClientVersion(){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select version from Clientversion where version = '"+Var.VERSION+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getString("version") != null){
- return false;
- }else{
- new update();
- return true;
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static int getUserID(String username){
- if(username == null || username.equals("")){
- return 0;
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select username, userID from clientSaveUsers where username = '"+username+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return res.getInt("userID");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return -1;
- }
- public static int getServerOnlinePlayers(int userID){
- String OvM = "";
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select players from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return res.getInt("players");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return 0;
- }
- public static int getServerMaxPlayers(int userID){
- String OvM = "";
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select slots from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return res.getInt("slots");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return 0;
- }
- public static int getServerStatus(int userID){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select status from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return res.getInt("status");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return 0;
- }
- public static boolean canServerUpdateStatus(int userID){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select ready from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getInt("ready") == 1){
- return true;
- }else{
- return false;
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean isUserIDUsed(int userID){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select userID from clientSaveUsers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return true;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean isServerPortUsed(int port){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select port from OwnServers where port = '"+port+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return true;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean isSaveLogin(String username){
- if(username == null || username.equals("")){
- return false;
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select username, isenable from clientSaveUsers where username = '"+username+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getString("username") != null){
- if(res.getString("isenable") != null){
- if(res.getInt("isenable") == 0){
- return false;
- }else{
- return true;
- }
- }
- }
- return false;
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean isUserRegistered(String username){
- if(username == null || username.equals("")){
- return false;
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select username from clientSaveUsers where username = '"+username+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getString("username") != null){
- return true;
- }else{
- return false;
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean isServernameUsed(String servername){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select servername from OwnServers where servername = '"+servername+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getString("username") != null){
- return true;
- }else{
- return false;
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean isMailUsed(String mail){
- if(mail == null || mail.equals("")){
- return false;
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select mail from clientMails where mail = '"+mail+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getString("mail") != null){
- return true;
- }else{
- return false;
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean isServerInBeta(int userID){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select beta from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getInt("beta") == 1){
- return true;
- }else{
- return false;
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return true;
- }
- public static boolean isServerInWartung(int userID){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select wartung from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getInt("wartung") == 1){
- return true;
- }else{
- return false;
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return true;
- }
- public static boolean isServerPremium(int userID){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select premium from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- if(res.getInt("premium") == 1){
- return true;
- }else{
- return false;
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean createAccount(String username, String password, String mail){
- if(username == null || username.equals("") || password == null || password.equals("") || mail == null || mail.equals("")){
- return false;
- }
- boolean b = true;
- String ids = "";
- int id = 0;
- while(b == true){
- id = new Random().nextInt(9);
- ids = id+"";
- id = new Random().nextInt(9);
- ids += id;
- id = new Random().nextInt(9);
- ids += id;
- id = new Random().nextInt(9);
- ids += id;
- id = new Random().nextInt(9);
- ids += id;
- id = new Random().nextInt(9);
- ids += id;
- id = new Random().nextInt(9);
- ids += id;
- id = new Random().nextInt(9);
- ids += id;
- System.out.println(ids);
- if(!isUserIDUsed(Integer.parseInt(ids))){
- b = false;
- }
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("INSERT INTO clientMails (mail) VALUES ('"+mail+"')");
- ps.execute();
- ps = con.prepareStatement("INSERT INTO clientSaveUsers (username, isenable, mail, password, userID, changes, premium) VALUES ('"+username+"', '0', '"+mail+"', '"+password+"', '"+Integer.parseInt(ids)+"', '1', '0')");
- ps.execute();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static boolean createServer(int userID, String username, String Servername, String motd, boolean PvP, boolean PvE){
- boolean b = true;
- String port = "";
- int id = 0;
- while(b == true){
- id = new Random().nextInt(9);
- port = id+"";
- id = new Random().nextInt(9);
- port += id;
- id = new Random().nextInt(9);
- port += id;
- id = new Random().nextInt(9);
- port += id;
- id = new Random().nextInt(9);
- port += id;
- if(!isServerPortUsed(Integer.parseInt(port))){
- b = false;
- }
- }
- int pvp = 0;
- int pve = 0;
- if(PvP) pvp = 1;
- if(PvE) pve = 1;
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("INSERT INTO OwnServers (userID, username, servername, port, motd, slots, status, PvP, PvE, players, premium, wartung, beta, ready) VALUES ('"+userID+"', '"+username+"', '"+Servername+"', '"+Integer.parseInt(port)+"', '"+motd+"', '60', '0', '"+pvp+"', '"+pve+"', '0', '0', '1', '0', '0')");
- ps.execute();
- ps = con.prepareStatement("CREATE TABLE IF NOT EXISTS `Server-"+username+"` (userID int(10), username varchar(15), owner int(1), headadmin int(1), admin int(1), premium int(1))");
- ps.execute();
- ps = con.prepareStatement("INSERT INTO `Server-"+username+"` (userID, username, owner, headadmin, admin, premium) VALUES ('"+userID+"', '"+username+"', '1', '0', '0', '0')");
- ps.execute();
- ps = con.prepareStatement("INSERT INTO `Server-"+username+"` (userID, username, owner, headadmin, admin, premium) VALUES ('"+getUserID("rexlNico")+"', 'rexlNico', '0', '1', '0', '0')");
- ps.execute();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return false;
- }
- public static String getPassword(String username){
- if(username == null || username.equals("")){
- return "ERROR >> Kein Username";
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select username, password from clientSaveUsers where username = '"+username+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return res.getString("password");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return "ERROR >> MYSQL";
- }
- public static int getNameChanges(String username){
- if(username == null || username.equals("")){
- return 0;
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select username, changes from clientSaveUsers where username = '"+username+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return res.getInt("changes");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return 0;
- }
- public static String getMail(String username){
- if(username == null || username.equals("")){
- return "ERROR >> Kein Username";
- }
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select username, mail from clientSaveUsers where username = '"+username+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return res.getString("mail");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return "ERROR >> MYSQL";
- }
- public static String getServerMotd(int userID){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("Select motd from OwnServers where userID = '"+userID+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return res.getString("motd");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- }
- return "ERROR >> MYSQL";
- }
- public static void setSaveLogin(String username, boolean trueoderfalse){
- if(username == null || username.equals("")){
- return;
- }
- PreparedStatement ps;
- try {
- if(trueoderfalse){
- ps = con.prepareStatement("UPDATE clientSaveUsers SET isenable = '1' where username = '"+username+"'");
- ps.execute();
- }else{
- ps = con.prepareStatement("UPDATE clientSaveUsers SET isenable = '0' where username = '"+username+"'");
- ps.execute();
- }
- } catch (SQLException e) {
- }
- }
- public static void updateServerStatus(int userID, boolean online, boolean restart){
- PreparedStatement ps;
- try {
- if(restart){
- ps = con.prepareStatement("UPDATE OwnServers SET status = '-1' where userID = '"+userID+"'");
- ps.execute();
- }else if(online){
- ps = con.prepareStatement("UPDATE OwnServers SET status = '1' where userID = '"+userID+"'");
- ps.execute();
- }else{
- ps = con.prepareStatement("UPDATE OwnServers SET status = '0' where userID = '"+userID+"'");
- ps.execute();
- }
- ps = con.prepareStatement("UPDATE OwnServers SET ready = '0' where userID = '"+userID+"'");
- ps.execute();
- } catch (SQLException e) {
- }
- }
- public static void setUsername(String usernameOld, String usernameNew){
- if(usernameOld == null || usernameOld.equals("") || usernameNew == null || usernameNew.equals("")){
- return;
- }
- PreparedStatement ps;
- try {
- if(!isUserRegistered(usernameNew)){
- ps = con.prepareStatement("UPDATE clientSaveUsers SET username = '"+usernameNew+"' where username = '"+usernameOld+"'");
- ps.execute();
- Var.USERNAME = usernameNew;
- }
- } catch (SQLException e) {
- }
- }
- @SuppressWarnings("resource")
- public static void updateServerBetaAndWartung(int userID, boolean beta, boolean wartung){
- PreparedStatement ps;
- try {
- if(beta){
- ps = con.prepareStatement("UPDATE OwnServers SET beta = '1' where userID = '"+userID+"'");
- ps.execute();
- }else{
- ps = con.prepareStatement("UPDATE OwnServers SET beta = '0' where userID = '"+userID+"'");
- ps.execute();
- }
- if(wartung){
- ps = con.prepareStatement("UPDATE OwnServers SET wartung = '1' where userID = '"+userID+"'");
- ps.execute();
- }else{
- ps = con.prepareStatement("UPDATE OwnServers SET wartung = '0' where userID = '"+userID+"'");
- ps.execute();
- }
- } catch (SQLException e) {
- }
- }
- public static void setPassword(String username, String password){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("UPDATE clientSaveUsers SET password = '"+password+"' where username = '"+username+"'");
- ps.execute();
- Var.PASSWORD = password;
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- public static void updateServerMotd(int userID, String motd){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("UPDATE OwnServers SET motd = '"+motd+"' where userID = '"+userID+"'");
- ps.execute();
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- public static void setMail(String username, String mail){
- PreparedStatement ps;
- try {
- if(isMailUsed(mail)){
- return;
- }
- ps = con.prepareStatement("UPDATE clientSaveUsers SET mail = '"+mail+"' where username = '"+username+"'");
- ps.execute();
- ps = con.prepareStatement("DELETE FROM clientMails where mail = '"+mail+"'");
- ps.execute();
- ps = con.prepareStatement("INSERT INTO clientMails (mail) VALUES ('"+mail+"')");
- ps.execute();
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- public static void updateServerAdmin(String username, String playerName, boolean trueorefalse){
- PreparedStatement ps;
- if(username.equals(playerName)) return;
- if(trueorefalse){
- try {
- ps = con.prepareStatement("Select userID from `Server-"+username+"` where userID = '"+getUserID(playerName)+"'");
- ResultSet res = ps.executeQuery();
- if(!res.next()){
- return;
- }
- ps = con.prepareStatement("INSERT INTO `Server-"+username+"` (userID, username, owner, headadmin, admin, premium) VALUES ('"+getUserID(playerName)+"', '"+playerName+"', '0', '0', '1', '0')");
- ps.execute();
- }catch (Exception e) {
- System.out.println(e);
- }
- }else{
- try {
- ps = con.prepareStatement("Select userID from `Server-"+username+"` where userID = '"+getUserID(playerName)+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return;
- }
- ps = con.prepareStatement("INSERT INTO `Server-"+username+"` (userID, username, owner, headadmin, admin, premium) VALUES ('"+getUserID(playerName)+"', '"+playerName+"', '0', '0', '0', '0')");
- ps.execute();
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- }
- public static void updateServerPremium(String username, String playerName, boolean trueorefalse){
- PreparedStatement ps;
- if(username.equals(playerName)) return;
- if(trueorefalse){
- try {
- ps = con.prepareStatement("Select userID from `Server-"+username+"` where userID = '"+getUserID(playerName)+"'");
- ResultSet res = ps.executeQuery();
- if(!res.next()){
- return;
- }
- ps = con.prepareStatement("INSERT INTO `Server-"+username+"` (userID, username, owner, headadmin, admin, premium) VALUES ('"+getUserID(playerName)+"', '"+playerName+"', '0', '0', '0', '1')");
- ps.execute();
- }catch (Exception e) {
- System.out.println(e);
- }
- }else{
- try {
- ps = con.prepareStatement("Select userID from `Server-"+username+"` where userID = '"+getUserID(playerName)+"'");
- ResultSet res = ps.executeQuery();
- if(res.next()){
- return;
- }
- ps = con.prepareStatement("INSERT INTO `Server-"+username+"` (userID, username, owner, headadmin, admin, premium) VALUES ('"+getUserID(playerName)+"', '"+playerName+"', '0', '0', '0', '0')");
- ps.execute();
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- }
- public static void setNameChanges(String username, int changes){
- PreparedStatement ps;
- try {
- ps = con.prepareStatement("UPDATE clientSaveUsers SET changes = '"+changes+"' where username = '"+username+"'");
- ps.execute();
- }catch (Exception e) {
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement