-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLAPI.java
More file actions
143 lines (128 loc) · 4.42 KB
/
MySQLAPI.java
File metadata and controls
143 lines (128 loc) · 4.42 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package de.maxistlegit.core.api;
/*
(MySQL-API)
This Class was created by MaxIstLegit
09/03/2022 | 07:28
*/
import java.sql.*;
/**
* <h1>Lightweight MySQL API</h1><br />
* <p>
* Usage: <br />
* - <bold>Write in main class ( "<code>MySQLAPI coinMySQL = new MySQLAPI(String username, String password, String host, String database)</code>" ) <br />
* - <bold>Make a try/catch with ( "<code>coinMySQL.startConnection()</code>" )</bold> <br />
* - <bold>Then you can use it</bold> <br />
* <br />
* - <code>getConnection()</code> [Connection] - gets the connection <br />
* - <code>isConnected()</code> [Boolean] - returns true if connected <br />
* - <code>startConnection()</code> [void] - Connects the MySQLAPI Instance <br />
* - <code>closeConnection()</code> [void] - Closes the MySQLAPI Instance <br />
* - <code>createNewTable(String tableName, String[] entries, String[] entryTypes)</code> [void] - creates a new table <br />
* - - INFORMATION: entires and entrytypes must match with the length of the array <br />
* - - - WRONG: <code>createNewTable(String "sixnine", new String[]{"foo", "bar"}, new String[]{"VARCHAR(100)"})</code> <br />
* - - - - that won't work <br />
* - - - RIGHT: <code>createNewTable(String "foo", new String[]{"foo", "bar"}, new String[]{"VARCHAR(100)", "BIGINT(64)"})</code> <br />
* <br />
*
* @author <bold>KeineSecrets</bold> <br />
* @version <bold>1.0.0</bold> <br />
*/
public class MySQLAPI {
final String username;
final String password;
final String host;
final String port;
final String database;
Connection connection;
public MySQLAPI(String username, String password, String host, String database) {
this.username = username;
this.password = password;
this.host = host;
this.database = database;
this.port = "3306";
}
public MySQLAPI(String username, String password, String host, String database, String port) {
this.username = username;
this.password = password;
this.host = host;
this.database = database;
this.port = port;
}
public boolean isConnected() {
return (connection != null);
}
/**
* Creates a new table
*
* @param tableName The name of the Table
* @param entries The Entries (names) (e.g. COINS)
* @param entryTypes The EntryTypes (e.g. VARCHAR(100) or BIGINT(64))
*/
public void createNewTable(String tableName, String[] entries, String[] entryTypes) {
if (isConnected()) {
StringBuilder stringBuilder = new StringBuilder();
try {
for (int i = 0; i < entries.length || i < entryTypes.length; i++) {
stringBuilder.append(entries[i].toUpperCase()).append(" ").append(entryTypes[i]).append(", ");
}
String statement = stringBuilder.toString();
if (statement.endsWith(", ")) {
statement = statement.substring(0, statement.length() - 2);
}
PreparedStatement preparedStatement = this.getStatement("CREATE TABLE IF NOT EXISTS " + tableName + " (" + statement + ")");
preparedStatement.executeUpdate();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
}
}
public void startConnection() {
if (!isConnected()) {
try {
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
System.out.println("Connected to database " + database + " with " + username + ".");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeConnection() {
if (isConnected()) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public Connection getConnection() {
if (isConnected()) return connection;
return null;
}
public PreparedStatement getStatement(String query) {
PreparedStatement preparedStatement = null;
if (isConnected()) {
try {
preparedStatement = getConnection().prepareStatement(query);
} catch (SQLException e) {
e.printStackTrace();
}
}
return preparedStatement;
}
public ResultSet getResult(String query) {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
if (isConnected()) {
try {
preparedStatement = getStatement(query);
resultSet = preparedStatement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}
return resultSet;
}
}