Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.habdxchlieb.Utils;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import de.habdxchlieb.Main.LifeGeldAPI;
- public class API_MySQL {
- private String HOST = LifeGeldAPI.host;
- private String DATABASE = LifeGeldAPI.Database;
- private String USER = LifeGeldAPI.user;
- private String PASSWORD = LifeGeldAPI.password;
- private Connection connection;
- public API_MySQL(String host, String database, String user, String password) {
- this.HOST = host;
- this.DATABASE = database;
- this.USER = user;
- this.PASSWORD = password;
- connect();
- }
- public void connect() {
- try {
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":3306/" + this.DATABASE + "?autoReconnect=true", this.USER, this.PASSWORD);
- System.out.println("§c§l[MySQL] §7Die verbindung zur MySQL wurde hergestellt!");
- }
- catch (SQLException e) {
- System.out.println("§c§l[MySQL] §4Die verbindung zur MySQL ist fehlgeschlagen!");
- }
- }
- public void close() {
- try {
- if (this.connection !=null) {
- this.connection.close();
- System.out.println("§c§l[MySQL] §aDie verbindung zur MySQL wurde erfolgreich beendet!");
- }
- }
- catch (SQLException e) {
- System.out.println("§c§l[MySQL] §5Fehler beim beenden der Verbindung zu MySQL! &6Fehler:" + e.getMessage());
- }
- }
- public void update(String qry) {
- try {
- if (this.connection.isClosed()) {
- connect();
- }
- Statement st = this.connection.createStatement();
- st.executeUpdate(qry);
- st.close();
- }
- catch (SQLException e) {
- System.err.println(e);
- }
- }
- public ResultSet query(String qry) {
- ResultSet rs = null;
- try {
- if(this.connection.isClosed()) {
- connect();
- }
- Statement st = this.connection.createStatement();
- st.executeQuery(qry);
- return st.getResultSet();
- }
- catch (SQLException e) {
- System.err.println(e);
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement