Advertisement
Guest User

MySQL Nukkit

a guest
Jan 6th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package com.horus.lobby.utils;
  2.  
  3. import lombok.Getter;
  4. import lombok.Setter;
  5. import lombok.ToString;
  6.  
  7. import java.sql.*;
  8.  
  9. @Getter
  10. @Setter
  11. @ToString(exclude = {"username", "password"})
  12. public class MySQL
  13. {
  14.     private String hostname;
  15.  
  16.     private Integer port;
  17.  
  18.     private String username;
  19.  
  20.     private String password;
  21.  
  22.     private String database;
  23.  
  24.     private Connection connection;
  25.  
  26.     private Statement statement;
  27.  
  28.     private ResultSet result;
  29.  
  30.     public MySQL()
  31.     {
  32.         this("localhost", 3306);
  33.     }
  34.  
  35.     public MySQL(String hostname)
  36.     {
  37.         this(hostname, 3306);
  38.     }
  39.  
  40.     public MySQL(Integer port)
  41.     {
  42.         this("localhost", port);
  43.     }
  44.  
  45.     public MySQL(String hostname, Integer port)
  46.     {
  47.         this.setHostname(hostname);
  48.         this.setPort(port);
  49.     }
  50.  
  51.     public Boolean connect(String username, String password, String database)
  52.     {
  53.         this.setUsername(username);
  54.         this.setPassword(password);
  55.  
  56.         try {
  57.             this.setConnection(DriverManager.getConnection("jdbc:mysql://"+ this.getHostname() + ":" + this.getPort() + "/" + this.getDatabase() + "?" +
  58.                             "user="+ this.getUsername() + "&password=" + this.getPassword()));
  59.             return true;
  60.         }
  61.         catch (SQLException ex)
  62.         {
  63.             return false;
  64.         }
  65.     }
  66.  
  67.     public Boolean exec(String query)
  68.     {
  69.         try
  70.         {
  71.             this.setStatement(this.getConnection().createStatement());
  72.  
  73.             if (this.getStatement().execute(query))
  74.             {
  75.                 this.setResult(this.getStatement().getResultSet());
  76.             }
  77.             return true;
  78.         }
  79.         catch (SQLException ex)
  80.         {
  81.             return false;
  82.         }
  83.         finally
  84.         {
  85.             if (this.getResult() != null)
  86.             {
  87.                 try
  88.                 {
  89.                     this.getResult().close();
  90.                 }
  91.                 catch (SQLException sqlEx)
  92.                 {
  93.  
  94.                 }
  95.  
  96.                 this.setResult(null);
  97.             }
  98.  
  99.             if (this.getStatement() != null)
  100.             {
  101.                 try
  102.                 {
  103.                     this.getStatement().close();
  104.                 }
  105.                 catch (SQLException sqlEx)
  106.                 {
  107.  
  108.                 }
  109.  
  110.                 this.setStatement(null);
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement