-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
70 lines (57 loc) · 1.85 KB
/
Database.java
File metadata and controls
70 lines (57 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package de.primeapi.util.sql;
import de.primeapi.util.sql.queries.SelectStatement;
import de.primeapi.util.sql.queries.UpdateStatement;
import lombok.Getter;
import lombok.NonNull;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author Lukas S. PrimeAPI
* created on 08.03.2022
* crated for SQLAdapter
*/
@Getter
public class Database {
private static Database instance;
@NonNull
private final Connection connection;
/**
* Initializies the Database.
* @param connection The SQL-Connection the statements shall be sent to
*/
public Database(@NonNull Connection connection) {
this.connection = connection;
}
/**
* Initializes the Database by creating a jdbc connection.
* @param host The Hostname (including the port)
* @param database The name of the database
* @param username The username of the sql-user
* @param password The password for the user (may be null)
* @throws SQLException if a database access error occurs
*/
public Database(String host, String database, String username, String password) throws SQLException {
this.connection = DriverManager.getConnection(
String.format("jdbc:mysql://%s/%s?autoReconnect=true", host, database),
username,
password)
;
}
/**
* Used for select statements
* @param query The Query which shall be executed
* @return a {@link SelectStatement} for building an {@link de.primeapi.util.sql.queries.Collector}
*/
public SelectStatement select(String query){
return new SelectStatement(this, query);
}
/**
* Used fpr update statements (UPDATE, INSERT, CREATE, DROP, DELETE)
* @param query The Query which shall be executed
* @return a {@link UpdateStatement} for building an {@link de.primeapi.util.sql.queries.Collector}
*/
public UpdateStatement update(String query){
return new UpdateStatement(this, query);
}
}