-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e830623
commit bc445ee
Showing
7 changed files
with
258 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/eu/crackscout/crackcore/utils/DatabaseManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @author rzepk | ||
* | ||
* Nov 29, 2024 5:46:47 AM | ||
*/ | ||
package eu.crackscout.crackcore.utils; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class DatabaseManager { | ||
private Connection connection; | ||
|
||
/** | ||
* Initialisiert die Verbindung zur MariaDB-Datenbank. | ||
* | ||
* @param host Die IP-Adresse oder der Hostname der Datenbank. | ||
* @param port Der Port der Datenbank (Standard: 3306). | ||
* @param database Der Name der Datenbank. | ||
* @param username Der Benutzername für die Verbindung. | ||
* @param password Das Passwort für die Verbindung. | ||
* @throws SQLException Falls ein Fehler beim Aufbau der Verbindung auftritt. | ||
* @throws ClassNotFoundException Falls der JDBC-Treiber nicht gefunden wird. | ||
*/ | ||
public void connect(String host, int port, String database, String username, String password) throws SQLException, ClassNotFoundException { | ||
// MariaDB/MySQL JDBC-Treiber laden | ||
Class.forName("org.mariadb.jdbc.Driver"); | ||
|
||
// Verbindung zur Datenbank aufbauen | ||
String url = "jdbc:mariadb://" + host + ":" + port + "/" + database; | ||
connection = DriverManager.getConnection(url, username, password); | ||
System.out.println("Verbindung zur Datenbank erfolgreich hergestellt!"); | ||
} | ||
|
||
/** | ||
* Führt eine SELECT-Abfrage aus. | ||
* | ||
* @param query Die SQL-Abfrage. | ||
* @param params Parameter für die Abfrage (falls benötigt). | ||
* @return Das ResultSet mit den Ergebnissen. | ||
* @throws SQLException Falls ein Fehler bei der Abfrage auftritt. | ||
*/ | ||
public ResultSet executeQuery(String query, Object... params) throws SQLException { | ||
PreparedStatement statement = connection.prepareStatement(query); | ||
setParameters(statement, params); | ||
return statement.executeQuery(); | ||
} | ||
|
||
/** | ||
* Führt eine INSERT, UPDATE oder DELETE-Abfrage aus. | ||
* | ||
* @param query Die SQL-Abfrage. | ||
* @param params Parameter für die Abfrage (falls benötigt). | ||
* @throws SQLException Falls ein Fehler bei der Abfrage auftritt. | ||
*/ | ||
public void executeUpdate(String query, Object... params) throws SQLException { | ||
try (PreparedStatement statement = connection.prepareStatement(query)) { | ||
setParameters(statement, params); | ||
statement.executeUpdate(); | ||
} | ||
} | ||
|
||
/** | ||
* Beendet die Verbindung zur Datenbank. | ||
*/ | ||
public void disconnect() { | ||
if (connection != null) { | ||
try { | ||
connection.close(); | ||
System.out.println("Verbindung zur Datenbank geschlossen."); | ||
} catch (SQLException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Setzt Parameter für eine vorbereitete Abfrage. | ||
* | ||
* @param statement Die vorbereitete SQL-Anweisung. | ||
* @param params Die Parameter für die Abfrage. | ||
* @throws SQLException Falls ein Fehler beim Setzen der Parameter auftritt. | ||
*/ | ||
private void setParameters(PreparedStatement statement, Object... params) throws SQLException { | ||
for (int i = 0; i < params.length; i++) { | ||
statement.setObject(i + 1, params[i]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* @author rzepk | ||
* | ||
* Nov 29, 2024 5:45:07 AM | ||
*/ | ||
package eu.crackscout.crackcore.utils; | ||
|
||
/** | ||
* @author rzepk | ||
* | ||
*/ | ||
public class MySQL { | ||
|
||
} |