Advertisement
XploreLP

Untitled

Dec 28th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package de.xplore.city;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class MySQL {
  10.    
  11.     public static String host = "46.20.34.169";
  12.     public static String port = "3306";
  13.     public static String database = "city";
  14.     public static String username = "TheXplore";
  15.     public static String password = "Random21";
  16.     public static Connection con;
  17.    
  18.     public static Connection connect()
  19.     {      
  20.         if(!hasConnection())
  21.         {
  22.             try
  23.             {
  24.                 con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  25.                 System.out.println("MySQL: Connected!");
  26.                 return con;
  27.             }
  28.             catch (SQLException e)
  29.             {
  30.                 System.out.println("MySQL: Cant connect!");
  31.                 e.printStackTrace();
  32.                
  33.                 return null;
  34.             }
  35.         }
  36.         return null;
  37.     }
  38.    
  39.     public static Connection getConnection()
  40.     {
  41.         if(!hasConnection())
  42.         {
  43.             return connect();
  44.         }
  45.         else
  46.             return con;
  47.     }
  48.    
  49.     public static void disconnect()
  50.     {
  51.         if(hasConnection())
  52.         {
  53.             try
  54.             {
  55.                 con.close();
  56.                 System.out.println("MySQL: Verbindung geschlossen.");
  57.             }
  58.             catch (SQLException e)
  59.             {
  60.                 e.printStackTrace();
  61.             }
  62.         }
  63.     }
  64.    
  65.     public static boolean hasConnection()
  66.     {
  67.         try
  68.         {
  69.            
  70.             if(!(con == null))
  71.             {
  72.                 if(!con.isValid(10))
  73.                 {
  74.                     System.out.println("MySQL: Connection timeout");
  75.                 }
  76.                 return con.isValid(10);
  77.             }
  78.             else
  79.             {
  80.                 return false;
  81.             }
  82.         }
  83.         catch(Exception ex)
  84.         {
  85.             ex.printStackTrace();
  86.             return false;
  87.         }
  88.     }
  89.    
  90.     public static void execute(String query)
  91.     {
  92.       try
  93.       {
  94.         Statement st = getConnection().createStatement();
  95.         st.execute(query);
  96.         st.close();
  97.       }
  98.       catch (SQLException e)
  99.       {
  100.         e.printStackTrace();
  101.       }
  102.     }
  103.  
  104.    
  105.     public static ResultSet select(String query)
  106.     {
  107.       ResultSet rs = null;
  108.       try
  109.       {
  110.         Statement st = getConnection().createStatement();
  111.         rs = st.executeQuery(query);
  112.       }
  113.       catch (SQLException e)
  114.       {
  115.         e.printStackTrace();
  116.       }
  117.       return rs;
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement