Advertisement
comniemeer

MySQL-Klasse

Aug 4th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. package de.comniemeer.mysqlbeispiel;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import org.bukkit.configuration.file.FileConfiguration;
  10.  
  11. public class MySQL {
  12.    
  13.     private String host;
  14.     private int port;
  15.     private String user;
  16.     private String password;
  17.     private String database;
  18.    
  19.     private Main plugin;
  20.    
  21.     private Connection conn;
  22.    
  23.     public MySQL(Main main) throws Exception {
  24.         this.plugin = main;
  25.        
  26.         FileConfiguration cfg = this.plugin.getConfig();
  27.         String db = "MySQL.";
  28.        
  29.         this.host = cfg.getString(db + "Host");
  30.         this.port = cfg.getInt(db + "Port");
  31.         this.user = cfg.getString(db + "User");
  32.         this.password = cfg.getString(db + "Passwort");
  33.         this.database = cfg.getString(db + "Datenbank");
  34.        
  35.         this.openConnection();
  36.     }
  37.    
  38.     public Connection openConnection() throws Exception {
  39.         Class.forName("com.mysql.jdbc.Driver");
  40.         Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
  41.         this.conn = conn;
  42.         return conn;
  43.     }
  44.    
  45.     public Connection getConnection() {
  46.         return this.conn;
  47.     }
  48.    
  49.     public boolean hasConnection() {
  50.         try {
  51.             return this.conn != null || this.conn.isValid(1);
  52.         } catch (SQLException e) {
  53.             return false;
  54.         }
  55.     }
  56.    
  57.     public void queryUpdate(String query) {
  58.         Connection conn = this.conn;
  59.         PreparedStatement st = null;
  60.         try {
  61.             st = conn.prepareStatement(query);
  62.             st.executeUpdate();
  63.         } catch (SQLException e) {
  64.             this.plugin.log.severe("Failed to send update '" + query + "'.");
  65.         } finally {
  66.             this.closeRessources(null, st);
  67.         }
  68.     }
  69.    
  70.     public void closeRessources(ResultSet rs, PreparedStatement st) {
  71.         if (rs != null) {
  72.             try {
  73.                 rs.close();
  74.             } catch (SQLException e) {
  75.             }
  76.         }
  77.         if (st != null) {
  78.             try {
  79.                 st.close();
  80.             } catch (SQLException e) {
  81.             }
  82.         }
  83.     }
  84.    
  85.     public void closeConnection() {
  86.         try {
  87.             this.conn.close();
  88.         } catch (SQLException e) {
  89.             e.printStackTrace();
  90.         } finally {
  91.             this.conn = null;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement