Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MySQL {
- public static String Host ="";
- public static String Database = "";
- public static String User = "";
- public static String Password = "";
- private static Connection con;
- public MySQL(String host, String database, String user, String password) {
- MySQL.Host = host;
- MySQL.Database = database;
- MySQL.User = user;
- MySQL.Password = password;
- connect();
- }
- public static void connect() {
- if(!isConnected()){
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + Host + ":3306/" + Database + "?autoReconnect=true", User, Password);
- System.out.println("[MySQL] Die Verbindung zur MySQL wurde hergestellt!");
- } catch (SQLException e) {
- System.out.println("[MySQL] Die Verbindung zur MySQL ist fehlgeschlagen! Fehler: " + e.getMessage());
- }
- }
- }
- public static void close() {
- if(!isConnected()){
- try {
- con.close();
- System.out.println("[MySQL] Die Verbindung zur MySQL wurde Erfolgreich beendet!");
- } catch (SQLException e){
- e.printStackTrace();
- }
- }
- }
- public static boolean isConnected(){
- return con != null;
- }
- public void update(String qry) {
- if(isConnected()){
- try {
- con.createStatement().executeUpdate(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static ResultSet getResult(String qry) {
- if(isConnected()){
- try {
- return con.createStatement().executeQuery(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement