Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.comniemeer.mysqlbeispiel;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.bukkit.configuration.file.FileConfiguration;
- public class MySQL {
- private String host;
- private int port;
- private String user;
- private String password;
- private String database;
- private Main plugin;
- private Connection conn;
- public MySQL(Main main) throws Exception {
- this.plugin = main;
- FileConfiguration cfg = this.plugin.getConfig();
- String db = "MySQL.";
- this.host = cfg.getString(db + "Host");
- this.port = cfg.getInt(db + "Port");
- this.user = cfg.getString(db + "User");
- this.password = cfg.getString(db + "Passwort");
- this.database = cfg.getString(db + "Datenbank");
- this.openConnection();
- }
- public Connection openConnection() throws Exception {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
- this.conn = conn;
- return conn;
- }
- public Connection getConnection() {
- return this.conn;
- }
- public boolean hasConnection() {
- try {
- return this.conn != null || this.conn.isValid(1);
- } catch (SQLException e) {
- return false;
- }
- }
- public void queryUpdate(String query) {
- Connection conn = this.conn;
- PreparedStatement st = null;
- try {
- st = conn.prepareStatement(query);
- st.executeUpdate();
- } catch (SQLException e) {
- this.plugin.log.severe("Failed to send update '" + query + "'.");
- } finally {
- this.closeRessources(null, st);
- }
- }
- public void closeRessources(ResultSet rs, PreparedStatement st) {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- }
- }
- if (st != null) {
- try {
- st.close();
- } catch (SQLException e) {
- }
- }
- }
- public void closeConnection() {
- try {
- this.conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- this.conn = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement