Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package ru.jopacode.gameserver.utils;
  2.  
  3. import java.sql.CallableStatement;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9.  
  10. import ru.jopacode.gameserver.Config;
  11.  
  12.  
  13. public class DataAcess {
  14.    
  15.     private static final String MYSQL_CONNECTION_STRING =          
  16.             "jdbc:mysql://ragecode.ru/licensedata?" +
  17.             "user=root&password=njPPohV5XAFxcPrLmHtV0IKZHefgFvKx";
  18.     /*for test, in rls fix -----------------------------------------------------------------------------------------------------------------"Вот это"--*/
  19.     private static final String SELECT_QUERY = "SELECT license_ip, license_key ,expire_date from licensedata.licensedata WHERE client_name =\'%s\' ";
  20.    
  21.     public static ArrayList<String> validateUser(String username)
  22.     {
  23.         CallableStatementImp callableStatementImp = new CallableStatementImp();
  24.         ArrayList<String> list = new ArrayList<String>();
  25.         try (Connection connection = callableStatementImp.createConnection())
  26.         {
  27.              CallableStatement callableStatement = connection.prepareCall(SELECT_QUERY);
  28.             ResultSet resultSet = callableStatement.executeQuery();
  29.             if (!(resultSet == null))
  30.             {
  31.              
  32.                 while (resultSet.next())
  33.                 {
  34.                     for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++)
  35.                     {
  36.                         list.add(resultSet.getString(i));
  37.                     }
  38.                 }
  39.                 return list;
  40.             }
  41.         }
  42.         catch (SQLException e)
  43.         {
  44.             e.printStackTrace();
  45.         }
  46.         return list;
  47.     }
  48.  
  49.     private static class CallableStatementImp {
  50.        
  51.         private Connection connection;
  52.  
  53.         CallableStatementImp() {
  54.             try
  55.             {
  56.                 // Loading the driver
  57.                 Class.forName("com.mysql.jdbc.Driver");
  58.             }
  59.             catch (ClassNotFoundException e)
  60.             {
  61.                 System.out.println(e.toString());
  62.             }
  63.         }
  64.         // Creating a function to get a connection
  65.         Connection createConnection() {
  66.             // checking connection
  67.             if (connection != null) {
  68.                 System.out.println("Can't create a connection");
  69.                 return connection;
  70.             } else {
  71.                 try {
  72.                     // Getting connection
  73.                     connection = DriverManager.getConnection(MYSQL_CONNECTION_STRING);
  74.                 } catch (Exception e) {
  75.                     System.out.println(e.toString());
  76.                 }
  77.             }
  78.             return connection;
  79.         }
  80.     }
  81.    
  82.     public static void LicenseCheck()
  83.     {
  84.         boolean acess = false;
  85.         String username = Config.client_name;
  86.         ArrayList<String> userList = validateUser(username);
  87.         //0-ip, 1-key, 2-date
  88.         if (userList.get(0).equalsIgnoreCase(Config.EXTERNAL_HOSTNAME) && userList.get(1).equalsIgnoreCase(Config.client_key))
  89.         {
  90.             acess = true;
  91.         }
  92.         if (!acess)
  93.         {
  94.             System.out.println("License is not valid! Acess Forbidden!");
  95.             System.exit(1);
  96.         }
  97.        
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement