Advertisement
Guest User

MysqlClass

a guest
Jul 2nd, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Properties;
  9.  
  10. public class MySQL
  11. {
  12.   private Connection con;
  13.   private Statement st;
  14.   private ResultSet rs;
  15.   private PreparedStatement ps;
  16.   private Main r;
  17.  
  18.   public MySQL(Main reg)
  19.   {
  20.     this.r = reg;
  21.   }
  22.  
  23.  
  24. protected boolean loadDriver()
  25.   {
  26.     try
  27.     {
  28.       Class.forName("com.mysql.jdbc.Driver");
  29.       return true;
  30.     }
  31.     catch (ClassNotFoundException ex)
  32.     {
  33.       this.r.getLogger().severe("Could not load JDBC driver!");
  34.       ex.printStackTrace();
  35.     }
  36.     return false;
  37.   }
  38.  
  39.   protected String getJdbcUrl()
  40.   {
  41.     String host = this.r.getConfig().getString("connection.hostname");
  42.     String port = this.r.getConfig().getString("connection.port");
  43.     String name = this.r.getConfig().getString("connection.name");
  44.     String url = "jdbc:mysql://" + host + ":" + port + "/" + name;
  45.     return url;
  46.   }
  47.  
  48.   protected Properties getProperties()
  49.   {
  50.     String user = this.r.getConfig().getString("connection.username");
  51.     String pass = this.r.getConfig().getString("connection.password");
  52.     Properties prop = new Properties();
  53.     prop.put("autoReconnect", "true");
  54.     prop.put("user", user);
  55.     prop.put("password", pass);
  56.     prop.put("useUnicode", "true");
  57.     prop.put("characterEncoding", "utf8");
  58.     return prop;
  59.   }
  60.  
  61.   public boolean connect()
  62.   {
  63.     if (loadDriver())
  64.     {
  65.       this.r.getLogger().info("Connecting to database...");
  66.       try
  67.       {
  68.         this.con = DriverManager.getConnection(getJdbcUrl(), getProperties());
  69.         this.r.getLogger().info("Connected to database!");
  70.         return true;
  71.       }
  72.       catch (SQLException ex)
  73.       {
  74.         this.r.getLogger().severe("Could not connect to database!");
  75.         ex.printStackTrace();
  76.         return false;
  77.       }
  78.     }
  79.     return false;
  80.   }
  81.  
  82.   public boolean close()
  83.   {
  84.     this.r.getLogger().info("Disconnecting from database...");
  85.     if (isConn()) {
  86.       try
  87.       {
  88.         this.con.close();
  89.         this.r.getLogger().info("Disconnected from database!");
  90.         return true;
  91.       }
  92.       catch (SQLException ex)
  93.       {
  94.         this.r.getLogger().severe("Could not disconnect from database!");
  95.         return false;
  96.       }
  97.     }
  98.     this.r.getLogger().severe("No connection detected!");
  99.     return false;
  100.   }
  101.  
  102.   public boolean isConn()
  103.   {
  104.     if (this.con != null) {
  105.       try
  106.       {
  107.         if (this.con.isValid(1)) {
  108.           return true;
  109.         }
  110.         return false;
  111.       }
  112.       catch (SQLException ex)
  113.       {
  114.         this.r.getLogger().severe("Database error!");
  115.         ex.printStackTrace();
  116.         return false;
  117.       }
  118.     }
  119.     return false;
  120.   }
  121.  
  122.   public ResultSet query(String query)
  123.     throws SQLException
  124.   {
  125.     if (isConn())
  126.     {
  127.       this.st = this.con.createStatement();
  128.       this.st.execute(query);
  129.       this.rs = this.st.getResultSet();
  130.       return this.rs;
  131.     }
  132.     this.r.getLogger().severe("Database error!");
  133.     return null;
  134.   }
  135.  
  136.   public ResultSet query(PreparedStatement ps)
  137.     throws SQLException
  138.   {
  139.     if (isConn())
  140.     {
  141.       ps.execute();
  142.       this.rs = ps.getResultSet();
  143.       return this.rs;
  144.     }
  145.     this.r.getLogger().severe("Database error!");
  146.     return null;
  147.   }
  148.  
  149.   public PreparedStatement prepare(String query)
  150.     throws SQLException
  151.   {
  152.     if (isConn())
  153.     {
  154.       this.ps = this.con.prepareStatement(query);
  155.       return this.ps;
  156.     }
  157.     this.r.getLogger().severe("Database error!");
  158.     return null;
  159.   }  
  160.   public boolean isTable(String table)
  161.   {
  162.     try
  163.     {
  164.       String query = "SELECT * FROM " + table;
  165.       this.st = this.con.createStatement();
  166.       this.st.executeQuery(query);
  167.       return true;
  168.     }
  169.     catch (SQLException ex) {}
  170.     return false;
  171.   }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement